DEV Community

Cover image for Jenkins Server setup using Terraform
Gbenga Ojo-Samuel
Gbenga Ojo-Samuel

Posted on • Edited on

Jenkins Server setup using Terraform

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/Samuel7050/jenkins-server-terraform.git

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

Image description

Modules Folder

Image description

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

• keypairs

Image description

• Launch Instance

Image description

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

Image description

Image description

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

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

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

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

Image description

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

Image description

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

Image description

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

Image description

Run terraform init to initialize terraform.

Image description

Run terraform plan to preview the infrastructure to deploy.

Image description

Image description

Run terraform apply to deploy the infrastructure on AWS.

Image description

*Confirm Infrastructure on AWS *

Image description
To access the Jenkins server, copy and paste the output of the website_url on your web browser.

Image description

Image description

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

Image description

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

Image description

Install suggested plugins and configure the admin user credentials.

Image description

Image description

Image description

Image description

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!!!

Top comments (0)