【问题标题】:Conditionally provision a gcp vm instance with terraform有条件地使用 terraform 配置 gcp vm 实例
【发布时间】:2022-01-18 04:57:39
【问题描述】:

我想以变量为条件提供资源(gcp vm 实例),例如:

resource "${var.param > 0 ? "google_compute_instance" : "null_resource"}" "cluster" {
  # ...
}

但上述语法无效:

Error: Invalid resource type name
A name must start with a letter or underscore and may contain only letters, digits, underscores, and dashes.

Error: Invalid string literal
Template sequences are not allowed in this string. To include a literal "$", double it (as "$$") to escape it.

有没有办法做到这一点?理想情况下单独使用 terraform。

【问题讨论】:

    标签: google-cloud-platform terraform terraform-provider-gcp


    【解决方案1】:

    您可以为此使用count

    resource "google_compute_instance" {
      count = var.param > 0 ? 1 : 0
    }
    
    resource "cluster" {
      count = var.param > 0 ? 0 : 1
    }
    

    【讨论】:

    • 有没有办法避免副作用:Because google_compute_instance.[...] has "count" set, its attributes must be accessed on specific instances. For example, to correlate with indices of a referring resource, use: google_compute_instance.[...][count.index] ?
    • @Sparkler 遗憾的是没有,除非您在应用 TF 文件之前对其进行预处理。一些人使用 jinja2 模板化他们的 TF 代码,以使其以后“更干净”。
    猜你喜欢
    • 2021-04-22
    • 2021-08-14
    • 2021-09-20
    • 2021-10-19
    • 2019-11-05
    • 2021-09-26
    • 2019-08-12
    • 2021-09-16
    • 1970-01-01
    相关资源
    最近更新 更多