Terraform: TF_VAR_ prefixed environment variable ignored by Terraform in Azure DevOps

Created on 3 Nov 2019  ยท  4Comments  ยท  Source: hashicorp/terraform

Running a Terraform build with the following code and environment variable set on the pipeline. It ignores the environment variable and uses default. Works fine locally with PowerShell. Seen lots of similar bugs reported in this area but not beyond the realms that I'm doing something wrong.

variable "testvar" {
default = "meh"
}

variable "testenvvar" {
    type = "string"
    default = "fml"
}

output "printvar" {
value = "${var.testvar}"
}

output "printenvvar" {
value = "${var.testenvvar}"
}

image

Output:

image

Of course, I'm expecting printenvvar = meow instead.

-Terraform 0.12.13
-Agent Pool: Azure Pipelines (hosted)
-Agent Spec: vs2017-win2016

I have workaround -var testenvvar=meow on plan but this should work, is my preferred and seems to be Terraform best practice.

Thanks
Guy

Most helpful comment

Hi @guywood13!

It looks like you might be running into the problem described in microsoft/azure-pipelines-agent#1645: it seems that Azure DevOps doesn't allow setting environment variables containing lowercase letters or periods, and so it's likely converting your variable to be named TF_VAR_TESTENVVAR and thus Terraform (whose variable names are case sensitive) is not looking at it.

It sounds like the Azure team is still considering how to support mixed-case environment variable names in that issue, but in the mean time you might be able to work around it by naming your Terraform variable with an uppercase name so that it won't get altered by the Azure Pipelines Agent. Alternatively, as you've seen you can use the -var and -var-file command line arguments to set variables in a way that isn't subject to transformation by the Azure Pipelines Agent.

If you have any questions about this, please feel free to start a new topic in the community forum. Thanks!

All 4 comments

Hello! :wave:

This issue doesn't seem to be following one of the standard issue templates available from the "New Issue" page. The information requested in those templates is important for my human friends to fully understand what's going on.

Please feel free to open a new issue using either the Bug Report or Feature Request template, as appropriate, and then the Terraform maintainers will review it. Thanks!

Please note that we use GitHub issues for tracking bugs and enhancements rather than for questions. While we may be able to help with certain simple problems here it's better to use the community forum, where there are more people ready to help. The GitHub issues here are generally monitored only by our few core maintainers.

Hi @guywood13!

It looks like you might be running into the problem described in microsoft/azure-pipelines-agent#1645: it seems that Azure DevOps doesn't allow setting environment variables containing lowercase letters or periods, and so it's likely converting your variable to be named TF_VAR_TESTENVVAR and thus Terraform (whose variable names are case sensitive) is not looking at it.

It sounds like the Azure team is still considering how to support mixed-case environment variable names in that issue, but in the mean time you might be able to work around it by naming your Terraform variable with an uppercase name so that it won't get altered by the Azure Pipelines Agent. Alternatively, as you've seen you can use the -var and -var-file command line arguments to set variables in a way that isn't subject to transformation by the Azure Pipelines Agent.

If you have any questions about this, please feel free to start a new topic in the community forum. Thanks!

I've tested this and you're correct. Thank you for pointing me at the root cause.

I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings