Getting Started with Jenkins on AWS EC2

Jenkins is a powerful open-source automation server widely used for continuous integration and continuous delivery (CI/CD) pipelines. Deploying Jenkins on AWS EC2 allows you to use scalable infrastructure while automating various aspects of your software development lifecycle. In this guide, we’ll walk you through installing Jenkins on an AWS EC2 instance, configuring it to run efficiently, and addressing common setup issues.

Introduction to Installing Jenkins on AWS EC2

Installing Jenkins on an AWS EC2 instance allows developers to set up and manage CI/CD pipelines in the cloud. This guide will cover everything from launching your EC2 instance to running a sample project in Jenkins, ensuring that you have a fully functional Jenkins server ready for your development needs.

Prerequisites for a Smooth Installation Process

Before diving into the installation, ensure you have the following:

  • An AWS account with appropriate permissions to create and manage EC2 instances.
  • Basic knowledge of AWS EC2, SSH, and Linux command line.
  • An SSH client is installed on your local machine to connect to the EC2 instance.

Setting Up the AWS EC2 Environment

Launching and Connecting to an EC2 Instance

  1. Launch an EC2 Instance: Log in to your AWS account, navigate to the EC2 dashboard, and launch a new instance. Choose an Amazon Linux 2 AMI (or another preferred Linux distribution) and an instance type (e.g., t2.micro for testing purposes).
  2. Configure Security Group: Ensure your security group allows inbound traffic on ports 22 (SSH) and 8080 (Jenkins default port). You’ll adjust these settings later to accommodate Jenkins on a custom port.
  3. Download the PEM File: When launching your instance, download the PEM key file for SSH access.
  4. Connect via SSH: Open your SSH client and connect to your EC2 instance using the following command:
    ssh -i /path/to/your-key.pem ec2-user@your-ec2-public-ip

Installing Necessary Packages for Jenkins Compatibility

Once connected to your EC2 instance, update the package list and install the necessary packages:

sudo yum update -y

sudo yum install java-11-openjdk-devel -y

Installing Jenkins on the EC2 Instance

Two Methods for Installing Jenkins: Local Download and Direct Server Download

Method 1: Local Download and Upload

  1. Download the Jenkins package from Jenkins’s official site on your local machine.
  2. Use SCP (Secure Copy Protocol) to upload the Jenkins package to your EC2 instance.
    scp -i /path/to/your-key.pem jenkins.war ec2-user@your-ec2-public-ip:/home/ec2-user/
  3. Move the package to the desired directory and install it.

Method 2: Direct Server Download Using wget Command

For a more streamlined process, you can download Jenkins directly on your EC2 instance:

wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo

sudo rpm –import https://pkg.jenkins.io/redhat-stable/jenkins.io.key

sudo yum install jenkins -y

Starting the Jenkins Server

Once installed, start the Jenkins service:

sudo systemctl start jenkins

Jenkins typically runs on port 8080 by default, but you may want to change this for security or other reasons.

Initial Attempt to Run Jenkins and Address Port Configuration Issues

If you encounter issues where Jenkins fails to start on the default port due to conflicts, you must change the port configuration.

  1. Modify Jenkins Configuration: Open the Jenkins configuration file.
    sudo vi /etc/sysconfig/jenkins
  2. Change the Port: Look for the line that reads JENKINS_PORT=”8080″ and change it to your desired port, 9999.
    JENKINS_PORT=”9999″
  3. Restart Jenkins:
    sudo systemctl restart jenkins

Adjusting Security Group Settings for Port 9999

To access Jenkins on the new port, adjust your security group settings:

  1. Navigate to the EC2 dashboard, select your instance, and click on the Security Group.
  2. Edit inbound rules to allow traffic on port 9999.

Starting Jenkins Server with Corrected Configuration

After adjusting the port and security group settings, attempt to access Jenkins via your web browser:

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

You should see the Jenkins initial setup page if everything is configured correctly.

Troubleshooting and Background Processing

Identifying and Resolving Errors During Jenkins Initialization

If Jenkins fails to start, check the logs for errors:

sudo journalctl -u jenkins

Common issues include incorrect Java versions or insufficient permissions. Address these as needed.

Ensuring Jenkins Runs as a Background Process for Persistent Operation

To ensure Jenkins runs in the background and persists across reboots, enable the Jenkins service:

sudo systemctl enable jenkins

Creating a Script for Jenkins Background Execution

For more control, you can create a custom script to manage Jenkins in the background:

#!/bin/bash

sudo systemctl start jenkins

Make the script executable:

chmod +x start_jenkins.sh

Run this script whenever you need to start Jenkins:

./start_jenkins.sh

Conclusion

By following this guide, you’ve successfully installed Jenkins on an AWS EC2 instance, configured it for custom port usage, and ensured it’s running as a persistent background process. You’re ready to start setting up your CI/CD pipelines and automating your software development processes.

References

Set Up a Jenkins Build Server

Setting up a CI/CD pipeline by integrating Jenkins with AWS CodeBuild and AWS CodeDeploy