v0.11.8
// 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
...
}
https://gist.github.com/mehemken/1b72cf03131a4cc73d8d0b42633af388
Disk attachments are created once and for all
terraform apply creates attachments.
terraform apply deletes attachments.
terraform apply creates attachments.
terraform apply deletes attachments.
...
...
...
use the resource: google_compute_attached_disk
terraform applyterraform applyterraform applydepends_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.
Could not find a similar issue
@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!
Most helpful comment
Does
lifecycle.ignore_changes = ["attached_disk"]ongoogle_compute_instanceresolve the issue?