Running powershell (7.4.1) and terraform (1.7.5) on Ubuntu (20.04). I have created a powershell profile in which I set some environment variables for terraform to use when executing it.
For example, in ‘Microsoft.PowerShell_profile.ps1’ I have set.
$env:DA_SUBSCRIPTION_ID="abcd1234"
gci env:DA_*
And when I run pwsh
I can see the value printed to console.
Name Value
---- -----
DA_SUBSCRIPTION_ID abcd1234`
Then I have a terraform file ‘providers.tf’ (among other tf-files) having content like this.
provider "azurerm" {
features {}
subscription_id = "${env.DA_SUBSCRIPTION_ID}"
}
Now, in the powershell console, when I run terraform validate
.
I get response:
A managed resource "env" "DA_SUBSCRIPTION_ID" has not been declared in the root module.
May be simple, but I am not able to find the correct syntax to use when referring to the environment variables set in the powershell profile.
Could someone give me an advice on how to do this correctly.
Thanks.
2
Answers
First, you have to declare a variable in Terraform.
Then, you can set an environment variable in your shell with the
TF_VAR_
prefix. Terraform searches the environment of its own process for environment variables namedTF_VAR_
followed by the name of a declared variable.There’s no need to declare a variable like that. As long as you define
ARM_SUBSCRIPTION_ID
in your PowerShell environment, Terraform will use it for the provider configuration, refer to the Azure provider documentation.However, if you want to explicitly set a different variable for setting the subscription ID, what @vrdse suggested is correct.