Set Up & Install Jenkins Server on AWS EC2 Using Terraform
Muhammad Rashid
Entrepreneur | Software Developer | AWS DevOps Guru | Python, Django, Backend Developer | Tech Writer - Empowering Startups to Build Exceptional Web and Mobile Apps
Setting up a Jenkins server on AWS EC2 using Terraform is an efficient way to automate your CI/CD pipeline infrastructure. This guide will walk you through creating necessary AWS resources such as an S3 bucket, DynamoDB table, IAM user, and EC2 instance, and deploying Jenkins server step by step.
Steps:
1. IAM User Setup
Create an IAM user with the necessary permissions for EKS, S3, DynamoDB, ECR, and EC2.
On AWS Console, search I am, and click Users.
Next, click Create User
Give a username to your iam User.
Attach administrative access for testing; for your company project, assign limited policies, and click Create user.
Now you can create an access key
Click Security Credentials, and click on create access key.
Click Command Line Interface (CLI)
Download the file for later use of the secret key and access key.
We have done now with IAM user creation.
2. Terraform & AWS CLI Installation
To install Terraform, You can visit Terraform official website.
To install AWS CLI, You can Visit AWS installtion page .
After installation, you can verify installation with these two commands
terraform --version
aws --version
Then configure aws CLI using the given blow commands, Use Access and secret key from the CSV file you download in the IAM User step.
aws configure
3. S3 Bucket and DynamoDB Table
As S3 Bucket and Dynamo DB are required for Terraform's backend state, you can create them by watching my video.
S3 bucket can be any, give any unique name to it.
lock-files name will be DynamoDB table, and the partition key will be LockID.
4. Jenkins EC2 Server Setup with Terraform
Clone the git repo .
git clone https://github.com/codewithmuh/three-tier-devsecops-project.git
Go to the folder jenkins-server-terraform in the terminal.
Just make sure to make changes in backend.tf file. Update the name of the s3 bucket and dynamo db name.
Next, we have to run the command to create .pem file for our Jenkins instance. will be used to ssh the server.
aws ec2 create-key-pair --key-name devsecops-project --query "KeyMaterial" --output text > devsecops-project.pem
Now run this command to create a Jenkins server.
terraform init
terraform validate
terraform apply
Now ssh to your server :
Make sure you run these commands from the folder where your pem file exist.
chmod 400 "devsecops-project.pem"
ssh -i "devsecops-project.pem" [email protected]
configure aws on the Jenkins server(EC2 Instance). As you did for your local machine.
aws configure
5. Installing Plugins on Jenkins
Open the EC2-IP:8080 in the browser.
You can get the password of Jenkins
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Install recommended plugins.
This is how you can easily setup the Jenkins server on AWS EC2 instance with Terraform.
if you enjoyed this article, be sure to: