有条件地使用 terraform 提供 gcp vm 实例

Conditionally provision a gcp vm instance with terraform

我想在变量上设置资源(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。

你可以使用 count

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

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