Google compute engine 如何使用Terraform禁用google_compute_实例中的资源
我想创建一个实例,并根据一些变量创建一个连接的磁盘Google compute engine 如何使用Terraform禁用google_compute_实例中的资源,google-compute-engine,terraform,Google Compute Engine,Terraform,我想创建一个实例,并根据一些变量创建一个连接的磁盘 ... variable "extra-disk-count" { default = "0" } variable "extra-disk-size" { default = "100" } resource "google_compute_instance" "openqa" { count = "${var.count}" name = "${var.name}-${ele
...
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\u compute\u实例
中“禁用”连接的磁盘选项 在google\u compute\u实例中,您可以使用“google\u compute\u attached\u disk”而不是attached\u disk。
请注意,额外的磁盘计数应为0或与google\u compute\u附加的\u disk.default(var.count)的数量相同。如果额外磁盘计数大于var.count,则会出错。如果额外磁盘数大于0小于var.count,则某些计算实例将没有磁盘
所以我想修改如下:
// remove extra-disk-count
variable "create-extra-disk" {
default = "false"
}
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}"
}
}
}
resource "google_compute_attached_disk" "default" {
count = "${var.create-extra-disk ? var.count: 0}"
disk = "${element(google_compute_disk.openqa.*.self_link, count.index)}"
instance = "${element(google_compute_instance.default.*.self_link, count.index)}"
}
resource "google_compute_disk" "default" {
name = "ssd-disk"
count = "${var.create-extra-disk ? var.count : 0}"
type = "pd-ssd"
zone = "${var.region}"
size = "${var.extra-disk-size}"
physical_block_size_bytes = 4096
}
如果“创建额外磁盘”为false,则不会创建和附加额外磁盘。
如果“创建额外磁盘”为true,则将创建额外磁盘并将其附加到所有计算实例。您可以使用“谷歌计算附加磁盘”,而不是谷歌计算实例中的附加磁盘。
请注意,额外的磁盘计数应为0或与google\u compute\u附加的\u disk.default(var.count)的数量相同。如果额外磁盘计数大于var.count,则会出错。如果额外磁盘数大于0小于var.count,则某些计算实例将没有磁盘
所以我想修改如下:
// remove extra-disk-count
variable "create-extra-disk" {
default = "false"
}
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}"
}
}
}
resource "google_compute_attached_disk" "default" {
count = "${var.create-extra-disk ? var.count: 0}"
disk = "${element(google_compute_disk.openqa.*.self_link, count.index)}"
instance = "${element(google_compute_instance.default.*.self_link, count.index)}"
}
resource "google_compute_disk" "default" {
name = "ssd-disk"
count = "${var.create-extra-disk ? var.count : 0}"
type = "pd-ssd"
zone = "${var.region}"
size = "${var.extra-disk-size}"
physical_block_size_bytes = 4096
}
如果“创建额外磁盘”为false,则不会创建和附加额外磁盘。
如果“创建额外磁盘”为true,则将创建额外磁盘并将其连接到所有计算实例。您使用的是Terraform的哪个版本?Terraform v0.11.13但如果在0.11中不可能,我可以尝试升级到0.12,如果有更简单的方法。在0.12之前可以(取决于您是否依赖于
google_compute_instance.openqa
资源的输出)但是使用0.12更容易。您使用的是Terraform的哪个版本?Terraform v0.11.13但是如果使用0.11不可能,如果有更简单的方法,我可以尝试升级到0.12。这在0.12之前是可能的(取决于您是否依赖于google\u compute\u实例.openqa
resource的输出)但是使用0.12更容易。这非常好。非常感谢!只是一个小的更正。最后一个资源应该是“google\u compute\u disk”很好。我修改了你的评论,效果很好。非常感谢!只是一个小小的更正。最后一个资源应该是“谷歌计算磁盘”。很好。我修改了你的评论