Skip to main content

Amazon Web Services

Integrating Terraform with Jenkins (CI/CD)

Pexels Kevin Ku 577585

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

2

1

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:

  1. Write: You set resources, which can be split between several cloud providers and services.
  2. Plan: Terraform creates a workplan for your existing infrastructure and configuration that describes the infrastructure it will create and update.
  3. 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.

 

  1. Launch One Linux Machine and Install Jenkins. 

3

    • 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…

4

  1. Install the Terraform Plugin in Jenkins
    • In the Jenkins console, go to Manage Jenkins > Manage Plugins > Available > and search “Terraform”.

5

  1. 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/

6

    • 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

7

  1. 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.

8

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’)

}

}

}

}

 

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

9

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

10

11

 

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!

Thoughts on “Integrating Terraform with Jenkins (CI/CD)”

  1. Pallavi Choudhari

    All information shared by you is very helpful for new learner..well written Suraj sir keep sharing the knowledge.its best KT.

  2. Good article Suraj, explains how its done in an easy to follow way.

    A question regarding the Jenkins Terraform plugin, it seems this is now unmaintained. Not having had a release since Feb 2020. Do you know if there are other plugins that can be used?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Suraj Shende

Suraj Shende is a Technical Consultant at Perficient Nagpur, having 4 years of experience in cloud services. Suraj is a great team player and focuses on cloud and DevOps technologies.

More from this Author

Follow Us