How to Install Jenkins on AWS EC2 Instance
- Mohammad Abu Mattar
- Cloud Computing , DevOps
- 06 Dec, 2022
- 06 Mins read
Introduction
In this post, I will show you how to Create an EC2 Instance on AWS and install Jenkins on it.
Prerequisites
- AWS CLI installed and configured
- IAM user with the following permissions:
- AmazonVPCFullAccess
- AmazonEC2FullAccess
Create a VPC
Step 1: Create a VPC
To create a VPC, run the following command:
Explanation:
AWS_VPC
is a variable that holds the VPC ID.--cidr-block
The IPv4 network range for the VPC, in CIDR notation.--query
The JMESPath query that is used to extract data from the output.--output
The output format of the command.
Step 2: Modify your custom VPC and enable DNS hostname support, and DNS support
To modify your custom VPC and enable DNS hostname support, and DNS support, run the following command:
Explanation:
--enable-dns-hostnames
Indicates whether the instances launched in the VPC get DNS hostnames.--enable-dns-support
Indicates whether DNS resolution is supported for the VPC.
Step 3: Create a Public Subnet
To create a public subnet, run the following command:
Explanation:
AWS_PUBLIC_SUBNET
is a variable that holds the public subnet ID.--vpc-id
The ID of the VPC.--cidr-block
The IPv4 network range for the subnet, in CIDR notation.--query
The JMESPath query that is used to extract data from the output.--output
The output format of the command.
Step 4: Enable Auto-assign Public IP on the subnet
To enable auto-assign public IP on the subnet, run the following command:
Explanation:
--subnet-id
The ID of the subnet.--map-public-ip-on-launch
Indicates whether to assign a public IPv4 address to instances launched in the subnet.
Step 5: Create an Internet Gateway
To create an internet gateway, run the following command:
Explanation:
AWS_INTERNET_GATEWAY
is a variable that holds the internet gateway ID.--query
The JMESPath query that is used to extract data from the output.--output
The output format of the command.
Step 6: Attach the Internet Gateway to the VPC
To attach the internet gateway to the VPC, run the following command:
Explanation:
--internet-gateway-id
The ID of the internet gateway.--vpc-id
The ID of the VPC.
Step 7: Create a Route Table
To create a route table, run the following command:
Explanation:
AWS_ROUTE_TABLE
is a variable that holds the route table ID.--vpc-id
The ID of the VPC.--query
The JMESPath query that is used to extract data from the output.--output
The output format of the command.
Step 8: Create a custom route table association
To create a custom route table association, run the following command:
Explanation:
--subnet-id
The ID of the subnet.--route-table-id
The ID of the route table.
Step 9: Associate the subnet with route table, making it a public subnet
To associate the subnet with route table, making it a public subnet, run the following command:
Explanation:
--route-table-id
The ID of the route table.--destination-cidr-block
The IPv4 CIDR address block used for the destination match.--gateway-id
The ID of an internet gateway or virtual private gateway attached to your VPC.
Step 10: Create a Security Group
To create a security group, run the following command:
Explanation:
AWS_SECURITY_GROUP
is a variable that holds the security group ID.--group-name
The name of the security group.--description
A description for the security group.--vpc-id
The ID of the VPC.--query
The JMESPath query that is used to extract data from the output.--output
The output format of the command.
Step 11: Add a rule to the security group
To add a rule to the security group, run the following command:
Explanation:
--group-id
The ID of the security group.--protocol
The IP protocol name or number.--port
The port number or range of port numbers.--cidr
The IPv4 CIDR range.--output
The output format of the command.
Create an EC2 Instance
Step 1: Get the latest AMI ID
To get the latest AMI ID, run the following command:
Explanation:
AWS_AMI
is a variable that holds the AMI ID.--owners
The AWS account ID of the owner.--filters
The filters.--query
The JMESPath query that is used to extract data from the output.--output
The output format of the command.
Step 2: Create a Key Pair
To create a key pair, run the following command:
Explanation:
--key-name
The name of the key pair.--query
The JMESPath query that is used to extract data from the output.--output
The output format of the command.>
The output is redirected to a file.aws-key-pair.pem
The name of the file.chmod 400 aws-key-pair.pem
Change the permission of the key pair, so that only the owner can read and write.
Step 3: Create a User Data Script
To create a user data script, run the following command:
Explanation:
cat << EOF > user-data.sh
The output is redirected to a file.user-data.sh
The name of the file.#!/bin/bash
The shebang line.sudo yum update -y
Update the system.sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
Add the Jenkins repo.sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
Import the Jenkins repo key.sudo yum upgrade
Upgrade the system.sudo amazon-linux-extras install java-openjdk11 -y
Install Java.sudo yum install jenkins -y
Install Jenkins.sudo systemctl start jenkins
Start Jenkins.sudo systemctl enable jenkins
Enable Jenkins.
Step 4: Create an EC2 Instance
To create an EC2 instance, run the following command:
Explanation:
AWS_INSTANCE
is a variable that holds the instance ID.--image-id
The ID of the AMI.--instance-type
The instance type.--key-name
The name of the key pair.--monitoring
The monitoring for the instance.--security-group-ids
The IDs of the security groups.--subnet-id
The ID of the subnet.--user-data
The user data to provide when launching the instance.--query
The JMESPath query that is used to extract data from the output.--output
The output format of the command.
Step 5: Create an Elastic IP
To create an Elastic IP, run the following command:
Explanation:
AWS_ELASTIC_IP
is a variable that holds the Elastic IP ID.--domain
The domain name.--query
The JMESPath query that is used to extract data from the output.--output
The output format of the command.
Step 6: Associate the Elastic IP with the EC2 Instance
To associate the Elastic IP with the EC2 instance, run the following command:
Explanation:
--allocation-id
The allocation ID.--instance-id
The ID of the instance.
Connect to the Jenkins Server, and Setup Jenkins
Step 1: Connect to the Jenkins Server
To connect to the Jenkins server, run the following command:
Explanation:
AWS_PUBLIC_IP
is a variable that holds the public IP address of the Jenkins server.ssh -i aws-key-pair.pem ec2-user@$AWS_PUBLIC_IP
Connect to the Jenkins server.
Step 2: Setup Jenkins
To setup Jenkins, run the following command:
Explanation:
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Get the initial admin password.
Step 3: Configuring Jenkins
To configure Jenkins:
- Go to
http://<public-ip-address>:8080/
.
- Enter the initial admin password.
- Select the recommended plugins.
- Create the first admin user.
- Jenkins is ready.
Conclusion
In this tutorial, you learned how to install Jenkins on an AWS EC2 instance.