DEV Community

Olumoko Moses
Olumoko Moses

Posted on • Originally published at Medium

Installing Jenkins On AWS EC2 Instance: Simplified Guide For Beginners

Credits: Image via official Amazon EC2 Services documentation

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.

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**.

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”.

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.

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.

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”.

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

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.

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

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

7. Add Storage

Follow the below settings:

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.

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.

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

    1. 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

    1. 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
Enter fullscreen mode Exit fullscreen mode
  1. Install Java:

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

  2. Install Jenkins:

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

  3. Enable the Jenkins service to start at boot:

    [ec2-user ~]$ sudo systemctl enable jenkins

  4. Start Jenkins as a service:

    [ec2-user ~]$ sudo systemctl start jenkins

  5. 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 :8080 from your browser

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

  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
Enter fullscreen mode Exit fullscreen mode
  1. The Jenkins installation script directs you to the Customize Jenkins page. Click Install suggested plugins.

  2. 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

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.

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

  1. 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.

  1. 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

  2. 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.

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

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

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

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.

Top comments (0)