Wednesday, September 14, 2022

How To Setup Jenkins using BootStrap Scripts in AWS EC2 Instance while Launching | How to Run commands on your EC2 instance at launch

 

What is bootstrap script in aws?

If you want to execute some commands during boot up(launch), you can execute it easily by loading script in user data section during EC2 launch. Bootstrap scripts run only once - when the instance is instantiated for the 1st time.

Please follow the below steps to create an EC2 instance.  We will be installing Java, Maven and Jenkins during boot up.

How to create EC2 instance in AWS console?

What is EC2 instance? 

It is virtual server provided by AWS. We will be using this EC2 to setup Jenkins. Please follow the below steps to create an EC2 instance.

Pre-requisites:

Steps:
1: Login to AWS console by clicking this link -->  https://aws.amazon.com/console/
click on All services, Click on Compute -->  Click on EC2


2. Click on Launch instance


3. Enter Name as Jenkins-EC2 and enter 1 as number of instance


4. Select Ubuntu and choose Ubuntu server 18.0.4 as AMI




5. Enter t2.small as instance type
6. You can choose existing Key
7. Under Network settings, Click Edit



Add port range as 8080 and select AnyWhere as Source Type, that should enter 0.0.0.0/0 as Source

8. Enter 10 GB as storage 

Steps to add bootstrap script during EC2 launch


Click on Advanced Details:

go to User Data section and Copy the script from this link.


9. Click on Launch Instance.

Click on View instances

Now you should be able to view instances in AWS console. 

once EC2 is provisioned, you can login and you will be able to see Java, Maven and Jenkins installed in EC2 instance after launch.

Check the Console Output Logs in EC2 instance

Login to EC2 instance, and type the below command:

tail -f /var/log/cloud-init-output.log

This will give the output of bootstrap execution

Verify if Java got installed.

java -version
mvn --version


Go to the browser and try to access Jenkins in the browser, Jenkins should be coming up.(make sure you open port 8080 in the firewall rules)


Enter Jenkins Admin password:
Execute below command to get Jenkins admin password

sudo cat /var/lib/jenkins/secrets/initialAdminPassword

Enter the password and click on continue

Click on Suggested plug-ins and setup the admin user for Jenkins.

Watch the steps in YouTube channel:


1 comment: