Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

azurerm_virtual_machine_data_disk_attachment shows incorrect changes during plan #1784

Closed
rohrerb opened this issue Aug 16, 2018 · 6 comments

Comments

@rohrerb
Copy link
Contributor

rohrerb commented Aug 16, 2018

Terraform Version

Terraform v0.11.8
+ provider.azurerm v1.13.0

Affected Resource(s)

azurerm_virtual_machine_data_disk_attachment

Terraform Configuration Files

Terraform files can be found here - https://gist.github.com/rohrerb/4b449b778a6d902014f580a34e7390df

File format as follows:

  • module/linux/main.tf
  • module/linux/variables.tf
  • main.tf

Debug Output

Expected Behavior

The first VM shouldn't have any changes when we increment the VM count.

Actual Behavior

The first [0] VM's azurerm_virtual_machine_data_disk_attachment resource is prompting for change.

Steps to Reproduce

  1. terraform init
  2. terraform apply
  3. After the creation you will have a VM with a disk which is attached.
  4. Increment instance_count from 1 to 2 in main.tf
  5. terraform apply

You will see that the first VM disk attach is wanting to be modified.

-/+ module.x11x.azurerm_virtual_machine_data_disk_attachment.data_disk_attach[0] (new resource required)
      id:                                                                 "/subscriptions/55546ce0-6c1c-40d0-98dd-007c68adfcd6/resourceGroups/TUS1E11-Web/providers/Microsoft.Compute/virtualMachines/tus1e11x11x001/dataDisks/tus1e11x11x001-datadisk1" => <computed> (forces new resource)
      caching:                                                            "ReadOnly" => "ReadOnly"
      create_option:                                                      "Attach" => "Attach"
      lun:                                                                "0" => "0"
      managed_disk_id:                                                    "/subscriptions/55546ce0-6c1c-40d0-98dd-007c68adfcd6/resourceGroups/TUS1E11-Web/providers/Microsoft.Compute/disks/tus1e11x11x001-datadisk1" => "${element(azurerm_managed_disk.data_disk.*.id, count.index)}" (forces new resource)
      virtual_machine_id:                                                 "/subscriptions/55546ce0-6c1c-40d0-98dd-007c68adfcd6/resourceGroups/TUS1E11-Web/providers/Microsoft.Compute/virtualMachines/tus1e11x11x001" => "${element(azurerm_virtual_machine.vm.*.id, ceil((count.index + 1) * 1.0 / var.data_disk_count) - 1)}" (forces new resource)
      write_accelerator_enabled:                                          "false" => "false"

The interesting part is that it will go away if you just apply the 2nd vm & disk creation.

  1. terraform apply -target module.x11x.azurerm_managed_disk.data_disk[1] -target module.x11x.azurerm_virtual_machine.vm[1]
  2. terraform plan

The module.x11x.azurerm_virtual_machine_data_disk_attachment.data_disk_attach[0] is no longer prompting. I did nail it down to the managed_disk_id which is causing the resource mod, but no clue why yet.

Any ideas?

FYI - @tombuildsstuff

@jcmaasb
Copy link

jcmaasb commented Aug 17, 2018

I have not thoroughly reviewed your code, but I think your problem is related to the "element ()" function

Check these threads:
hashicorp/terraform#3449
hashicorp/terraform#17014

@rohrerb
Copy link
Contributor Author

rohrerb commented Aug 20, 2018

Hi @jcmaasb - Not related to either of the issues you mentioned.

@tombuildsstuff
Copy link
Contributor

hey @rohrerb

Thanks for opening this issue - apologies for the delayed response here!

There's a couple of known issues with count in Terraform 0.11 in conjunction with the splat operator and element - for example when the number of items in a list changes. My understanding is that these may be fixed as a part of Terraform 0.12, but in the interim it may be possible to work around the issue with the element/splat/count issue by switching to use the [] syntax rather than element() e.g.

resource "azurerm_virtual_machine_data_disk_attachment" "data_disk_attach" {
  count =   "${var.instance_count == 0 ? 0 : var.instance_count * var.data_disk_count}"
  virtual_machine_id    = "${azurerm_virtual_machine.vm[ceil((count.index + 1) * 1.0 / var.data_disk_count) - 1)].id}"
  managed_disk_id    = "${azurerm_managed_disk.data_disk[count.index].id}"
  lun                = "${floor((count.index +1) / ceil((count.index + 1) * 1.0 / var.data_disk_count)) - 1}"
  caching            = "ReadOnly"
  create_option      = "Attach"
}

Would you be able to take a look and see if that works for you?

Thanks!

@rohrerb
Copy link
Contributor Author

rohrerb commented Sep 5, 2018

Hi @tombuildsstuff,

Thank you for the suggestion. That fixed my issue...

Looking forward to 0.12 :D

@tombuildsstuff
Copy link
Contributor

@rohrerb glad to hear that's now working for you :) Since this has now been resolved I'm going to close this issue for the moment.

@tombuildsstuff tombuildsstuff added the service/disk-attachments Virtual Machine Disk Attachments label Dec 10, 2018
@ghost
Copy link

ghost commented Mar 5, 2019

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 👉 hashibot-feedback@hashicorp.com. Thanks!

@ghost ghost locked and limited conversation to collaborators Mar 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants