【发布时间】:2019-07-09 08:35:43
【问题描述】:
我想创建一个实例,并根据一些变量创建一个附加磁盘。
...
variable "extra-disk-count" {
default = "0"
}
variable "extra-disk-size" {
default = "100"
}
resource "google_compute_instance" "openqa" {
count = "${var.count}"
name = "${var.name}-${element(random_id.service.*.hex, count.index)}"
machine_type = "${var.type}"
zone = "${var.region}"
boot_disk {
initialize_params {
image = "${var.image_id}"
}
}
attached_disk {
source = "${element(google_compute_disk.default.*.self_link, count.index)}"
device_name = "${element(google_compute_disk.default.*.name, count.index)}"
}
....
}
resource "google_compute_attached_disk" "default" {
name = "ssd-disk"
count = "${var.extra-disk-count}"
type = "pd-ssd"
zone = "${var.region}"
size = "${var.extra-disk-size}"
physical_block_size_bytes = 4096
}
如果我不想创建磁盘,我可以设置var.extra-disk-count = 0,但是我得到了错误
* google_compute_instance.openqa: element: element() may not be used with an empty list in:
${element(google_compute_disk.default.*.self_link, count.index)}
因为它试图添加一个未创建的元素。
当var.extra-disk-count = 0 时,如何“禁用”google_compute_instance 中的attached_disk 选项?
【问题讨论】:
-
您使用的是哪个版本的 Terraform?
-
Terraform v0.11.13
-
但如果 0.11 无法做到这一点,如果有更简单的方法,我可以尝试升级到 0.12。
-
0.12 之前的版本是可能的(取决于您是否依赖
google_compute_instance.openqa资源的输出),但在 0.12 版本中更容易。
标签: google-compute-engine terraform