In another episode of “things you should know but aren’t mentionned in the documentation”: Declaring and using Terraform environment variables via variable groups in Azure pipelines.
The problem
So let’s say you have a variable group TF-VARs-group, you want to use a Terraform variable “varableiwanttouse”, and want to export it as TF_VAR_varableiwanttouse. Normally you would create it in the Library:
And declare it in your azure-pipelines.yml
variables:
- group: TF-VARs-group
Well it doesn’t work. The Terraform plan per example will throw an error saying no value was provided for “varableiwanttouse”. However, when I included the TF_VAR_ as an env: block in the task itself, it worked.
- task: TerraformCLI@0
displayName: 'Plan Terraform'
inputs:
command: 'plan'
environmentServiceName: $(service-name)
runAzLogin: true
allowTelemetryCollection: true
providerAzureRmSubscriptionId: '$(subscriptionid)'
publishPlanResults: 'infrastructure-plan'
commandOptions: '-input=false -out=$(System.DefaultWorkingDirectory)/infrastructure-plan.tfplan -detailed-exitcode'
env:
TF_VAR_varableiwanttouse: $(TF_VAR_varableiwanttouse)
This meant that the TF_VAR_ variable from the variable group was ignored during the runtime of the pipeline. But this is not a sustainable fix.
The fix
Well, looks like for Terraform you need to declare these TF_VAR_ variables in uppercase, all capitals ( no cap wllh hhh). This means you should define it in your variable group as follows : TF_VAR_VARIABLEIWANTTOUSE.
It iiiis what it iiiis. Now we both know.