在Terraform配置中获取环境变量? [英] Getting an Environment Variable in Terraform configuration?
本文介绍了在Terraform配置中获取环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个环境变量。一个是 TF_VAR_UN
,另一个是 TF_VAR_PW
。然后,我有一个看起来像这样的terraform文件。
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"
]
}
}
我要用环境变量 TF_VAR_UN
和 TF_VAR_PW
替换的两个值是值用户名和密码。我尝试了上面显示的方法,但没有成功,并且我尝试了一些其他方法,但是总是遇到语法问题。
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.
推荐答案
我会尝试更多类似的方法,它似乎更接近文档 。
I would try something more like this, which seems closer to the 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
这篇关于在Terraform配置中获取环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文