Jenkins Server setup using Terraform
Tags: webdev, devops, git, aws
Published: 2024-03-13 11:33:00

In this project, we are going to setup a Jenkin server using Terraform script. This Terraform script will spin up an EC2 instance server on AWS with the required tools to run and manage a Jenkins server.
Setting up a Jenkins server on AWS EC2 using Terraform requires a few prerequisites.
• **AWS Account**: You need an AWS account to create and manage resources like EC2 instances.
• **Terraform Installed**: Install Terraform on your local machine to run Terraform commands. Ensure its configured properly and accessible from the command line.
• **AWS CLI** : Having the AWS CLI installed and configured can be helpful for managing AWS resources and troubleshooting.

The Terraform codes for this project can be found in the link below. 
https://github.com/7hundredtech/jenkins-server-terrafom.git

**Terraform Codes Review**
The Terraform codes consist of the **modules folder**, **script folder**, **main.tf**, **local.tf** and **terraform.tf** files.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fl8b00dvnli28b7hswkn.png)

**Modules Folder**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ozlt1qqyfj4749jsqpbl.png)

**ec2 folder** – This folder contains the _main.tf_, _output.tf_ and _variable.tf_ files that defines the ec2 resource and its dependencies. This module defines the following -
**•  security group**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ckno6auv1mosjjscuwx7.png)

**• keypairs**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3uyb81nm17jkm1i8c7l1.png)

**• Launch Instance**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b9dt9uo4lo1ohun1b2fv.png)


**Iam folder**- This module defines the IAM policy, IAM Role, IAM policy attachment and IAM Instance profile for the EC2 server.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1zym560942nwy82rw46e.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/068cahm1jpp0v750xb5c.png)

**ssh_connection folder**- This module defines a null resource that will enable ssh connection into our EC2 instance, copy script.sh file from the local machine into the EC2 instance, set permissions and  execute the shell script in the script folder.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7rb735da84vw1cjwp1d7.png)

**vpc folder**- This module defines a default VPC. A default vpc and default subnet will be created if they are not currently existing.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lpapkpyxlaqhqvmhxoiy.png)

**Script folder**- This folder contains the script.sh file. This script contains the required tools  that will be installed on the Jenkins server at launch.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kizbqvy73swjipv6hea2.png)

**Local.tf**- This file allows us to pass values to all variables as required in the terraform code.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ssia6ymk1xf03ngkjs5w.png)

**main.tf**- This is the main terraform file that will be used for deploying our infrastructures by referencing the modules folder.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d7fuodjeozlz90zj2b5k.png)

**terraform.tf**- This is terraform file defines the terraform provider, and configures the aws provider.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wgy166fd1qf97hax536j.png)

**Building  Infrastructure with Terraform codes.**
Open the terminal in the root of your project folder.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3xi7h9atji74ktaftwkh.png)

Run `terraform init` to initialize terraform.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7imnrhvf15veb9x3nbgh.png)

Run `terraform plan` to preview the infrastructure to deploy.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fn2gywztbe89n6ogt7xh.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hqp0s84dvrp4qdlk5aa4.png)

Run `terraform apply` to deploy the infrastructure on AWS.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6vtxaq6inejqagwja4af.png)

**Confirm Infrastructure on AWS **

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cp7cfissujsbmksljenp.png)
To access the Jenkins server, copy and paste the output of the  `website_url`  on your web browser.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1oj53o7oheigtiab11l1.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/culqpbyz4wina1cddojf.png)

The admin password can be retrieved from the terraform outputs on the terminal. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/euz4ddyzcs75xdrqekr1.png)

or from the location on the Jenkins server
/var/lib/jenkins/secrets/initialAdminPassword


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l3fd64kw9kriqxnq6va3.png)

 Install suggested plugins and configure the admin user credentials.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m7gaxke2otz88pdyqkd2.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jo8sijyc58m9as2d5g47.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wmeqjl0i47ypvrmyhacz.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6kgi4wy0lkfqpp9fyic6.png)

At this point, we have successfully deployed our Jenkins server on an EC2 instance, and we are ready to create our first CICD pipeline.
In the next project, we will use this Jenkins server to create a CICD pipeline that pulls code from git , build, and deploy applications on AWS.  
**Watch this space!!!**

**Clean Up**: To avoid bills from AWS, you can  destroy the infrastructure by running `terraform destroy` command.
 I will be using this server for subsequent project; hence, I will just stop the instance from the AWS console and start it anytime I need to use it.
Thanks for reading!!!