Terraform:GCP 磁盘类型 "PERSISTENT" -> "pd-standard" 强制 instance_template 在每个 运行 上重建

Terraform: GCP disk type "PERSISTENT" -> "pd-standard" forces instance_template rebuild on every run

Terraform 被迫在每个 运行 上重建 instance_templates,因为磁盘类型从 "PERSISTENT" 更改为 "pd-standard"

例如:

# module.instance_template_webserver.google_compute_instance_template.tpl must be replaced
+/- resource "google_compute_instance_template" "tpl" {
<...>
      ~ disk {
            auto_delete  = true
            boot         = true
          ~ device_name  = "persistent-disk-0" -> (known after apply)
            disk_size_gb = 100
            disk_type    = "pd-standard"
          ~ interface    = "SCSI" -> (known after apply)
          - labels       = {} -> null
          ~ mode         = "READ_WRITE" -> (known after apply)
          ~ source_image = "projects/cicd-00055-dd12/global/images/webserver-frontend-1591611505" -> "https://www.googleapis.com/compute/v1/projects/cicd-00055-dd12/global/im
ages/webserver-frontend-1591611505"
          ~ type         = "PERSISTENT" -> "pd-standard" # forces replacement
        }
<...>
}

为什么会这样,我们该如何避免?

似乎我们找到了原因(和解决方案):

在 terraform 资源 google_compute_instance_template 中,我们将 disk_typetype 混淆了。

删除后 type = var.disk_type 一切正常。

https://www.terraform.io/docs/providers/google/r/compute_instance_template.html 说:

disk_type - (Optional) The GCE disk type. Can be either "pd-ssd", "local-ssd", or "pd-standard".

type - (Optional) The type of GCE disk, can be either "SCRATCH" or "PERSISTENT".

完全差异:

@@ -35,7 +35,6 @@ resource "google_compute_instance_template" "tpl" {
      disk_size_gb = var.disk_size_gb
      disk_type    = var.disk_type
      source_image = data.google_compute_image.image_family.self_link
-     type         = var.disk_type
  }

  service_account {