How to Run a Terraform Script in Jenkins: A Step-by-Step Guide
Introduction:
Automating your infrastructure deployments with Terraform and Jenkins can significantly improve your team’s productivity and reduce the risk of manual errors. In this blog, we will walk you through the process of setting up a Jenkins pipeline to run a Terraform script. We’ll start by writing the Terraform code to spin up an EC2 instance and store the state in AWS S3. Then, we’ll create a seed job using the Jenkins Job DSL plugin, which will create an actual job to run the Jenkinsfile. The Jenkinsfile will include all the necessary stages to fetch the code from the source repository, execute Terraform plan and apply, and store the state in AWS S3. So, let’s get started!
Step 1:
Write the Terraform Code Create a new directory for your Terraform project. Inside the directory, create a main.tf
file with the following Terraform code:
provider "aws" {
region = "your-aws-region"
}
resource "aws_instance" "example" {
ami = "your-ami-id"
instance_type = "t2.micro"
tags = {
Name = "ExampleInstance"
}
}
terraform {
backend "s3" {
bucket = "your-bucket-name"
key = "terraform.tfstate"
region = "your-aws-region"
}
}
Replace the placeholders such as “your-aws-region”, “your-ami-id”, and “your-bucket-name” with appropriate values for your AWS setup.
Step 2:
Install Required Plugins Ensure you have Jenkins installed, and then install the following plugins through the Jenkins Plugin Manager:
- Jenkins Job DSL Plugin
- AWS CLI Plugin
- Terraform Plugin
- Pipeline Utility Steps Plugin
Step 3:
Configure AWS Credentials To allow Jenkins to interact with AWS, you need to set up AWS credentials:
- Go to “Manage Jenkins” > “Manage Credentials” > “Jenkins” Store.
- Add a new “AWS Credentials” with your AWS Access Key ID and Secret Access Key.
Step 4:
Create the Seed Job using DSL
- Create a new Freestyle project in Jenkins and name it “Seed Job — Terraform Pipeline.”
- In the “Build” section, select “Process Job DSLs” and provide the DSL script below:
pipelineJob("Terraform Pipeline") {
definition {
cpsScm {
scm {
git('https://github.com/your-terraform-repo.git')
}
scriptPath('Jenkinsfile')
}
}
}
Step 5:
Set up your Terraform Code Repository Create a Git repository with your Terraform code, including the main.tf
file that describes your desired EC2 instance.
Step 6:
Write the Jenkinsfile In your Terraform code repository, create a Jenkinsfile
with the following stages:
pipeline {
agent any
environment {
AWS_DEFAULT_REGION = 'your-aws-region'
}
stages {
stage('Checkout Code') {
steps {
checkout scm
}
}
stage('Terraform Init') {
steps {
script {
sh 'terraform init'
}
}
}
stage('Terraform Plan') {
steps {
script {
sh 'terraform plan -out=tfplan'
}
}
}
stage('Terraform Apply') {
steps {
script {
sh 'terraform apply -auto-approve tfplan'
}
}
}
stage('Upload State to S3') {
steps {
script {
sh 'aws s3 cp terraform.tfstate s3://your-bucket-name'
}
}
}
}
post {
always {
cleanWs()
}
}
}
Step 7:
Configure Jenkins Job
- In Jenkins, create a new pipeline job.
- Under the “Pipeline” section, select “Pipeline script from SCM.”
- Provide the repository URL and specify the Jenkinsfile location (e.g.,
Jenkinsfile
). - Save the job configuration.
Step 8:
Run the Jenkins Job Now, you can run the pipeline job to execute your Terraform code and provision an EC2 instance. Jenkins will fetch the code from the repository, initialize Terraform, create a plan, apply the changes, and upload the Terraform state to an S3 bucket.
Conclusion:
In this blog, we learned how to run a Terraform script in Jenkins by first writing the Terraform code to spin up an EC2 instance and store the state in AWS S3. Then, we created a seed job using the Job DSL plugin, which generated an actual Jenkins pipeline that executes the Terraform stages, including plan, apply, and state storage in AWS S3. By following these steps, you can automate your infrastructure deployments, increasing reliability and efficiency while reducing manual errors. Happy automating!
Thank you for reading my blog! If you enjoyed the content and want to receive timely updates whenever I publish a new article, make sure to follow me on Medium. It’s quick and easy! Just log in to your Medium account (or create one if you haven’t already), visit my profile, and click the “Follow” button. Stay informed and never miss a new post! Your support means a lot to me, and I’m excited to continue sharing valuable insights with you. Happy reading! 🚀