How to run Terraform in your Jenkins CI/CD pipeline.

How to run Terraform in your Jenkins CI/CD pipeline.

Running Terraform in Jenkins involves setting up a Jenkins job or pipeline to automate your infrastructure provisioning and management. Here are the general steps to run Terraform in Jenkins:

Prerequisites

  • Install Jenkins: If Jenkins is not already installed, you need to install it on a server or a suitable environment. You can follow the official installation guide for your specific operating system: https://www.jenkins.io/download/
  • Install Terraform: Ensure that Terraform is installed on the Jenkins server or the Jenkins agent where you plan to run your jobs. You can download Terraform from the official website (https://www.terraform.io/downloads.html) and install it according to your platform.

Create a Jenkins Job or Pipeline

You can create a Jenkins job or pipeline based on your project's requirements. The choice between a freestyle job or a pipeline depends on your needs and complexity. A pipeline is more versatile and recommended for complex workflows.

  • Freestyle Job: Create a new Jenkins job. In the job configuration, add a build step (Execute shell) to run Terraform commands. Here's an example of running Terraform init and apply:
cd /path/to/your/terraform/project
terraform init
terraform apply -auto-approve

Freestyle Job
  • Pipeline: Create a Jenkinsfile in your project repository to define the pipeline. Below is an example Jenkinsfile for running Terraform commands using a declarative pipeline:
pipeline {
    agent any

    stages {
        stage('Checkout') {
            steps {
                checkout scm
            }
        }
        stage('Terraform Init') {
            steps {
                sh 'cd /path/to/your/terraform/project && terraform init'
            }
        }
        stage('Terraform Apply') {
            steps {
                sh 'cd /path/to/your/terraform/project && terraform apply -auto-approve'
            }
        }
    }
}

Jenkins file for running Terraform commands using a declarative pipeline.

Configure Jenkins Job

Configure the Jenkins job or pipeline with the necessary settings, such as repository URL, credentials, and triggers (e.g., GitHub webhook, periodic builds, etc.).

Install Required Plugins

Depending on your project's needs, you may need to install Jenkins plugins for version control systems (e.g. Git), and any other required integrations for cost control, drift detection and policies.

Run the Job

Trigger the Jenkins job manually or configure it to be triggered automatically based on your requirements.

Some Hygiene steps

  • Ensure that you monitor the Terraform job's output in the Jenkins console output or logs. You can also use Jenkins plugins like the "Terraform Plugin" to display Terraform-specific information within Jenkins
  • After the Terraform job is complete, you may want to add a post-build step to clean up any resources or artifacts if needed. For example, you can run terraform destroy to tear down the infrastructure created by Terraform.
  • Ensure that you manage credentials and secrets securely within Jenkins, especially if your Terraform code interacts with cloud providers or requires access keys.
  • Implement error handling and notifications within your Jenkins job or pipeline to alert you in case of any issues during the Terraform run.

Jenkins Pricing

So, how much does jenkins actually cost?

Jenkins has been freely available as an open-source tool since its inception in 2011. Over the years, it has earned a reputation as the go-to choice for developers, appealing to a wide range of users from burgeoning startups to large enterprises. Using Jenkins incurs no licensing fees since it is an open-source tool. However, you do need to bear in mind that you are responsible for both hosting it on a server and ensuring its ongoing maintenance and updates. You can find more information and documentation on this here

Summary

Jenkins is an excellent choice for implementing versatile pipeline automation, offering a broad spectrum of applications. The flexibility it provides allows for extensive customization of Terraform workflow pipelines. Therefore, for organizations that have already embraced Jenkins for various functions and are just beginning to explore Terraform, integrating Terraform pipelines into Jenkins becomes a logical and practical step.

But for most organisations building automation for infrastructure as code are looking for the following 3 features:

  1. Enterprise Support (Potentially Non-Negotiable): The need for comprehensive enterprise-level assistance and guidance, which may be an inflexible requirement.
  2. Swiftly and Reliably Achieving a Comparable Configuration as Described, Minus the Necessity for Specialized Knowledge to Conceive or Execute Such a Solution: Quickly and consistently attaining a similar setup as outlined above, without the need for in-depth expertise to conceptualize or enact it.
  3. A Reliable Agreement for Usage with Clear Dependability for Other Systems: A dependable contract for usage that other systems can readily rely upon.

Digger simplifies running Terraform, Pulumi, Cloud Formation in the CI/CD of your choice. Digger has:

  • Private runners by default - no sharing of secrets with a 3rd party
  • Is scalable & reliable - Digger reuses your existing CI/CD system for compute
  • Faster Deployments - Digger has parallel runs enabled on all plans
  • Easy to get started - No need to host and maintain an extra server.
  • Audit Trails  - Digger Maintains an audit trail of all deployments & changes.
  • Policies -  Enforce project and organisation level policies (Via OPA) for compliance.
  • RBAC -  Control who can view, modify, and deploy infrastructure based on their role.
  • Single Sign-On (SSO) via SAML - User authentication and access management with  SSO through SAML integration.

Try it here or join Digger’s slack for more information.