Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting an Environment Variable in Terraform configuration?

I have two environment variables. One is TF_VAR_UN and another is TF_VAR_PW. Then I have a terraform file that looks like this.

resource "google_container_cluster" "primary" {     name = "marcellus-wallace"     zone = "us-central1-a"     initial_node_count = 3      master_auth {         username = ${env.TF_VAR_UN}         password = ${env.TF_VAR_PW}     }      node_config {         oauth_scopes = [             "https://www.googleapis.com/auth/compute",             "https://www.googleapis.com/auth/devstorage.read_only",             "https://www.googleapis.com/auth/logging.write",             "https://www.googleapis.com/auth/monitoring"         ]     } } 

The two values I'd like to replace with the environment variables TF_VAR_UN and TF_VAR_PW are the values username and password. I tried what is shown above, with no success, and I've toyed around with a few other things but always get syntax issues.

like image 567
Adron Avatar asked Apr 14 '16 16:04

Adron


People also ask

How do I see terraform environment variables?

Terraform can directly access environment variables that are named using the pattern TF_VAR_ , for example TF_VAR_foo=bar will provide the value bar to the variable declared using variable "foo" {} .

How can we use environment variables to pass input variables in terraform scripts?

Additionally, input variable values can also be set using Terraform environment variables. To do so, simply set the environment variable in the format TF_VAR_<variable name> . The variable name part of the format is the same as the variables declared in the variables.tf file.

How do you access a variable in terraform?

Terraform variables can be defined within the infrastructure plan but are recommended to be stored in their own variables file. All files in your Terraform directory using the . tf file format will be automatically loaded during operations. Create a variables file, for example, variables.tf and open the file for edit.


2 Answers

I would try something more like this, which seems closer to the documentation.

variable "UN" {   type = string }  variable "PW" {   type = string }  resource "google_container_cluster" "primary" {   name = "marcellus-wallace"   zone = "us-central1-a"   initial_node_count = 3    master_auth {     username = var.UN     password = var.PW   }    node_config {     oauth_scopes = [         "https://www.googleapis.com/auth/compute",         "https://www.googleapis.com/auth/devstorage.read_only",         "https://www.googleapis.com/auth/logging.write",         "https://www.googleapis.com/auth/monitoring"     ]   } } 

With the CLI command being the below.

TF_VAR_UN=foo TF_VAR_PW=bar terraform apply 
like image 196
Liam Avatar answered Oct 08 '22 12:10

Liam


The use of interpolation syntax throws warning with terraform v0.12.18. Now you don't need to use the interpolation syntax. You can just reference it as var.hello.

Caution : One important thing to understand from a language standpoint is that, you cannot declare variables using environment variables. You can only assign values for declared variables in the script using environment varibles. For example, let's say you have the following .tf script

variable "hello" { type=string }

Now if the environment has a variable TF_VAR_hello="foobar", during runtime the variable hello will have the value "foobar". If you assign the variable without the declaration of the variable there will not be any effect.

like image 32
Soundararajan Avatar answered Oct 08 '22 11:10

Soundararajan