在Terraform配置中获取环境变量?

问题描述:

我有两个环境变量。一个是TF_VAR_UN,另一个是TF_VAR_UN。然后我有一个terraform文件,看起来像这样。在Terraform配置中获取环境变量?

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" 
     ] 
    } 
} 

两个值我想和环境变量来代替TF_VAR_UNTF_VAR_UN是价值的用户名和密码。我尝试了上面所示的东西,但没有成功,而且我玩弄了其他一些东西,但总是遇到语法问题。

我会尝试更类似的东西,这似乎更接近documentation

variable "UN" {} 
variable "PW" {} 

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" 
    ] 
} 

CLI命令如下所示。

TF_VAR_UN=foo TF_VAR_PW=bar terraform apply 
+0

拖尾'terraform apply'部分困惑我关于TF_VARS的' 你也可以'只导出TF_VAR_your_var'或者像'.profile'这样的点文件 – Mikeumus

,以便使用它需要与 “包裹” 例如:

用户名= “变量$ {var.UN}”