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.
First, you have to declare a variable in Terraform.
variable "DA_SUBSCRIPTION_ID" {
type = string
}
provider "azurerm" {
features {}
subscription_id = var.DA_SUBSCRIPTION_ID
}
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 named TF_VAR_
followed by the name of a declared variable.
$env:TF_VAR_DA_SUBSCRIPTION_ID="abcd1234"
terraform plan