Vazid
Vazid

Reputation: 803

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

I am trying to create a vm instance in gcp with a boot_disk and additional attached_disk using terraform. I could not find any parameter to auto delete the additional attached_disk when instance is deleted.

auto-delete option is availble in gcp console. enter image description here

Terraform code:

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 {

    }
  }
}

Upvotes: 2

Views: 1820

Answers (1)

John Hanley
John Hanley

Reputation: 81414

The feature to set auto-delete for attached disks is not supported. HashiCorp/Google decided to not support this feature for Terraform.

Reference this 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.

My opinion is that Terraform should manage the entire lifecycle from creation to destruction. For disks that you want to attach to a new instance, create those disks as part of your Terraform HCL and destroy them as part of your HCL.

Upvotes: 3

Related Questions