Installing Jenkins On AWS EC2 Instance: Simplified Guide For Beginners
Published: 2023-03-14 16:24:42

![Credits: Image via official Amazon EC2 Services documentation](https://cdn-images-1.medium.com/max/2000/1*7GY1d3aejCs4oR52z8TAcw.jpeg)

Hey! Buddy, Since you are here, I want to believe that you already know what cloud and cloud computing are, so I will go straight to a quick introduction of **Amazon’s Web Service(AWS), Elastic Compute Cloud(EC2) instance, and Jenkins .**

### What Is AWS?

**AWS** is an on-demand online platform by AMAZON that provides secured, scalable, cost-effective cloud services.

![](https://cdn-images-1.medium.com/max/2000/1*tYOuChNq6lvU5fvPAygyJw.png)

### What Is EC2 & EC2 Instance?

**EC2** is an on-demand computing service on the AWS cloud platform that allows users to rent virtual computers on which to run their computer applications.

**EC2 Instance** is a virtual server in (EC2)for running applications on the Amazon Web Services (AWS) infrastructure.

### What Is Jenkins?

Jenkins is an open-source CI/CD tool that helps with automation and enables developers to reliably build, test, and deploy their software.

Prerequisite: An AWS account. if you don't have one, you can register [here](https://portal.aws.amazon.com/billing/signup?nc2=h_ct&src=header_signup&redirect_url=https%3A%2F%2Faws.amazon.com%2Fregistration-confirmation#/start/email)**.

## Let's Begin 🤝

### 1. Signing In and Accessing AWS Services

Now log into your AWS account and click on Console Home Page to have access to all AWS services.

### 2. Launch EC2 Instance

Now that you are on the console home page scroll down to see the list of AWS services and click on “**Launch EC2 Machine**”.

![](https://cdn-images-1.medium.com/max/2000/1*ITf9nTq17tTqk85otyCzSQ.png)

### 3. Name & Choose an Amazon Machine Image

* Now it’s time to name/tag your instance, so put any name you want.

* An **Amazon Machine Image (AMI) **is **a template that contains a software configuration** (for example, an operating system, an application server, and applications). From an AMI, you launch an instance, which is a copy of the AMI running as a virtual server in the cloud.

So now that you know what AMI is, go ahead and select **Amazon Linux, **this I strongly recommend because it is free.

![](https://cdn-images-1.medium.com/max/2704/1*uaC9NBKf-vkrjVb3X7RnDA.png)

### 4. Choose Instance Type

Leave it as default and if you must change the settings, make sure to select a free tier eligible, so you don’t get charged.

![](https://cdn-images-1.medium.com/max/2000/1*l6Xfb4zDOusUrRhdQ-5VEA.png)

### 5. Create Key Pair

A key pair, consisting of a public key and a private key, is a set of security credentials that you use to prove your identity when connecting to an Amazon EC2 instance.

So now click on “Create new Key pair”.

![](https://cdn-images-1.medium.com/max/2000/1*f_t2T-c4KjYMwowwWzhnBg.png)

Enter a name for the key pair and click on “create key pair” and it will automatically be downloaded on your computer.

![](https://cdn-images-1.medium.com/max/2000/1*QtbXKq3QeP38PXiyZiQCoQ.png)

### 6. Configuring Network Settings

On the security network settings, Click on 
“edit”.

Then navigate to the “inbound security group rules” page and click on **add security group rule.**

![](https://cdn-images-1.medium.com/max/2000/1*EqBQOXdBy5fI_azcV41lpQ.png)

Now make the same configuration as shown below for your **security group rule 2**, and then click on add security group rule again.

![](https://cdn-images-1.medium.com/max/2000/1*H4YYHQfPRLSqUE7UorAgsw.png)

Lastly, make the same configuration as below for your **Security group rule 3**, and our network security configuration is complete.

![](https://cdn-images-1.medium.com/max/2000/1*-1VhUfMgFPC_IO4eF9NEXg.png)

### 7. Add Storage

Follow the below settings:

![](https://cdn-images-1.medium.com/max/2000/1*YsUUhiabU_n32RPqRaMS-A.png)

### 8. Summary

Now, this is the part you specify how many EC2 Instances you want to launch, I will only select 1 for this tutorial after that **Click on Launch Instance.**

![](https://cdn-images-1.medium.com/max/2000/1*SUC3ucyPlrnfDFrRgqe19g.png)

### 9. Installing Jenkins and Running Jenkins

Now that your instance is running, click on your instance then click on **connect** to access the **AWS **terminal where you will run the below commands.

![](https://cdn-images-1.medium.com/max/2698/1*7DeVAtD0duqWFDwcm5v1rw.png)

Completing the previous steps enables you to download and install Jenkins on AWS. To download and install Jenkins:

 1. Ensure that your software packages are up to date on your instance by using the following command to perform a quick software update:

    [ec2-user ~]$ sudo yum update –y

2. Add the Jenkins repo using the following command:

    [ec2-user ~]$ sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo

3. Import a key file from Jenkins-CI to enable installation from the package:

    [ec2-user ~]$ sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key

Then this

    [ec2-user ~]$ sudo yum upgrade

4. Install Java:

    [ec2-user ~]$ sudo amazon-linux-extras install java-openjdk11 -y

5. Install Jenkins:

    [ec2-user ~]$ sudo yum install jenkins -y

6. Enable the Jenkins service to start at boot:

    [ec2-user ~]$ sudo systemctl enable jenkins

7. Start Jenkins as a service:

    [ec2-user ~]$ sudo systemctl start jenkins

8. Check Jenkins Status:

    [ec2-user ~]$ sudo systemctl status jenkins

### 10. Configuring Jenkins

Now that Jenkins is installed and running on your EC2 Instance.

Connect to <your_server_public_DNS>:8080 from your browser

![](https://cdn-images-1.medium.com/max/2000/1*n1dKgFqzPc2rsxpAS1MKWA.png)

You will be able to access Jenkins through its management interface:

![](https://cdn-images-1.medium.com/max/2478/1*sv2YAtI7qS7aSb9WdHyrIQ.png)

 1. As prompted, enter the password found in **/var/lib/jenkins/secrets/initialAdminPassword**.

Enter the below command in your terminal to display the password in **/var/lib/jenkins/secrets/initialAdminPassword :**

    [ec2-user ~]$ sudo cat /var/lib/jenkins/secrets/initialAdminPassword

2. The Jenkins installation script directs you to the **Customize Jenkins page**. Click **Install suggested plugins**.

3. Once the installation is complete, then **Create First Admin User** will open. Enter your information, and then select **Save and Continue**.

And Boom! our Jenkins is now ready!

![JENKINS DASHBOARD](https://cdn-images-1.medium.com/max/5760/1*BFqeBiekt0YdM1ZE03BiOg.jpeg)

### 11. Creating A Simple Build Project On Jenkins

### **•Create a New Freestyle Project**

 1. Click on the **New Item** link on the left-hand side of the Jenkins dashboard.

![](https://cdn-images-1.medium.com/max/2000/1*eCvAEB5i2qOVVelbkCsNEw.png)

2. Name the new project in the **Enter an item name** field and select the **Freestyle project** type. Click **OK** to continue.

![](https://cdn-images-1.medium.com/max/2000/1*sTLxxivLNJf7vOlf2SJV4g.png)

3. Under the *General* tab, add a project description that explains what your project is about in the **Description** field.

### **•Add A New Build Step**

 1. Scroll down to the *Build* section Open the **Add build step** drop-down menu and select **Execute Windows batch command**.

![](https://cdn-images-1.medium.com/max/2000/1*y3u0c5By7xns6O26P3au2A.png)

2. Enter the commands you want to execute in the **Command** field. For this tutorial, I will be using a simple Bash Script that generates Password:

    #! /usr/bin/bash

    #NOTE: THIS IS A SIMPLE PASSWORD GENERATOR USING BASH SCRIPTING

    echo "THIS IS A SIMPLE PASSWORD GENERATOR"

    echo "Now enter the length of the password you want to generate:"

    read PASS_LENGTH

    for p in $(seq 1);

    do

    openssl rand -base64 48 | cut -c1-$PASS_LENGTH

    done

3. Click the **Save** button to save changes to the project.

### •Build The Project

 1. Click the **Build Now** link on the left-hand side of the new project page.

![](https://cdn-images-1.medium.com/max/2000/1*Vp9xbK6i59yaDmpDLzUsnQ.png)

2. Click the link to the latest project build in the *Build History* section.

![](https://cdn-images-1.medium.com/max/2000/1*Jz95AAdDsDKaAsZl4NBUrQ.png)

3. Click the **Console Output** link on the left-hand side to display the output for the commands you entered.

![](https://cdn-images-1.medium.com/max/2000/1*FG3upY48_XkEJh1B4sz9Sg.png)

4. The console output indicates that Jenkins is successfully executing the commands.

![](https://cdn-images-1.medium.com/max/2000/1*ViKdhhKnjxWjmNaoLxfcEA.png)

***After this going through this tutorial, you should be able to launch an EC2 Instance, Install Jenkins on it and run your first Jenkins freestyle project.***

***Please leave a clap if you find this helpful.***