How does Jenkins integrate with AWS EC2?

  1. Home
  2. AWS
  3. How does Jenkins integrate with AWS EC2?
How does Jenkins integrates with AWS EC2?

Jenkins is a popular open-source automation tool that is widely used for building, testing, and deploying software applications. Amazon Web Services (AWS) EC2, on the other hand, is a cloud-based infrastructure service that provides scalable computing resources for deploying applications. Integrating Jenkins with AWS EC2 can help streamline the software development process and improve overall efficiency.

In this blog, we will explore how Jenkins integrates with AWS EC2 and the benefits of this integration. We will also provide a step-by-step guide on how to set up Jenkins to work with AWS EC2.

Overview of Jenkins and AWS EC2

Jenkins is an open-source automation tool that is widely used for building, testing, and deploying software applications. It allows developers to automate repetitive tasks and streamline the software development process.

AWS EC2, on the other hand, is a cloud-based infrastructure service that provides scalable computing resources for deploying applications. It allows developers to quickly and easily spin up virtual machines in the cloud, making it an ideal platform for running Jenkins.

Integrating Jenkins with AWS EC2 is important for several reasons. Firstly, it provides developers with a scalable and flexible infrastructure that can be easily adapted to the needs of the project. Secondly, it allows developers to take advantage of the cost-effectiveness of the cloud, by only paying for the resources they use. Finally, it increases automation and efficiency by allowing developers to automate the deployment process.

Benefits of integrating Jenkins with AWS EC2:

Integrating Jenkins with AWS EC2 offers several benefits for developers and organizations, including:

  • Scalability and flexibility: AWS EC2 provides developers with a scalable and flexible infrastructure that can easily be adapted to the needs of the project. Jenkins running on EC2 can dynamically provision additional resources as needed, ensuring that the application can handle changes in traffic and demand.
  • Cost-effectiveness: With AWS EC2, developers only pay for the resources they use, making it a cost-effective platform for running Jenkins. This eliminates the need to invest in expensive hardware and allows organizations to reduce their infrastructure costs.
  • Increased automation and efficiency: Jenkins automates the deployment process, which reduces the time and effort required for manual deployment. Running Jenkins on AWS EC2 further increases automation and efficiency by allowing developers to automatically provision resources for testing, building, and deploying applications.
  • Streamlined software development process: Integrating Jenkins with AWS EC2 allows developers to easily spin up virtual machines in the cloud and run tests, build applications, and deploy them quickly. This helps streamline the software development process, reducing time-to-market and improving overall efficiency.

How does Jenkins work with Amazon EC2?

Jenkins is a popular open-source automation tool that can be used to automate a variety of tasks, including building and deploying software. When used with Amazon EC2, Jenkins can be used to automate the creation and management of EC2 instances, making it easier for organizations to manage their infrastructure.

Here’s a basic overview of how Jenkins works with Amazon EC2:

  • Launch EC2 Instances: The first step is to launch one or more EC2 instances, which will be used to run Jenkins.
  • Install Jenkins: Next, Jenkins must be installed on the EC2 instance. This can be done manually, or by using an EC2 launch script that automatically installs and configures Jenkins.
  • Configure Jenkins: After Jenkins is installed, it must be configured to connect to Amazon EC2. This includes setting up the necessary credentials and plugins to allow Jenkins to interact with EC2.
  • Create a Job: Once Jenkins is configured, a job must be created to automate the desired task. This might include tasks such as building a software project, deploying an application to EC2 instances, or performing regular maintenance tasks.
  • Trigger the Job: The job is triggered, either manually or on a schedule, and Jenkins connects to EC2 to perform the desired task. This might involve creating new EC2 instances, scaling existing instances, or performing other tasks as defined in the job.
  • Monitor the Job: Finally, Jenkins provides a number of tools and interfaces for monitoring the progress of the job and reviewing the results. This might include log files, status reports, and other tools that help organizations monitor and manage their automation tasks.

The next step is to decide whether you want to install the software yourself or use an image that already has the Jenkins stack configured. The latter is by far the easier option. In this post, we will know how Jenkins integrates with AWS EC2.

jenkins
Jenkins Requirements

Jenkins requires only Java to function on your machine. You’ll need to install Java and another way to run Jenkins via Docker. You can also review the prerequisites section on the Installing Jenkins page.

Ways to Integrate

Jenkins can integrate with AWS EC2 in several ways, including:

  • AWS EC2 Plugin: Jenkins has a built-in AWS EC2 Plugin that allows you to launch EC2 instances as build agents on-demand. This plugin enables you to configure and manage EC2 instances directly from Jenkins, including launching new instances, terminating idle ones, and provisioning agents with required software and configurations.
  • CloudBees AWS CLI Plugin: This plugin enables you to run AWS CLI commands from Jenkins, allowing you to automate various AWS tasks, such as creating or deleting EC2 instances, scaling Auto Scaling groups, or managing S3 buckets.
  • AWS CodeDeploy Plugin: This plugin allows you to deploy applications to EC2 instances or Auto Scaling groups using AWS CodeDeploy, a fully managed deployment service that automates software deployments to a variety of compute services, including EC2, Lambda, and ECS.
  • Jenkins Pipeline: Jenkins Pipeline is a powerful feature that enables you to create complex, multi-step workflows using code. You can use the AWS SDK for Java or Python within your Pipeline script to interact with AWS services, including EC2, S3, and DynamoDB.

By integrating Jenkins with AWS EC2, you can leverage the scalability, flexibility, and automation capabilities of the AWS cloud to improve your Continuous Integration and Continuous Deployment (CI/CD) pipelines, and accelerate your software delivery.

Steps to Integrate

Create an AWS account, an admin user, and log in using that user. Once on the dashboard, select the EC2 instance as shown below.

When you select EC2 from the compute section, you will be taken to the following page, where you can select Launch Instance as shown below.

You can look for CentOS and select it as shown below. You can look for any other Linux distribution you want.

On the following page, you can select an instance type. You can select the free tier eligible one from the list below.

After going through all of the steps, such as attaching the EBS volume, etc., you can finally press the launch button.

To connect to this instance from the CLI, you must have an ec2 key pair. You should download it before pressing the start button.

After some time, you can see it running as shown below.

Connect to Instance Using SSH

There are numerous ways to connect to the instance, which you can see by clicking on the connect button, as shown below.

You can choose SSH and copy the necessary command.

You can log in to that instance after running the command in the terminal. The IP address and instance name are visible.

Install Jenkins on EC2 Instance

Let’s proceed to the next page and follow the instructions to install Jenkins on the EC2 instance.

https://www.jenkins.io/doc/book/installing/linux/#red-hat-centos

First, run the following command to install wget.

yum install -y wget// then follow these commands
sudo wget -O /etc/yum.repos.d/jenkins.repo \
https://pkg.jenkins.io/redhat/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat/jenkins.io.key
sudo yum upgrade
sudo yum install epel-release java-11-openjdk-devel
sudo yum install jenkins
sudo systemctl daemon-reload

The Jenkins can be started with the following command.

sudo systemctl start jenkins

The Jenkins status can be checked using the following commands.

systemctl status jenkins

Access Jenkins From the Internet

Jenkins uses port 8080 by default when it is installed and started. You can test it using the commands below.

netstat -ntlp
systemctl status jenkins
ps -ef | grep jenkins

Because the ports are not open, if you use the EC2 instance’s public DNS and connect to it via port 8080, you will not see anything on the browser.

As you can see, that address is inaccessible.

Let us now include the inbound rule in the security group. The rule is open to all traffic here. You can edit inbound rules in the security group as shown below.

Add the following rule:

When you click the Save rules button, the newly added rule will appear under the security tab.

Now that the rule has been added, you can access Jenkins from the browser.

Setup Jenkins

To begin, you must unlock Jenkins by retrieving the password from /var/lib/jenkins/secrets/initialAdminPassword. You can get it from the above path if you are already logged in via SSH.

In the next step, you can choose to install the suggested plugins.

In the following step, you must create the first admin user.

Check that your Jenkins URL is the same.

After this step, your Jenkins instance is complete.

The Jenkins Dashboard is shown below.

Conclusion

This was a step-by-step tutorial for Jenkins to integrate with AWS EC2. Jenkins is one of the most common tools in any company’s DevOps strategy. Pipeline as code is one of the most in-demand skills to learn if you are serious about DevOps. Jenkins requires only Java to function on your machine. You’ll need to install Java and another way to run Jenkins via Docker.

AWS Certified Advanced Networking Specialty free practice test: Jenkins integrate with AWS
Menu