How to run Terraform from Jenkins

jenkins logo
Reading Time: 4 minutes

In this blog, We will learn how to run terraform using Jenkins job. First, we need to know what is Jenkins?? Basically, Jenkins is a CI/CD tool. Jenkins is an open-source that is written in Java. Continuous Integration & Continuous Delivery are integral parts of DevOps. Therefore Jenkins is quite a famous tool. As they are used for integrating multiple stages of the methodology. We can see many CI/CD tools in the market but Jenkins is the most popular tool which is a JAVA-based tool. 

Now, What is terraform ?? Terraform is an open-source infrastructure as a code software tool. Terraform is ow by HashiCorp. It provides a consistent CLI workflow to manage hundreds of cloud services. Terraform codifies cloud APIs into declarative human-readable configuration files. Terraform can manage low-level components like compute, storage, and networking resources, as well as high-level components like DNS entries and SaaS features. The Terraform community has already written more than 1600 providers to manage hundreds of different types of resources and services, and this number continues to increases. You can find all available providers on the Terraform Registry, including Google Cloud Platform (GCP), Amazon Web Services (AWS), Azure, and many more.

Terraform consists of three stages of workflow:

  • Write: You define resources, which may be across multiple cloud providers and services.
  • Plan: Terraform creates an execution plan for your existing infrastructure and your configuration which describe the infrastructure it will create, update, or destroy.
  • Apply:  Terraform perform all operations in correct order.

How to configure Jenkins with Terraform

First, We need to install Jenkins and Terraform on the host system or server. The system can be any local system or AWS instance or others. In this blog, we will create a VPC on AWS using terraform and Jenkins. You can get the code at Github. That GitHub repository consist below code :

#provider.tf

provider "aws"{
region = "us-east-1"
access_key = "<access_key>"
secret_key = "<secret_key>"
}
 #vpc.tf

resource "aws_vpc" "main" {
  cidr_block       = "10.0.0.0/16"
  instance_tenancy = "default"

  tags = {
    Name = "VPC-demo"
    Purpose = "terrafrom using Jenkins"
  }
}

Now you can follow the below steps:

Step-1

After the installation, We can access the Jenkins Using an IP address or localhost with port 8080. Port 8080 is the default port for Jenkins.

Step-2

Now, We should install the Terraform plugin on Jenkins which makes Jenkins interact with terraform. Follow the below steps to install the Terraform plugin:

Dashboard >> Manage Jenkins >> Manage Plugin

Now search the terraform in the search bar.

Now we need to configure the global tool for terraform. In the install directory, we need to pass the path of terraform where we have installed terraform and save it. Follow the below step to configure:

Dashboard >> Manage jenkins >> Global Tool Configuration

Step-3

Now create a job to run the pipeline. We just need to select the pipeline option and ok to create a pipeline. Follow the below steps:

Dashboard >> new Item >> give name and create

Now we will leave all options as default and in the pipeline tab, we will write the script for running the terraform. We have one more option to generate pipeline syntax. Therefore we will use that option. We will select Declarative Directive Generator and then select terraform to generate pipeline. Copy that script into the pipeline.

Now, We will write the script for GitHub or we can use the Jenkins syntax generator.

The complete script will look like below:

pipeline {
    agent any
    tools {
       terraform 'terraform'
    }
    stages {
        stage('Git checkout') {
           steps{
                git branch: 'main', credentialsId: 'Github', url: 'https://github.com/muzakkirsaifi123/terraform_demo'
            }
        }
        stage('terraform format check') {
            steps{
                sh 'terraform fmt'
            }
        }
        stage('terraform Init') {
            steps{
                sh 'terraform init'
            }
        }
        stage('terraform apply') {
            steps{
                sh 'terraform apply --auto-approve'
            }
        }
    }

    
}

In this script, I have written the 4 stages. The first stage is used to get code from Github. The second stage is used to make the correct format of terraform files then terraform init stage installs the required plugin in the system and finally, terraform apply is used to apply all configurations.

Step-4

In the last, We are good to build this job.

Step-5

Now, VPC is created. We can see in the below picture.

Conslusion:

In this blog, we have seen how to run the terraform with Jenkins. I have also written a blog on how to configure Ansible with Jenkins. you can check out that blog also. You can learn more here.

knoldus

Written by 

Mohd Muzakkir Saifi is a Software Consultant at Knoldus Software. He loves to take deep dives into cloud technologies & different tools. His hobbies are playing gymnastics and traveling.