Jenkins on AWS EC2

Victor Yeo
3 min readMar 2, 2023

--

This article describes the steps to install and run Jenkins on AWS EC2 instance, as well as setup a CI/CD pipeline on Jenkins.

  1. Create EC2 security group and keypair. For ssh access to EC2, use *.pem keypair format.
  2. Launch an EC2 instance, with Amazon Linux (AMI), choose t2.micro instance type and 8GB disk size, select the security group and keypair created in step 1.
  3. SSH to the EC2 instance
ssh -i <keypair_name>.pem ec2-user@<ec2_public_ip_address>

4. After gaining access to the EC2 instance, we download and install Jenkins

Firstly, update and add Jenkins repo

$ sudo yum update –y
$ sudo wget -O /etc/yum.repos.d/jenkins.repo \
https://pkg.jenkins.io/redhat-stable/jenkins.repo

and import key file from Jenkins repo

$ sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
$ sudo yum upgrade

Install java and Jenkins

$ sudo amazon-linux-extras install java-openjdk11 -y
$ sudo yum install jenkins -y

Use systemctl to start Jenkins service

sudo systemctl enable jenkins
sudo systemctl start jenkins
sudo systemctl status jenkins

We get the initial admin password of Jenkins

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

5. Connect to Jenkins on EC2 using browser

Connect to http://<ec2_public_ip_addr>:8080 from the browser, enter the initial admin password.

6. Create the first admin user

7. Configuring Jenkins

Firstly, in the Jenkins dashboard, we install Amazon EC2 plugin

Next, we configure the EC2 cloud

For the Amazon EC2 Credentials, we need to create an IAM access key

In AWS IAM screen, the IAM access key must be of the type “Application running on AWS compute service”.

For the key pair (to access Jenkins installed on EC2), choose SSH

For the private key, we copy from the .pem, the private key generated in AWS EC2. We must copy the private key, inclusive of everything in the .pem file.

-----BEGIN RSA PRIVATE KEY-----
.........
.........
.........
-----END RSA PRIVATE KEY-----

We enter the private key to the “Enter directly” text box.

Then, click the save button.

8. Configuring pipeline with github

To start the CI/CD process, create a new pipeline

Configure the pipeline to monitor a github repo.

To start the pipeline manually, click the “Build Now” text.

To schedule a pipeline to be run periodically, configure the Build Triggers.

Appendix

This exercise is carried out on Jenkins versio 2.375.3.

The end.

--

--