Conditionally provision a gcp vm instance with terraform

Viewed 65

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.

1 Answers

You can use count for that:

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

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