Automated Infrastructure (AWS) Setup Using Terraform and Jenkins (Launch EC2 and VPC)

In this blog we will discuss how to execute the Terraform code using Jenkins and set up AWS infrastructure such as EC2 and VPC.

For those of you are unfamiliar with Jenkins, it is an open-source continuous integration and continuous development automation tool that allows us to implement CI/CD workflows, called pipelines.

Getting to Know the Architecture

What is Terraform? – Terraform is the infrastructure as code delivered by HashiCorp. It is a tool for building, changing, and managing infrastructure in a safe repeatable way.

What is Jenkins? –  An open-source continuous integration/continuous delivery and deployment (CI/CD) automation software DevOps tool written in the Java programming language. It is used to implement CI/CD workflows, called pipelines.

What is Infrastructure as Code? – It is the process of managing infrastructure in a file, or files, rather than manually configuring resources in a user interface.

 

Advantages of Continuous Integration/Continuous Deployment –

Small code changes are easier and less consequential.
Insulating faults is easier and faster.
Testability is enhanced through smaller, specific changes.

 

Terraform consists of three stages of workflow:

Write: You set resources, which can be split between several cloud providers and services.
Plan: Terraform creates a workplan for your existing infrastructure and configuration that describes the infrastructure it will create and update.
Apply: Terraform completes all operations in the correct sequence.

 

In this Article, we will cover the basic functions of Terraform to create infrastructure on AWS.

 

Launch One Linux Machine and Install Jenkins. 

Access the Jenkins Web portal
Access the URL : http://34.201.1.206/http://:8080

The Admin password is created and stored in the log file. To access the password you will need to run the below command.

# cat  /var/lib/jenkins/secrets/initialAdminPasswordThen, Customize JenkinsAfter That, Create First Admin UserClick on Save and Continue…

Install the Terraform Plugin in Jenkins

In the Jenkins console, go to Manage Jenkins > Manage Plugins > Available > and search “Terraform”.

Configure Terraform

You will need to manually set up Terraform on the same sever as Jenkins using the below commands.

In Manage Jenkins > Global Tool Config > Terraform
Add Terraform.
Uncheck the “Install Automatically” check box.
Name: Terraform
Install Directory: /usr/local/bin/

After getting Terraform set up on the Jenkins’ server, you will need to install Git on your Jenkins VM and write Terraform code on .tf file

Push your .tf file on GitHub Git repository
For this project, I have already created the demo repository. Repository Link: https://github.com/suraj11198/Terraform-Blog.git 

Integrate Jenkins with Terraform and our Git Hub Repository

We need to create a new project to run Terraform using Jenkins.
In Jenkins got to New Item and enter and item name and create Pipeline.
Now, we will write the script for the GitHub and Terraform job. Here we can use the Jenkins syntax generator to write the script.

pipeline {

agent any

stages {

stage(‘Checkout’) {

steps {

checkout([$class: ‘GitSCM’, branches: [[name: ‘*/main’]], extensions: [], userRemoteConfigs: [[url: ‘https://github.com/suraj11198/Terraform-Blog.git‘]]])

}

}

stage (“terraform init”) {

steps {

sh (‘terraform init’)

}

}

stage (“terraform Action”) {

steps {

echo “Terraform action is –> ${action}”

sh (‘terraform ${action} –auto-approve’)

}

}

}

}

 

Using the Previous Steps, We Should Have Successfully Built Our Job

Our EC2 Instance and VPC are Created, and the Same VPC is Attached to Our EC2 Instance

 

Summary:

Using Terraform, we built an EC2 instance and VPC on AWS using remote control.

We have touched on the basics of Terraform and Jenkins capabilities. It has several functionalities for the construction, modification, and versioning of the infrastructure.

 

How Can Perficient Help You?

Perficient is a certified Amazon Web Services partner with more than 10 years of experience delivering enterprise-level applications and expertise in cloud platform solutions, contact center, application modernization, migrations, data analytics, mobile, developer and management tools, IoT, serverless, security, and more. Paired with our industry-leading strategy and team, Perficient is equipped to help enterprises tackle the toughest challenges and get the most out of their implementations and integrations.

Learn more about our AWS practice and get in touch with our team here!