CI/CD: Configuring Jenkins for AWS EC2 Instance:

A step by step guide to configure your Jenkins with AWS EC2 instance

Ali Uzair Mehdi
Published in
6 min readNov 29, 2023

--

CI/CD is a set of practices in software development that involve the continuous integration of code changes, automated testing, and the automated delivery or deployment of software to production environments, aiming to improve development efficiency, code quality, and release speed.

Amazon EC2 is a web service provided by AWS that enables users to rent virtual servers, known as instances, in the cloud. EC2 allows developers to quickly scale compute capacity based on demand, making it a fundamental component of many cloud-based applications. Together, CI/CD and EC2 form a powerful alliance, empowering developers to rapidly iterate, deploy, and scale applications in the ever-evolving landscape of modern software development. The benefits of EC2 are:

  • Scalability: EC2 instances can be easily scaled up or down based on the application’s demand, ensuring optimal performance and cost efficiency.
  • Flexibility: AWS EC2 offers a wide range of instance types with different compute, memory, and storage configurations, allowing users to choose the most suitable instance for their specific workloads.
  • Pay-as-You-Go Pricing: Users pay only for the compute capacity they consume, enabling cost savings by avoiding upfront investments and providing flexibility to adapt to changing requirements.

We will walk through the process of setting up Jenkins on an AWS EC2 instance, creating a basic CI/CD pipeline, and exploring best practices for efficient and secure automation.

Prerequisites:

Following are some prerequisites for setting up Jenkins on EC2 instance:

  1. Must have an AWS account.
  2. Basic knowledge of EC2 and Jenkins.

Launching an EC2 Instance

For launching an instance to host Jenkins, you need to follow these steps:

Step 1: Navigate to the EC2 Dashboard and launch an instance from there.

EC2 Dashboard

Step 2: Give your instance a name and select the image for OS e.g. Ubuntu.

Launching Instance

Step 3: Create a new key pair.

Creating new Key Pair

Note: Jenkins server normally takes up higher processing power so you can change the instance type from t3.micro to t3.medium so that it processes your Jenkins pipeline with ease.

Step 4: Edit Inbound rules for your security group i.e. you need to configure your security group to allow inbound traffic on port 8080 (Jenkins default port).

Note: Security Groups are a fundamental component of network security in Amazon Web Services (AWS) and they act as firewalls. Through configuring security groups we can control who can access one’s instance.

Network Settings

Step 5: Launch the instance and copy the public IPv4 address of the instance you created.

Instance Details

Step 6: Connect to your instance through git bash or similar software or EC2 serial console.

  1. Download and Install git bash from : https://git-scm.com/downloads.
  2. Go to the directory where your key “Instance_for_Jenkins” was downloaded.
  3. Execute the following command on the git bash:
ssh -i your_pem_key_file.pem ubuntu@instance_public_ip_address
// for example
ssh -i Instance_for_Jenkin.pem ubuntu@16.170.214.4
Authentication Successful

Step 7: Install java and Jenkins with the following commands:

Step 7a: Update all your packages:

sudo apt update

Step 7b: Install Java version 11:

sudo apt install openjdk-11-jdk -y

Step 7c: Install Jenkins by executing these commands:

sudo wget -O /usr/share/keyrings/jenkins-keyring.asc \
https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
/etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt-get update
sudo apt-get install fontconfig openjdk-11-jre
sudo apt-get install jenkins

Step 8: Configuring VPN on EC2 (Optional):
If the application under test which will be accessed by the Jenkins server is on a server that can only be accessed internally within a company or an organization then one needs to configure a VPN on the EC2 as well. We need to configure VPN on EC2 because only connecting the local machine to the VPN is not enough since the AUT (Application Under Testing) will always be accessed by Jenkins through the instance’s IP address. You need to have your vpn configuration file through which you can access your application on your local machine.

In my case, it’s OpenVPN:

Step 8a: Install OpenVPN on your EC2 instance with the command:

sudo apt install openvpn

Step 8b: Move your vpn configuration file to your instance by running the following command:

ssh -i your_pem_key_file.pem ubuntu@instance_public_ip_address:/home/ubuntu
// for example
scp -i Instance_for_Jenkins.pem vpn_conf.ovpn ubuntu@16.170.214.4:/home/ubuntu

Step 8c: Check the gateway by the command:

netstat -anr

You’ll see something like this, you need to copy the first gateway IP

Step 8d: Add a routing scheme:

sudo route add -host your_instance_public_ip gw 172.31.16.1

Now before you connect to your VPN, it’s important to have another git bash terminal open and connected to the instance for Jenkins server as the ssh key becomes invalid after you connect to the VPN.

Step 8e: Connect to your VPN by running the following command:

sudo openvpn --config your_vpn_configuration_file.ovpn 

You’ll be connected to your vpn and now your Jenkins can access the application. On the second git bash terminal you can start your Jenkins server.

Step 9: Start your Jenkins Server:

Step 9a: Start the Jenkins server by the following command:

sudo systemctl start jenkins

Step 9b: Check the status of the server by the following command:

sudo systemctl status jenkins

Step 9c: Enable the Jenkins server by the command:

sudo systemctl enable jenkins

Jenkins will be enabled.

Step 10: Run Jenkins on your instance’s public IPv4 address on port 8080.

Administrator Password For Jenkins

Step 11: Fill in the Administrator password with the key returned as a result of executing the following command.

sudo cat /var/lib/jenkins/secrets/initialAdminPassword
// you'll get some output like 54fd42fbd44e4927ae78fas3cacc18cb
// copy this key and paste in the Administrator password

Step 12: Install the necessary plugins as per your need.

Jenkins Plugins

Step 13: Create your first Admin user or you can continue as an admin:

Admin User Login

It will lead you to your main Jenkins dashboard. Now you are ready to create jobs and build them as per your need.

Jenkins Dashboard

CRUX:

In this article we touched upon:

  • Simplified Jenkins Setup on EC2: The process of making Jenkins function on an EC2 instance was straightforward and enjoyable.
  • Jenkins Superpowers: Witnessing the synergy between Jenkins and EC2 felt like observing a powerful duo, enhancing the efficiency and flexibility of software delivery.

For those intrigued by cloud software delivery, they are encouraged to experiment with Jenkins and EC2. It’s akin to uncovering a new superpower that facilitates the magic of software development!

--

--

Writer for

SQA Engineer @ Emumba | Automation Testing | API Testing | Performance Testing | Load Testing | Jenkins | JMeter | K6 | Grafana