Provisioning a Jenkins Server on AWS Using Terraform | by Matt Little | Mar, 2022

State of affairs

Your crew has been utilizing a Jenkins server domestically for his or her CI/CD pipeline and now desires to maneuver it into the cloud. You will have been tasked with organising a Jenkins server on AWS.

Necessities

  • The Jenkins server should be deployed on an EC2 occasion
  • The EC2 occasion needs to be accessible through the web on port 8080
  • Solely you must be capable to entry the EC2 occasion through SSH
  • Use Terraform for Infrastructure as Code

How will we accomplish this process with Terraform?

  1. Create the VPC
  2. Create the Web Gateway and connect it to the VPC utilizing a Route Desk
  3. Create a Public Subnet and affiliate it with the Route Desk
  4. Create a Safety Group for the EC2 Occasion
  5. Create a script to automate the set up of Jenkins on the EC2 Occasion
  6. Create the EC2 Occasion and connect an Elastic IP and Key Pair to it
  7. Confirm that the whole lot works

Stipulations

The structure diagram of what we will probably be creating in AWS by this tutorial

First, we’re going to wish to arrange our listing. That is what it’s going to seem like when all mentioned and performed.

terraform-jenkins/
├─ important.tf
├─ outputs.tf
├─ secrets and techniques.tfvars
├─ modules/
│ ├─ compute/
│ │ ├─ important.tf
│ │ ├─ outputs.tf
│ │ ├─ install_jenkins.sh
│ ├─ security_group/
│ │ ├─ important.tf
│ │ ├─ outputs.tf
│ ├─ vpc/
│ │ ├─ important.tf
│ │ ├─ outputs.tf

You’ll be able to run the next command to create the listing construction from above:

mkdir -p terraform-jenkins/modules/compute,security_group,vpc && cd terraform-jenkins && contact important.tf outputs.tf secrets and techniques.tfvars && cd modules/compute && contact important.tf outputs.tf install_jenkins.sh && cd ../security_group && contact important.tf outputs.tf && cd ../vpc && contact important.tf outputs.tf

Go forward and open the terraform-jenkins folder in your favourite IDE. Now let’s begin by filling out our variables and secrets and techniques file.

Variables and Secrets and techniques

Open variables.tf and fill it out with the next code:

Now open secrets and techniques.tfvars and fill it in with the next:

my_ip="0.0.0.0" // substitute 0.0.0.0 together with your IP deal with

Now that the variables and secrets and techniques have been created, we are able to begin engaged on our modules.

Step One — Create the VPC

Open the file ./modules/vpc/important.tf and fill it out with the next code:

Step Two — Create the Web Gateway and connect it to the VPC utilizing a Route Desk

Now that the VPC has been created, we have to give it entry to the web. We’ll be creating an Web Gateway and a Route Desk. Contained in the file ./modules/vpc/important.tf, add the next:

Step Three — Create a Public Subnet and affiliate it with the Route Desk

Now that now we have the Route Desk and Web Gateway created, the VPC has entry to the web. Now, all we have to do is create our public subnet and affiliate it with our public route desk in order that it could actually have entry to the web. Contained in the file ./modules/vpc/important.tf, add the next code:

That’s about it with organising our VPC. We simply have one final thing to do, and that’s to create a number of outputs. Open up the file ./modules/vpc/outputs.tf and add the next code:

Step 4 — Create a Safety Group for the EC2 Occasion

Our VPC has been arrange and configured. Now let’s transfer on to the safety group for our EC2 occasion. Open the file ./modules/security_group/important.tf and add the next code:

That’s it for creating our safety group, now let’s add an output to our ./modules/security_group/outputs.tf file:

Step 5 — Create a Script to Automate the Set up of Jenkins on the EC2 Occasion

Earlier than we begin engaged on the EC2 occasion, let’s create the bash script we’re going to use to automate the set up of Jenkins. We’re going to be attaching this to the EC2 occasion as consumer knowledge, which is able to run on the time of the creation of the EC2 occasion. Open up ./modules/compute/install_jenkins.sh and add the next code:

Step Six— Create the EC2 Occasion and Connect an Elastic IP and Key Pair To It

Earlier than we dive into creating the EC2 occasion and Elastic IP, let’s create our key pair actual fast. Run the next command to create a key pair:

ssh-keygen -t rsa -b 4096 -m pem -f tutorial_kp && mv tutorial_kp.pub modules/compute/tutorial_kp.pub && mv tutorial_kp tutorial_kp.pem && chmod 400 tutorial_kp.pem

Candy. Now that the important thing pair has been created let’s create the EC2 occasion and Elastic IP. Open up ./modules/compute/important.tf and add the next code:

We’re virtually performed! Let’s create an output in our ./modules/compute/outputs.tf file. This output goes to output the general public IP deal with of our EC2 occasion.

Step 7 —Confirm That The whole lot Works

Let’s initialize our terraform mission.

terraform init 

Now let’s run our terraform mission.

terraform apply -var-file="secrets and techniques.tfvars"

When prompted, enter: Sure

If profitable, you must see one thing like this:

Candy! That IP deal with labeled “jenkins_public_ip” is the IP deal with of our Jenkins server. Let’s see if we are able to SSH into our EC2 occasion.

ssh -i tutorial_kp.pem ubuntu@$(terraform output -raw jenkins_public_ip)

And we’re in! Let’s test and see if our consumer knowledge script was operating.

curl http://169.254.169.254/newest/user-data

And certain sufficient, it was! Now let’s have a look at if we are able to entry the Jenkins server from our net browser. Enter the IP deal with in your deal with bar adopted by :8080

And voilà, there it’s! If you wish to seize the administrator password from the EC2 occasion, head again over to the terminal and run the next:

sudo cat /var/lib/jenkins/secrets and techniques/initialAdminPassword

That offers you the administrator password for the Jenkins server and permit you to proceed with the setup course of.

Tear Down

To ensure you don’t incur any pointless costs from AWS, go forward and run the next command:

terraform destroy -var-file="secrets and techniques.tfvars"

When prompted, enter: Sure

More Posts