Terraform:如何根据条件将值分配给属性

Terraform: How to assign the value to an attribute based on conditions

如何使用Terraform实现如下登录?

variable "enable_multizone" {
  default = false
}

resource "google_compute_instance" "foo" {
  count = 4
  ...
  if enable_multizone == true:
    zone = data.google_compute_zones.zones.names[count.index % length(data.google_compute_zones.zones.names)]
  else:
    zone = data.google_compute_zones.zones.names[0]
  ...
}

您可以使用 conditional expressions:

获得您所描述的结果
  zone = data.google_compute_zones.zones.names[
    var.enable_multizone ?
    count.index % length(data.google_compute_zones.zones.names) :
    0
  ]