How To Setup Jenkins using BootStrap Scripts in AWS EC2 Instance while Launching

How to Run commands on your EC2 instance at launch

How To Setup Jenkins using BootStrap Scripts in AWS EC2 Instance while Launching

What is a bootstrap script in aws?

If you want to execute some commands during boot up(launch), you can execute them easily by loading the script in the user data section during the 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 an EC2 instance in the AWS console?

What is an EC2 instance?

It is a virtual server provided by AWS. Amazon Elastic Compute Cloud (Amazon EC2) provides scalable computing capacity in the Amazon Web Services (AWS) Cloud. Using Amazon EC2 eliminates your need to invest in hardware upfront, so you can develop and deploy applications faster. You can use Amazon EC2 to launch as many or as few virtual servers as you need, configure security and networking, and manage storage. Amazon EC2 enables you to scale up or down to handle changes in requirements or spikes in popularity, reducing your need to forecast traffic.

We will be using this EC2 to set up Jenkins. Please follow the below steps to create an EC2 instance.

Pre-requisites:

Shell script for setting up Jenkins in Ubuntu instance

#!/bin/bash
# Shell script for installing Java, Jenkins and Maven in Ubuntu EC2 instance

# Command for installing Java 11
sudo apt-get update
sudo apt-get install default-jdk -y

# Command for installing maven
sudo apt-get install maven -y

# Script for Jenkins installation

#Add Repository key to the system
wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add -

#  Append debian package repo address to the system
echo deb http://pkg.jenkins.io/debian-stable binary/ | sudo tee /etc/apt/sources.list.d/jenkins.list

sudo apt-get update
# Install Jenkins

sudo apt-get install jenkins -y

echo "Jenkins installed successfully.."

Steps:

1. Login to the AWS console by clicking this link -:  aws.amazon.com/console

Click on All services, Click on Compute -->  Click on EC2

2. Click on Launch instance

3. Enter Name as EC2 and enter 1 as the number of instances

4. Select Ubuntu and choose Ubuntu server 18.0.4 as AMI

5. Enter t2.small as instance type

6. You can choose the existing Key Or You Can create new key-pair

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 Or You can choose any number of Storage

Steps to add bootstrap script during EC2 launch

Click on Advanced Details:

Now go to the User Data section and Copy the script from above Shell script command

9. Click on Launch Instance.

Click on View instances

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

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

Check the Console Output Logs in the EC2 instance

Login to an EC2 instance, and type the below command

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

This will give the output of the 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 security rules)

Enter Jenkins Admin password:

Execute the below command as Jenkins user Or admin user 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.


Conclusion

EC2 is popular among Amazon customers because of its quick installations which allow users to access the computing infrastructure within minutes at a low cost. EC2 also lets customers easily increase and decrease their capacity for a long period and has a range of tools to secure data privacy. Finally, EC2 can service customers all around the world.

Did you find this article valuable?

Support Biswajit Mohapatra by becoming a sponsor. Any amount is appreciated!