Installing Jenkins on EC2 Instance: A Step-by-Step Guide 2024

4.7/5 - (8 votes)

Installing Jenkins on EC2 Instance: A Step-by-Step Guide 2024 | Setting Up Jenkins on AWS EC2: A Quick Installation Guide 2024


In this article you will learn how to install jenkins on ec2 machine in quickest way possible.

Along with that you will learn to create a jenkins job which has github webhook enabled so that once the commit push to github the job will triggered automatically.

Step 1: Launching an EC2 instance

Here we launching ec2 instance which has user data which automatically installed the git and jenkins inside the ec2 instance. Here are the steps:

  1. After signing to aws console, Go to ec2 dashboard.
  2. In the resources section, select instances(running).
image 45

3. Now select launch instances.

image 1

4. Enter server name, select ami (amazon linux), and instance type(t2.micro).

image 2
image 3

5. Create a key pair to securely connect to our ec2 instance.

image 4
image 5

6. Now select Advanced details and search for the user data option.

image 6

7. In the user data option, write the commands that will install git and Jenkins inside the server when the instance is launched, so we don’t have to manually enter the commands.

#!/bin/bash
sudo yum update -y
sudo yum install git -y
sudo 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-2023.key
sudo yum upgrade
sudo dnf install java-17-amazon-corretto -y
sudo yum install jenkins -y
sudo systemctl enable jenkins
sudo systemctl start jenkins

https://www.jenkins.io/doc/book/installing/linux/

image 7
image 8

8. Once the instance is launched successfully, we need to ssh into it. Copy the example command and run the command in the cmd in the directory where you download the pem file (key pair).

image 9
image 10

9. Now run the sudo bash to run as admin then check if jenkins is running properly or not by usiing this command:

systemctl status jenkins 
image 11

10. Now try to run the Jenkins application on the browser by running ip:8080.

The site can’t be reached because the security group is not permitted to view the 8080 port of that server publically.

We have to add inbound rules that allow this

image 12
image 13
image 14
image 15

Step 2: Configuring Jenkins

Now after installation, need to setup jenkins by unlocking jenkins using administrator password:

cat /var/lib/jenkins/secrets/initialAdminPassword

it will give you the password.

image 16
image 17
image 18

Create first admin user by adding username, password, fullname, and email. After that just continue and the jenkins configuration is done.

image 19
image 20
image 22

Here you will see the jenkins dashboard.

image 23

Step 3: How to use Github Personal Access Token in Jenkins

image 24
image 25
image 26
image 27
image 28
image 29
image 30

Step 4: Test a Jenkins pipeline of sample node application

image 31
image 32
image 33

How to configure Webhook in GitHub and Jenkins for automatic trigger with CICD pipeline?

image 34
image 41
image 36
image 37
image 38

Updating the code to trigger the jenkins job using webhook

image 39
image 40
image 42
image 43
Share On:

Leave a Comment