Deploying Terraform using an Azure DevOps pipeline, you can use pipeline variables as part of your Terraform variables, in this blog post I will show you how.
With Terraform, you are probably using .tfvars if deploying the multiple environments – awesome! There may be a time that you want to change a variable or have a static variable that can be changed on a pipeline run, potentially on a trigger or templating pipelines.
Lets have a look at how this can be achieved
The initial pipeline setup
Before we look at creating Terraform variables from Azure DevOps pipeline variables, lets look at the initial pipeline that can be found here
In this example, I will focus primarily on the plan for now – it gives a quick feedback to show in my demo. Looking at this initial task, notice the command options is referencing a .tfvars file?
- task: TerraformTaskV2@2
displayName: 'plan'
inputs:
provider: 'azurerm'
command: 'plan'
commandOptions: '-input=false -var-file="../terraform/environments/$(Environment)/$(Environment).tfvars"'
environmentServiceNameAzureRM: 'thomasthorntoncloud'
workingDirectory: '$(System.DefaultWorkingDirectory)/terraform/'
.tfvars consists of 3 variables found here:
name = "tamops"
location = "uksouth"
environment = "production"
With a successful terraform plan in Azure DevOps we can see:
+ resource "azurerm_resource_group" "acr_resource_group" {
+ id = (known after apply)
+ location = "uksouth"
+ name = "tamops-rg"
+ tags = {
+ "Environment" = "production"
}
}

Pipeline with Azure DevOps Variables to Terraform Variables
A simple addition, looking at the variables within the pipeline – as an example, I want to Terraform to use the var.name
& var.environment
that has been declared from the Azure DevOps pipeline.
Reviewing the variables created in the pipeline, notice environment & name is the same values from the previous .tfvars? (these two variables have been removed from .tfvars):
variables:
- name: backendServiceArm
value: 'thomasthorntoncloud'
- name: backendAzureRmResourceGroupName
value: 'thomasthorntoncloud'
- name: backendAzureRmStorageAccountName
value: 'thomasthorntontfstate'
- name: backendAzureRmContainerName
value: 'adovariableottf'
- name: backendAzureRmKey
value: 'terraform.tfstate'
- name: environment
value: 'production'
- name: name
value: 'tamops'
Now, looking at the terraform plan task:
- task: TerraformTaskV2@2
displayName: 'plan'
inputs:
provider: 'azurerm'
command: 'plan'
commandOptions: '-input=false -var name=$(name) -var environment=$(Environment) -var-file="../terraform/environments/$(Environment)/$(Environment)-ado-variables.tfvars"'
environmentServiceNameAzureRM: 'thomasthorntoncloud'
workingDirectory: '$(System.DefaultWorkingDirectory)/terraform/'
Notice commandOptions now includes both -var
& -var-file
? The pipeline variables have been passed into the Terraform configuration using -var
Reviewing another terraform plan, we can see the exact same plan as previously shown:
# azurerm_resource_group.acr_resource_group will be created
+ resource "azurerm_resource_group" "acr_resource_group" {
+ id = (known after apply)
+ location = "uksouth"
+ name = "tamops-rg"
+ tags = {
+ "Environment" = "production"
}
}
Awesome! A simple but very useful tip to assist you in your Azure DevOps & Terraform journey 🙂
Pipeline using Azure DevOps variables with Terraform
Full code used in this blog post
As always, thanks for viewing – check out my other content!