Terraform-provider-google: Alternating create/delete on apply with google_compute_attached_disk

Created on 22 Sep 2018  ยท  13Comments  ยท  Source: hashicorp/terraform-provider-google


Community Note

  • Please vote on this issue by adding a ๐Ÿ‘ reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or "me too" comments, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment
  • If an issue is assigned to the "modular-magician" user, it is either in the process of being autogenerated, or is planned to be autogenerated soon. If an issue is assigned to a user, that user is claiming responsibility for the issue. If an issue is assigned to "hashibot", a community member has claimed the issue already.

Terraform Version

v0.11.8

Affected Resource(s)

  • google_compute_attached_disk

Terraform Configuration Files

// Disk attachments
resource "google_compute_attached_disk" "extra_disk_attachments" {
  count = "${var.EXTRA_VOLUME_COUNT}"
  disk = "${element(google_compute_disk.extra_disk.*.self_link, count.index)}"
  instance = "${element(google_compute_instance.general_instance_with_volume.*.self_link, 0)}"
}

// Extra disks
resource "google_compute_disk" "extra_disk" {
  count = "${var.EXTRA_VOLUME_COUNT}"
  name  = "${lookup(var.EXTRA_DISKS[count.index], "NAME")}"
  ...
}

// Instances
resource "google_compute_instance" "general_instance_with_volume" {
  name         = "${lookup(var.INSTANCES[count.index], "INSTANCE_NAME")}"
  count        =  "${length(var.INSTANCES)}"
  // IMPORTANT: the attached_disk block is not set
  ...
}

Debug Output

https://gist.github.com/mehemken/1b72cf03131a4cc73d8d0b42633af388

Panic Output

Expected Behavior

Disk attachments are created once and for all

Actual Behavior

terraform apply creates attachments.
terraform apply deletes attachments.
terraform apply creates attachments.
terraform apply deletes attachments.
...
...
...

Steps to Reproduce

use the resource: google_compute_attached_disk

  1. terraform apply
  2. terraform apply
  3. terraform apply

Important Factoids

  • The example code above lives in a module. I'm calling it from a main.tf as a module.
  • setting depends_on does not fix the issue.

The directory structure looks something like this:

.
โ”œโ”€โ”€ configuration
โ”‚  โ”œโ”€โ”€ main.tf
โ”‚  โ”œโ”€โ”€ terraform.tfvars
โ”‚  โ””โ”€โ”€ variables.tf
โ””โ”€โ”€ module
   โ””โ”€โ”€ general_instance
      โ””โ”€โ”€ instance.tf

The code example above lives in instance.tf, I'm instantiating the module in main.tf.

References

Could not find a similar issue

  • #0000
documentation

Most helpful comment

Does lifecycle.ignore_changes = ["attached_disk"] on google_compute_instance resolve the issue?

All 13 comments

@ayk33

If I remove the google_compute_attached disk resources from the module and put them in main.tf, the problem continues.

If I add an empty attached_disk clause to the google_comput_instance declaration in the module, the problem continues.

The current workaround is to hardcode each disk attachment. ๐Ÿ˜ž

Does lifecycle.ignore_changes = ["attached_disk"] on google_compute_instance resolve the issue?

@paddycarver , I'll try this tomorrow during office hours and report back. Thanks for the suggestion.

@paddycarver , it looks promising. I'm going to play with it a little bit more and report back.

Yup. It works. Thank you for the fix @paddycarver .

@mehemken @paddycarver Where in the google_compute_instance resource would the lifecycle parameter be set? I can't seem to find it anywhere in the documentation. Thanks.

Nevermind, I see now that it's a terraform meta-parameter. For anyone else that hits this issue, this is what the code should look like:

resource "google_compute_instance" "this" {
  count                     = "${local.enabled}"
  name                     = "${local.name}"
  machine_type       = "${local.machine_type}"
  allow_stopping_for_update = true
  zone                      = "${local.zone}"
  tags                      = ["${concat(local.network_tags,local.default_tags)}"]

  service_account {
    email  = "${local.service_account}"
    scopes = ["storage-rw"]
  }

  boot_disk {
    initialize_params {
      image = "${local.image}"
      size  = "${local.boot_volume_size}"
      type  = "${local.boot_volume_type}"
    }
  }

  network_interface {
    subnetwork = "${local.subnet}"

    access_config {
      #creates ephemeral ip
    }
  }

  labels {
    name        = "${local.name}"
    environment = "${local.env}"
    application = "${local.application}"
  }

  # YOU NEED THIS TO PREVENT DISK ATTACHMENT LOOP
  lifecycle {
    ignore_changes = ["attached_disk"]
  }
}

have the same issue, but ignore_changes didn't work for me. But maybe I'm using it wrong.
The value has to be the name of the resource right? Not of the disk

Oh, is literally ignore_changes = ["attached_disk"]

I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. If you feel I made an error ๐Ÿค– ๐Ÿ™‰ , please reach out to my human friends ๐Ÿ‘‰ [email protected]. Thanks!

Was this page helpful?
0 / 5 - 0 ratings