Integrating Jenkins with Terraform: A Step-by-Step Guide

Yogesh BorudeYogesh Borude
4 min read

In today's fast-paced DevOps environment, automating infrastructure provisioning is critical for achieving operational efficiency and reliability. By integrating Jenkins—an open-source automation server—with Terraform, a powerful Infrastructure as Code (IaC) tool, you can streamline your CI/CD workflows and manage your cloud resources effectively. This blog will walk you through the setup process, from installation to running your first Terraform job through Jenkins.

Why Integrate Jenkins with Terraform?

Integrating Jenkins with Terraform offers several advantages:

  • Automation: Easily provision and de-provision infrastructure resources automatically.

  • Version Control: Manage infrastructure changes through code stored in version control systems like Git.

  • Reuse: Create reusable infrastructure modules, making your setup both efficient and maintainable.

  • Continuous Delivery: Achieve continuous delivery for your infrastructure setups, ensuring that changes are delivered rapidly and reliably.

Prerequisites

Before you get started, ensure that you have:

  • An Ubuntu machine (server or VM).

  • Jenkins installed and running.

  • Terraform installed on the Jenkins server.

Step 1: Installing Jenkins on Ubuntu

If Jenkins is not already installed, follow these steps:

  1. Update System Packages:

     sudo apt update
     sudo apt upgrade -y
    
  2. Install Java (Jenkins requires Java):

     sudo apt install openjdk-11-jdk -y
    
  3. Add Jenkins Repository and Install Jenkins:

     wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -
     echo deb http://pkg.jenkins.io/debian-stable binary/ | sudo tee /etc/apt/sources.list.d/jenkins.list
     sudo apt update
     sudo apt install jenkins -y
    
  4. Start and Enable Jenkins Service:

     sudo systemctl start jenkins
     sudo systemctl enable jenkins
    
  5. Access Jenkins: Open your web browser and go to http://your_server_ip_or_domain:8080 to complete the Jenkins setup.

Step 2: Installing Terraform on Ubuntu

  1. Install Unzip Utility (if not already installed):

     sudo apt install unzip -y
    
  2. Download the Terraform Binary:

     wget https://releases.hashicorp.com/terraform/1.6.0/terraform_1.6.0_linux_amd64.zip
    
  3. Unzip and Install Terraform:

     unzip terraform_1.6.0_linux_amd64.zip
     sudo mv terraform /usr/local/bin/
    
  4. Verify Terraform Installation:

     terraform --version
    

Step 3: Configure Jenkins for Terraform Integration

  1. Install Terraform Plugin in Jenkins:

    • Navigate to Manage Jenkins > Manage Plugins.

    • In the "Available" tab, search for "Terraform" and install the plugin. Restart Jenkins after the installation.

  2. Configure Terraform in Jenkins:

    • Go to Manage Jenkins > Global Tool Configuration.

    • Scroll to the "Terraform" section, click Add Terraform, and specify the path to the Terraform executable (usually /usr/local/bin/terraform).

  3. Add Credentials for Terraform Providers:

    • Navigate to Manage Jenkins > Manage Credentials.

    • Add new credentials for the cloud provider you will use (e.g., AWS, Azure).

Step 4: Create a Jenkins Job to Run Terraform

  1. Create a New Jenkins Job:

    • Go to the Jenkins Dashboard and click on New Item.

    • Select Freestyle Project or Pipeline, depending on your preference.

  2. Configure Source Code Management:

    • If your Terraform configurations are in a version control system (like Git), configure the SCM settings to pull from your repository. Enter the repository URL and credentials.
  3. Add Build Steps for Terraform:

    • For a Freestyle project, under the "Build" section, add build steps to initialize and apply Terraform:

      • Terraform Init:

        • Select the Terraform command: init.
      • Terraform Apply:

        • Select the command: apply -auto-approve.
  4. Save and Run the Job:

    • Save your job configuration and click Build Now to execute it.

Example Terraform Script

Here's a basic Terraform configuration script for deploying an AWS EC2 instance:

provider "aws" {
  region = "us-west-2"
}

resource "aws_instance" "example" {
  ami           = "ami-0c94855ba95c71c99"
  instance_type = "t2.micro"
  tags = {
    Name = "Jenkins-Terraform-Example"
  }
}

Running and Monitoring Terraform Jobs

Once you trigger the Jenkins job, you can monitor the execution in the Jenkins console output. This will provide you with detailed logs of each Terraform command, including any potential errors or warnings.

Conclusion

Integrating Jenkins with Terraform allows for a robust automated infrastructure provisioning setup that enhances productivity and ensures consistency across environments. By following the steps outlined above, you can effectively automate your cloud resource management, making your DevOps workflows more efficient.

Feel free to share your experiences with Jenkins and Terraform integration in the comments below, and don't forget to like and share this blog! Happy automating!

For more information on Terraform, visit Terraform Official Documentation.

0
Subscribe to my newsletter

Read articles from Yogesh Borude directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Yogesh Borude
Yogesh Borude

I am a DevOps engineer with over 2+ years of experience in enhancing deployment processes and automating workflows. Passionate about cloud technologies and continuous integration, I specialize in Docker, Kubernetes, and CI/CD pipelines.