How to Use Terraform with Jenkins for Infrastructure Automation
To use
Terraform with Jenkins, create a Jenkins pipeline that runs Terraform commands like init, plan, and apply. Configure Jenkins with proper credentials and workspace to securely manage infrastructure as code and automate deployments.Syntax
A Jenkins pipeline for Terraform typically includes these steps:
- Checkout: Get your Terraform code from version control.
- Terraform Init: Initialize Terraform in the workspace.
- Terraform Plan: Preview infrastructure changes.
- Terraform Apply: Apply changes to the cloud.
Each step runs a shell command inside Jenkins to execute Terraform commands.
groovy
pipeline {
agent any
stages {
stage('Checkout') {
steps {
git 'https://github.com/your-repo/terraform-code.git'
}
}
stage('Terraform Init') {
steps {
sh 'terraform init'
}
}
stage('Terraform Plan') {
steps {
sh 'terraform plan -out=tfplan'
}
}
stage('Terraform Apply') {
steps {
sh 'terraform apply -auto-approve tfplan'
}
}
}
}Example
This example Jenkinsfile shows a complete pipeline that clones Terraform code, initializes Terraform, plans changes, and applies them automatically. It assumes Jenkins has Terraform installed and credentials configured.
groovy
pipeline {
agent any
environment {
TF_VAR_region = 'us-east-1'
}
stages {
stage('Checkout') {
steps {
git 'https://github.com/your-repo/terraform-aws-example.git'
}
}
stage('Terraform Init') {
steps {
sh 'terraform init'
}
}
stage('Terraform Plan') {
steps {
sh 'terraform plan -out=tfplan'
}
}
stage('Terraform Apply') {
steps {
sh 'terraform apply -auto-approve tfplan'
}
}
}
}Output
Terraform has been successfully initialized!
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
+ create
Plan: 1 to add, 0 to change, 0 to destroy.
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Common Pitfalls
Common mistakes when using Terraform with Jenkins include:
- Not configuring AWS or cloud credentials in Jenkins, causing authentication failures.
- Running
terraform applywithout a plan file, risking unintended changes. - Not using
-auto-approvein automated pipelines, causing the job to hang waiting for manual approval. - Storing Terraform state locally instead of remote backend, leading to state conflicts in CI/CD.
Always use remote state storage like S3 with locking and configure credentials securely in Jenkins.
groovy
/* Wrong way: Applying without plan and approval */ pipeline { agent any stages { stage('Apply') { steps { sh 'terraform apply' } } } } /* Right way: Use plan and auto-approve */ pipeline { agent any stages { stage('Plan') { steps { sh 'terraform plan -out=tfplan' } } stage('Apply') { steps { sh 'terraform apply -auto-approve tfplan' } } } }
Quick Reference
Tips for using Terraform with Jenkins:
- Install Terraform on Jenkins agents or use Docker containers with Terraform.
- Store Terraform state remotely (e.g., AWS S3 with DynamoDB locking).
- Use Jenkins credentials plugin to manage cloud provider secrets securely.
- Separate pipeline stages for
init,plan, andapplyfor clarity and control. - Use
-auto-approveflag inapplyto avoid manual intervention.
Key Takeaways
Create Jenkins pipelines that run Terraform commands step-by-step: init, plan, and apply.
Always use remote state storage and manage credentials securely in Jenkins.
Use the plan file and -auto-approve flag to automate Terraform apply safely.
Separate pipeline stages improve clarity and error handling.
Test your pipeline with small changes before full deployment.