本文介绍了在 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_UNTF_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" {
  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"
    ]
  }
}

CLI 命令如下.

TF_VAR_UN=foo TF_VAR_PW=bar terraform apply

这篇关于在 Terraform 配置中获取环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-14 13:47