'Terraform - Use of environment variables in TF files

I would like to use environment variables in my TF files. How can I mention them in those files?

I use Terraform cloud and define the variables in the environment variables section. Which means I don't use my cli to run terraform commands commands (no export TF_VAR & no -var or -var-file parameter).

I didn’t find any answer to this in forums nor in documentation.

Edit: Maybe if I'll elaborate the things I've done it will be much clear.

So I have 2 environment variables named "username" and "password"

Those variables are defined in the environment variables section in Terraform Cloud.

In my main.tf file I create a mongo cluster which should be created with those username and password variables. In the main variables.tf file I defined those variables as:

variable "username" {
  type = string
}

variable "password" {
  type = string
}

My main.tf file looks like:

module "eu-west-1-mongo-cluster" {
...
...
  username = var.username
  password = var.password
}

In the mongo submodule I defined them in variables.tf file as type string and in the mongo.tf file in the submodule I ref them as var.username and var.password

Thanks !



Solution 1:[1]

I don't think what you are trying to do is supported by Terraform Cloud. You are setting Environment Variables in the UI but you need to set Terraform Variables (see screenshot). For Terraform Cloud backend you need to dynamically create *.auto.tfvars, none of the usual -var="myvar=123" or TF_VAR_myvar=123 or terraform.tfvars are currently supported from the remote backend. The error message below is produced from the CLI when running terraform 1.0.1 with a -var value:

? Error: Run variables are currently not supported
? 
? The "remote" backend does not support setting run variables at this time. Currently the
? only to way to pass variables to the remote backend is by creating a '*.auto.tfvars'
? variables file. This file will automatically be loaded by the "remote" backend when the
? workspace is configured to use Terraform v0.10.0 or later.
? 
? Additionally you can also set variables on the workspace in the web UI:
? https://app.terraform.io/app/<org>/<workspace>/variables

My use case is in a CI/CD pipeline with the CLI using a remote Terraform Cloud backend, so created the *.auto.tfvars with for example:

# Environment variables set by pipeline
TF_VAR_cloudfront_origin_path="/mypath"

# Dynamically create *.auto.tfvars from environment variables
cat >dev.auto.tfvars <<EOL
cloudfront_origin_path="$TF_VAR_cloudfront_origin_path"
EOL

# Plan the run
terraform plan

enter image description here

Solution 2:[2]

As per https://www.terraform.io/docs/cloud/workspaces/variables.html#environment-variables, terraform will export all the provided variables.

So if you have defined an environment variable TF_VAR_name then you should be able to use as var.name in the terraform code.

Hope this helps.

Solution 3:[3]

I managed to get around this in my devops pipeline, by copying the terraform.tfvars file from my sub directory to the working directory as file.auto.tfvars

For example:

cp $(System.DefaultWorkingDirectory)/_demo/env/dev/variables.tfvars  $(System.DefaultWorkingDirectory)/demo/variables.auto.tfvars

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 danialk
Solution 2 pradeep
Solution 3 David Makogon