Step-by-Step Guide: Installing Jenkins on AWS EC2 for CI/CD

Step-by-Step Guide: Installing Jenkins on AWS EC2 for CI/CD

  • Introduction

    Jenkins is one of the most popular tools for automating CI/CD processes, enabling seamless software development and deployment. If you're looking to set up Jenkins in a cloud environment, AWS EC2 is a fantastic choice due to its scalability and and flexibility . In this guide, I'll walk you through the straightforward process of installing Jenkins on an AWS EC2 instance, step by step. By the end, you'll have a fully functional Jenkins setup, ready to streamline your development workflows.

    Let’s get started!

Prerequisites:

  1. AWS Account – Active AWS account to create EC2 instances.

  2. EC2 Instance – Ubuntu or Amazon Linux AMI with sufficient resources (e.g., t2.micro)

  3. Security Group – Inbound rules:

  • Port 22 for SSH.

  • Port 8080 for Jenkins

4. SSH Client – Tool for connecting to the EC2 instance (e.g., terminal or PuTTY or WSL2).

🛠 Steps to Install Jenkins on AWS EC2:

1️⃣ Launch an EC2 Instance

  • Choose Ubuntu or Amazon Linux AMI.

  • Configure the necessary security group (allow port 8080 for Jenkins)

2️⃣ Connect to Your EC2 Instance via SSH

  • Locate Private Key – Use your .pem file downloaded during instance creation.

  • Find Public IP – Copy your instance’s public IPv4 address from the AWS EC2 dashboard.

  • Run SSH Command

ssh -i /path/to/key.pem ubuntu@<EC2-Public-IP>
  • Verify Connection – You’ll see a shell prompt like ubuntu@ip-xx-xx-xx-xx

    3️⃣ Prepare Your Instance:

  • Update the package list and upgrade installed packages: Update your package manager using the following command .

sudo apt-get update && sudo apt-get upgrade

The command sudo apt update && sudo apt upgrade -y does the following:

  • sudo apt update – Updates the package list to ensure your system knows about the latest available software.

  • sudo apt upgrade -y – Upgrades all installed packages to their latest versions automatically (with -y for confirmation).

4️⃣ Install Java (Required for Jenkins)

  • Jenkins requires Java to run. To install it:
  1. Run the following command to install OpenJDK:
sudo apt install fontconfig openjdk-17-jre

Verify the installation by checking the Java version:

 java --version

5️⃣Setup Jenkins

  1. Download the Jenkins Repository Key: Run the following command to download and save the Jenkins GPG key:
sudo wget -O /usr/share/keyrings/jenkins-keyring.asc \
  https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key
  • This command downloads the Jenkins GPG key and saves it to a keyring on your system, allowing you to securely install Jenkins from its official repository.

2.Add the Jenkins Repository: Add the Jenkins stable 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
  • This command configures your system to recognize the Jenkins repository for package installations.

Update your system again: Run the following command and update your system again which will contain all the necessary packages required to install Jenkins.

sudo apt-get update

6️⃣ Install Jenkins (LTS Version)

To install Jenkins on your EC2 instance, run the following command:

sudo apt-get install jenkins -y

This command installs the Jenkins LTS (Long-Term Support) version and all necessary dependencies. The -y flag automatically confirms the installation, allowing it to proceed without further input. After this step, Jenkins will be installed and ready for configuration.

  • Verify the installation by checking the Jenkins version:

      jenkins --version
    

    7️⃣ Edit Inbound Rules to Allow Jenkins Access:

  • Jenkins runs on port 8080 by default. To access Jenkins from your web browser, you need to allow inbound traffic on port 8080.

    1. Go to EC2 Console: Select your EC2 instance.

    2. Edit Security Group: Click on the security group linked to your instance.

    3. Modify Inbound Rules: Click "Edit inbound rules".

    4. Add Rule for Port 8080:

  • Type: Custom TCP Rule

  • Port Range: 8080

  • Source: Anywhere (0.0.0.0/0) or your specific IP range.

  1. Save: Apply the changes.

This opens port 8080 to access Jenkins from your browser.

8️⃣ Access Jenkins:

To access jenkins:

  1. Find your EC2 instance’s public IP in the AWS console.

  2. Open your browser and go to:

     http://<EC2-Public-IP>:8080
    

    9️⃣ Unlock and Set Up Jenkins:

    Initially, Jenkins is locked for security purposes. To unlock and complete the setup, follow these steps:

    1. Get the Unlock Key:
      On the Jenkins setup page, you'll see a prompt for an unlock key. To find it, run:

       sudo cat /var/lib/jenkins/secrets/initialAdminPassword
      
    2. Enter the Unlock Key:
      Copy the key and paste it into the setup page to unlock Jenkins.

    3. Install Suggested Plugins:
      After unlocking, Jenkins will prompt you to install suggested plugins. Click Install suggested plugins.

    4. Create Admin User:
      After plugin installation, create your Jenkins admin user and configure the system settings.

Jenkins is now ready for use!

Conclusion

You’ve successfully installed and configured Jenkins on your AWS EC2 instance! By following the steps to install Java, add the Jenkins repository, configure the security group, and complete the initial setup, Jenkins is now accessible and ready to use for continuous integration and delivery.