如何使用 terraform 在 gcp 实例中设置额外 attached_disk 的自动删除选项?

How to set auto-delete option for additional attached_disk in gcp instance uing terraform?

我正在尝试使用 terraform 在 gcp 中创建一个带有 boot_disk 和附加 attached_disk 的 vm 实例。当删除实例时,我找不到任何参数来自动删除额外的 attached_disk。

自动删除选项在 gcp 控制台中可用。

地形代码:

resource "google_compute_disk" "elastic-disk" {
    count   = var.no_of_elastic_intances
    name    = "elastic-disk-${count.index+1}-data"
    type    = "pd-standard"
    size    = "10"
}

resource "google_compute_instance" "elastic" {
  count        = var.no_of_elastic_intances
  name         = "${var.elastic_instance_name_prefix}-${count.index+1}"
  machine_type = var.elastic_instance_machine_type
  boot_disk {
    auto_delete = true
    mode = "READ_WRITE"
    initialize_params {
      image = var.elastic_instance_image_type
      type  = var.elastic_instance_disc_type
      size = var.elasitc_instance_disc_size
    }
  }
  attached_disk {
    source = "${element(google_compute_disk.elastic-disk.*.self_link, count.index)}"
    mode = "READ_WRITE"
  }
  network_interface {
    network = var.elastic_instance_network
    access_config {

    }
  }
}

不支持为附加磁盘设置自动删除的功能。 HashiCorp/Google 决定不支持 Terraform 的此功能。

参考这个 issue:

If Terraform were told to remove the instance, but not the disks, and auto-delete were enabled, then it would not specifically delete the disks, but they would still be deleted by GCP. This behaviour would not be shown in a plan run, and so could lead to unwanted outcomes, as well as the state still showing the disks existing.

我的观点是 Terraform 应该管理从创建到销毁的整个生命周期。对于要附加到新实例的磁盘,将这些磁盘创建为 Terraform HCL 的一部分,并将它们作为 HCL 的一部分销毁。