我正在尝试创建一个创建计算实例的 terraform 模块。当且仅当我在模块调用期间attached_disk_enabled
设置了变量时,我希望资源具有附加的磁盘。true
我有这个:
resource "google_compute_disk" "my-disk" {
name = "data"
type = "pd-ssd"
size = 20
count = var.attached_disks_enabled ? 1 : 0
}
resource "google_compute_instance" "computer" {
name = "computer"
boot_disk {
...
}
// How do I make this disappear if attached_disk_enabled == false?
attached_disk {
source = "${google_compute_disk.my-disk.self_link}"
device_name = "computer-disk"
mode = "READ_WRITE"
}
}
已经为模块声明了变量vars.tf
。模块调用是这样的:
module "main" {
source = "../modules/computer"
attached_disk_enabled = false
...
}
我知道动态块以及如何使用for
循环遍历列表并设置多个块,但我不确定如何使用此方法从资源中排除块:
dynamic "attached-disk" {
for_each in var.disk_list
content {
source = "${google_compute_disk.my-disk.*.self_link}"
device_name = "computer-disk-${count.index}"
mode = "READ_WRITE"
}
}
我想if
代替for_each
. 有没有办法做到这一点?
$ terraform version
Terraform v0.12.0