How to integrate Terraform with Jenkins

In this post, we will explore how Jenkins can be used to manage Terraform deployments and workflows. We will also implement a Jenkins pipeline to initiate Terraform deployments. In addition to using Jenkins to manage Terraform workflows, this post includes some of its most valuable customization options.
Jenkins
Jenkins is a Java-based open-source CI/CD automation tool. It was one of the first automation tools in the early days of "mainstream" DevOps. Today, it is used by many organizations and users, is well supported by the community, and has a roadmap for the future.
Jenkins is a CI/CD automation tool as well as a framework that can implement a variety of automation scenarios. A variety of scenarios are covered, including the automatic release of updates to mobile apps and embedded hardware.
Jenkins supports a wide range of automation scenarios through its plugin ecosystem. Our goal in this blog post is to implement an end-to-end workflow step-by-step using Terraform as one of the plugins. Refer to our previous article on How to setup and manage projects in Jenkins and How to schedule Jobs in Jenkins
Terraform
Terraform is HashiCorp's offering for infrastructure as code. The tool allows infrastructure to be built, changed, and managed in a safe and repeatable manner. HashiCorp Configuration Language (HCL) allows operators and infrastructure teams to create human-readable, automated deployments with Terraform.
Infrastructure as Code
If you're not familiar with infrastructure as code, it involves managing infrastructure in files rather than manually configuring resources in a user interface. Typically, a resource is any piece of infrastructure in an environment, such as a virtual machine, security group, or network interface.
With Terraform, operators can create definition files of their desired resources using HCL on almost any provider (AWS, GCP, GitHub, Docker, etc) and Terraform automatically creates them.
How to integrate Terraform with Jenkins
Step 1: Install Terraform Plugin on Jenkins
Install Jenkins by following the instructions on this page. Upon completion of the installation and enabling administration login, click the “Manage Jenkins” tab in the left navigation menu.
Install the Terraform plugin by clicking on the "Tools" icon.
Find "Terraform" by using the search bar in the Available Plugins menu on the left.Choose "Install without restart" from the checkbox beside Terraform.
Thus we have successfully installed the Terraform plugin in Jenkins. You can confirm this by navigating to the same path, and checking that it is available in the "Installed plugins" section.
Step 2: Install the Terraform binary
Jenkins needs the Terraform binary to work with the Terraform plugin. There are two ways to do this, depending on whether Terraform binary is installed separately (when it already exists) or via Jenkins auto-installer. To configure Terraform, go to “Dashboard > Manage Jenkins > Global Tool Configuration” and scroll to the section that represents “Terraform.”
The following screen appears when you click on the "Terraform installations" tab. By filling out the form in the center, we can give this installation a name and specify its location. Alternatively, if Terraform was installed before Jenkins, we can specify the Terraform binary's path in the "Installation directory" field.
We can also choose "Install automatically" if Terraform is not installed on the host. This option will help Jenkins install the Terraform version from multiple sources based on the help text in the screenshot below. The installer can be specified in several ways during automatic installation. It is easiest to install Terraform via bintray.com if we don't want to maintain the Terraform installation separately.
Other options include specifying a location from which to download the binary, running custom shell commands to install Terraform, etc. Due to a local Jenkins installation and Terraform installation, simply specifying the "Install directory" path was sufficient for this example.
Additional Terraform installations can be enabled by clicking the "Add Terraform" buttons. This becomes handy when Terraform versions are used across multiple pipelines. We can now build our Jenkins pipeline by clicking "Save."
Step 3: Setting up Terraform Config
We are going to create a single AWS EC2 instance using a simple Terraform configuration. I have hosted the configuration on Github.In the next steps, we will develop a Jenkins pipeline that consumes this configuration as well.
Step 4: Set up a Jenkins pipeline
To create a new item, navigate to "Dashboard" and click "New Item".
Choose the "Pipeline" icon among all the options on the following screen after entering a valid pipeline name. Save this pipeline by clicking "Ok".
Upon saving, we are directed to the Configuration page of the pipeline, where we can configure this newly created pipeline in multiple ways. For better understanding, please review the information messages hidden behind the question mark symbol if you're new to Jenkins.Click on Save now after configuring all the details.
Step 5: Jenkins Pipeline Script
Groovy scripting language is used to define Jenkins pipelines. If you are not familiar with it, we can generate this script using Jenkins' "Pipeline Syntax" utility. Click on "Dashboard > Terraform-Jenkins-Demo > Configuration." Scroll to the "Pipeline" section.
Next, we'll write the Terraform and GitHub scripts. To write the script, we can use the Jenkins syntax generator.
We will end up with a pipeline script like this
pipeline {
agent any
stages {
stage('Checkout') {
steps {
checkout( [$class: 'GitSCM',
branches: [[name: '*/main']],
extensions: [],
userRemoteConfigs: [[url: 'https://github.com/muthuannamalai12/terraform-demo-jenkins.git']]])
}
}
stage ("terraform init") {
steps {
bat ('terraform init')
}
}
stage ("terraform Action") {
steps {
bat ('terraform ${action} --auto-approve')
}
}
}
}
Step 7: Run the pipeline
Navigate to: “Dashboard >Terraform-Jenkins-Demo .”
Click on "Build Now" from the left navigation menu. Under the stage view, you will see the various stages of the pipelines as they are executed in the main panel. In Build History, click on the sequence number to learn more about that particular build.
To view the complete console output, click the "Console Output" tab. It is possible to identify the root cause of the failure, identify a solution, and decide to change the pipeline configuration if the build does not succeed.
The build indicates success, meaning Jenkins has successfully executed Terraform to create the AWS infrastructure.
Make sure the EC2 instance / intended infrastructure configured in IaC is created in AWS Console.
Conclusion:
We created an EC2 instance on AWS using Terraform. We have covered the basics of Terraform and Jenkins. There are several capabilities for constructing, modifying, and versioning infrastructures.