Building CI/CD pipeline for Terraform in Azure DevOps: A Step-by-Step Guide.

Amit Gujar
5 min readApr 14, 2023

Being able to build a CI/CD pipeline for your project is a really important practice in DevOps. This pipeline can save a lot of time by automating the workflow of tasks. Today we will learn, how to create Pipeline for terraform scripts. We will be using Azure DevOps for creating pipelines. This pipeline will automatically deploy terraform code and create resources accordingly on Azure Portal. This tutorial only consists of terraform code as of now but you can use the same steps to deploy other Azure services as well.

Requirements

  • Microsoft Azure account.
  • Azure DevOps account with parallel jobs permit.
  • Git should be installed on your system.
  • Terraform, Replace token extension for your organization.

NOTE: I have already written terraform code for this pipeline tutorial. Feel free to create your own. We will be using Azure Repos for version control in this tutorial.

Let’s Start……🤩

Step 1: Open the Azure DevOps portal and create a new project.

Note: If you are not able to select the public visibility option then go for the private.

Project creation

Step 2: Click on Repos to clone my GitHub repository. Now that you are cloning your copy, you can make changes here directly as your own repository.

Update: I have changed the URL of the git repo. Make changes in the path accordingly while adding terraform tasks.

https://amitgujar3342@dev.azure.com/amitgujar3342/AKS-Migration/_git/AKS-Migration
Importing GitHub repository into the Azure Repos

Step 3: Click on pipelines to create a new pipeline. Select Azure Repos as a source and select that cloned repository. Click on the run button.

Pipeline review

Note: This task will copy our Terraform files into the Terraform folder of the agent and it will generate it as an artifact. We will be using this artifact to create a release pipeline.

Pipeline in action

Step 4: Now click on Releases to create a new release pipeline. Select an empty job. We will be adding all the terraform commands inside this release pipeline as tasks. Once this pipeline gets deployed our resources should get deployed on the portal automatically.

Creating release pipeline

Step 5: Click on add artifact and enter the following details. It will automatically get the latest version of the artifact to create a release and deployment.

Adding artifact

Step 6: Enable continuous deployment trigger by clicking on the flash symbol on the artifact.

Optional: You can also set a schedule to automatically run the pipeline at a specific time

Creating deployment trigger

Step 7: Click on the Stage 1 area to add tasks for our deployment.

Task Initialization

Step 8: Add a task for our agent, select the Azure CLI task, and enter the following details.

Az CLI

Add the following code in the Inline script section and click on save. This script will create a resource group, storage account, and container named terraform to store state files.

# Creating a resource group for our storage account 
az group create -l westus -n $(resourcegroup)

# Create an storage account inside the resource group
az storage account create -n $(storageaccount) -g $(resourcegroup) -l westus --sku Standard_LRS

# Create a container inside storage account
az storage container create -n terraform --account-name $(storageaccount)

az storage account keys list -g $(resourcegroup) --account-name $(storageaccount)

# Get the storage account keys
storageKeys=$(az storage account keys list --account-name $(storageaccount) --resource-group $(resourcegroup) --query "[?keyName=='key1'].value" --output tsv)

echo "$storageKeys"

I have created a backend block to store Terraform.state file inside the Azure blob storage. I have initialized certain values which need to be replaced with the pipeline variables including the storage access key.

Add another task to replace values inside terraform script with variables of the pipeline.

Token Replacement

Step 9: Now we will create another task to install Terraform on our agent.

Step 10: Add terraform init task.

Terraform Init

Step 11: Add terraform validate task. (optional)

Terraform validate

Step 12: Add terraform plan task.

Terraform plan

Step 13: Add terraform apply task.

Terraform apply

Note: You can also add destroy task after this task, if you just want to use this pipeline for testing purposes, make sure to add a lock=false argument for the command to avoid errors.

Step 14: Add variables for our release pipeline. Click on the variables tab beside Tasks & then click on Save.

Initialize variables for the pipeline

Step 15: Create a release for our stage 1

Creating the first release

Step 16: And…..ACTION………

Deploy the release pipeline. This will be our first manual deployment. Next time if you make any changes in the code, it will create a new release and deployment automatically.

Deploying release pipeline

You can click on logs to see details.

Deployment logs

Step 17: Verify your resource deployment on Azure Portal.

Conclusion 😊:

We used the Azure DevOps portal to create CI/CD for our terraform code to automate the resource creation process.

--

--