简体   繁体   中英

Conditionally provision a gcp vm instance with terraform

I would like to condition the provisioning of a resource (gcp vm instance) on a variable, for example:

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

but the above is not valid syntax:

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.

Is there a way to accomplish the same? Ideally using terraform alone.

You can use count for that:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM