Gbenga Ojo-Samuel
Posted on March 13, 2024
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.
Modules Folder
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
• keypairs
• Launch Instance
Iam folder- This module defines the IAM policy, IAM Role, IAM policy attachment and IAM Instance profile for the EC2 server.
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.
vpc folder- This module defines a default VPC. A default vpc and default subnet will be created if they are not currently existing.
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.
Local.tf- This file allows us to pass values to all variables as required in the terraform code.
main.tf- This is the main terraform file that will be used for deploying our infrastructures by referencing the modules folder.
terraform.tf- This is terraform file defines the terraform provider, and configures the aws provider.
Building Infrastructure with Terraform codes.
Open the terminal in the root of your project folder.
Run terraform init
to initialize terraform.
Run terraform plan
to preview the infrastructure to deploy.
Run terraform apply
to deploy the infrastructure on AWS.
*Confirm Infrastructure on AWS *
To access the Jenkins server, copy and paste the output of the website_url
on your web browser.
The admin password can be retrieved from the terraform outputs on the terminal.
or from the location on the Jenkins server
/var/lib/jenkins/secrets/initialAdminPassword
Install suggested plugins and configure the admin user credentials.
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!!!
Posted on March 13, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.