Implementing CI/CD Pipeline with Jenkins and AWS EC2 — Part 1: Setting Up Jenkins and EC2

Ahmad Al Mezaal
7 min readAug 13, 2023

--

In the software development ecosystem, Continuous Integration (CI) and Continuous Delivery/Deployment (CD) have become core components in streamlining and automating the process from code to production. Jenkins, an open-source automation server, is a popular choice for setting up CI/CD pipelines. AWS EC2 provides scalable compute capacity in the cloud, making it a fitting choice for hosting Jenkins. In this article, we’ll walk through setting up a CI/CD pipeline using Jenkins hosted on an AWS EC2 instance.

Prerequisites:

  1. AWS account: If you don’t have one, sign up here.
  2. Basic knowledge of Git and AWS EC2.

Before diving in, let’s set the stage. You might wonder about the intricacies of setting up Jenkins on AWS. Fret not! With our step-by-step guide, enhanced by screenshots, it’s simpler than it seems. If you don’t have a project at hand or wish to follow along with our exact steps, I’ve provided a sample express.js project on GitHub. This sample is what we’ll be using to demonstrate the CI/CD implementation, and it’ll also contain the Jenkinsfile for your reference. Whether you’re a seasoned pro or a beginner, this guide is crafted for clarity and ease. Let’s get started!

Steps:

Creating an AWS EC2 Instance:

  1. Sign into the AWS Management Console here.
  2. Head to EC2 dashboard, click on “Launch Instance”.
EC2 dashboard

3. Give your instance a name: in our case we’ll just call it todos-backend but feel free to call it whatever you want. After that, choose an Amazon Machine Image (AMI) — For this guide, we’re using the Ubuntu Server.

4. Choose an instance type: t2.micro is fine for basic Jenkins tasks and is free-tier eligible.

AMI

5. Key Pair Configuration: As you proceed, you’ll be prompted to set a key pair for secure access:

  • Select “Create a new key pair”.
  • Name it, for instance, jenkins-key.
  • Download the resulting .pem file.

CAUTION: The .pem file is a one-time download, so keep it secure; you’ll use it for future access to your EC2 instance.

Create key pair

6. Configure Security Group: By default, AWS will suggest a security group that allows SSH access from anywhere. This will be needed to connect to the instance.

Security Group

7. Add Storage: The default 8GB is enough for Jenkins, but adjust based on your project needs.

8. Review and Launch: Leave the default settings for now, double-check your configurations and hit launch.

Launching Instance

After launching, AWS will start your instance, and in a few minutes, it should be up and running. You’ll be able to see its status and retrieve its public IP address from the EC2 dashboard, which will be essential for connecting to it.

EC2 Dashboard

Connecting to the AWS EC2 Instance:

1. Prepare the Key File: Before connecting, you must ensure your key file (the .pem file you downloaded earlier) has the correct permissions. On your local terminal, navigate to the directory where the key is stored and run:

chmod 400 path_to_your_key.pem

2. SSH into the EC2 Instance:
Head back to the AWS EC2 Dashboard.

  • Select your instance and click on the “Connect” button.
  • In the pop-up window, navigate to the “SSH client” tab. Here, AWS provides a ready-to-use SSH command tailored for your instance(in the example).
  • Copy that command and paste it into your terminal or command prompt to establish an SSH connection.

It would be something like this:

ssh -i path_to_your_key.pem ubuntu@your-ec2-public-ip

Executing this command would allow us to connect to the instance so we can install Jenkins there.

Installing Jenkins on the EC2 Instance:

Before we begin, you can always refer to the official Jenkins installation on Linux documentation for detailed steps and updates on the process.

1. Update the System: It’s good practice to begin by updating the package repositories and software packages. Run:

sudo apt update && sudo apt upgrade -y

2. Install Java: Jenkins is built using Java, and we’ll need it for Jenkins to run. Install the OpenJDK 11 package:

sudo apt install openjdk-17-jre

After the installation, verify the Java version by running:

java -version

3. Add Jenkins Repository: Jenkins provides a dedicated repository for its packages. Follow the steps below to add it:

  • Import the Jenkins key:
curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | sudo tee /usr/share/keyrings/jenkins-keyring.asc > /dev/null
  • Add the Jenkins repository to your system:
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

Install Jenkins: With the repository set up, install Jenkins:

sudo apt-get update
sudo apt-get install jenkins

4. Start Jenkins: Once installed, initialize the Jenkins server:

sudo systemctl enable jenkins

You can always make sure that Jenkins is running by executing the following:

sudo systemctl status jenkins

or

sudo service jenkins status
Jenkins status

Congratulations! You’ve successfully installed Jenkins on your AWS EC2 instance. The next steps would be to access our app and Jenkins from the browser.

Accessing Jenkins on your EC2 Instance:

For Jenkins and our application to be accessible from the outside world, we’ll need to expose the required ports. Follow these steps to open up ports 8080 (for Jenkins) and 3000 (can be a port of your choice, it’s for our app):

1. Head Back to AWS EC2 Dashboard: navigate to the EC2 dashboard and go to your instance details.

2. Navigate to Security Groups: Scroll down to the bottom tab, and click on the “Security” tab. After that, expand the “Security details” section and click on the ID of the security group, that will take your to its settings.

Security Group

3. Expose ports: Once you are there, click on the “Inbound rules” tab and hit the “Edit inbound rules” button. This will open the security group settings to allow us to add the ports that we want to expose to the outside world. We’re going to add ports “8080” and “3000”. Make sure to make the source “Anywhere” and then, hit “Save rules”.

Security group — Inbound rules

4. Open browser: Now that we exposed our ports, we can access Jenkins in our browser by visiting the following:

http://your-ec2-public-ip:8080

Replace your-ec2-public-ip with the actual public IP address of your AWS EC2 instance. You should see something like this:

Unlock Jenkins

Retrieving Jenkins Initial Admin Password:

Upon accessing Jenkins for the first time, you’ll be presented with an “Unlock Jenkins” screen.

1. Retrieve the Password: To get the initial admin password, execute the following command on your EC2 instance:

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

This will display a long alphanumeric string, which is the password you need.

2. Use the Password: Paste this password into the “Administrator password” field on the Jenkins unlock screen.

Note: While it’s advisable to change this password for security reasons, we’ll keep it as-is for the simplicity of this tutorial.

3. Plugin Installation: Jenkins will suggest installing a set of default plugins. These plugins provide a lot of the base functionality for Jenkins, so it’s recommended to go with the suggested setup. Click on “Install suggested plugins” to proceed.

4. Administrator Setup: Once the plugins are installed, Jenkins will prompt you for admin user setup. For the purpose of this tutorial, we won’t create a new admin user. Instead, click on the “Continue as admin” option.

5. Jenkins URL: Jenkins will then ask you to confirm the instance’s URL. The default should typically be correct (it will be your EC2 instance’s IP followed by :8080). Click “Save and Finish”.

With these steps, Jenkins is fully set up and you’re ready to start building your CI/CD pipeline!

Jenkins Dashboard

In Conclusion:
Setting up Jenkins on AWS EC2 can seem daunting, but with the right guidance, it becomes a manageable task. We’ve successfully installed Jenkins and made it accessible for future CI/CD operations. This foundation will be crucial as we dive deeper into the intricacies of Continuous Integration and Deployment in subsequent parts of this guide.

Up Next:
Check “Implementing CI/CD Pipeline with Jenkins and AWS EC2 — Part 2: Configuring Our App’s CI/CD Pipeline”, where we’ll delve into setting up our app on Jenkins pipeline, and getting everything ready for a seamless deployment.

--

--