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

Azure - Managed Datadisks gets deleted when destroying all nodes but leaves datadisk untouched when specified with target option #1701

Closed
ghost opened this issue Aug 1, 2018 · 2 comments

Comments

@ghost
Copy link

ghost commented Aug 1, 2018

This issue was originally opened by @ajaybhatnagar as hashicorp/terraform#18581. It was migrated here as a result of the provider split. The original body of the issue is below.


Terraform Version

Terraform v0.11.7
+ provider.azurerm v1.9.0
+ provider.template v1.0.0

Terraform Configuration Files

Settings used in main.tf for managed disk:

resource "azurerm_managed_disk" "datadisk" {
    count = "${var.es_node_count}"
    name = "${element(var.es_vm_name, count.index)}-datadisk-0"
    location = "${var.deployment_location}"
    resource_group_name = "${var.vm_resource_group}"
    storage_account_type = "${var.azure_datadisk_sku}"
    disk_size_gb  = "${element(split("-",lookup(var.node_settings,"${element(var.es_vm_name,count.index)}")),0)}"
    create_option = "Empty" 
}

resource "azurerm_virtual_machine" "elasticsearch" {
    count                         = "${var.es_node_count}"
    name                          = "${element(var.es_vm_name,count.index)}"
    delete_os_disk_on_termination = "true"
    delete_data_disks_on_termination = "false"
    location                      = "${var.deployment_location}"
    resource_group_name           = "${var.vm_resource_group}"
    network_interface_ids         = ["${element(azurerm_network_interface.elasticsearch.*.id,count.index)}"]
    vm_size                       = "${element(split("-",lookup(var.node_settings,"${element(var.es_vm_name,count.index)}")),1)}"
    availability_set_id           = "${azurerm_availability_set.elasticsearch.id}"


    storage_image_reference {
         id = "${element(azurerm_image.osdisk.*.id, count.index)}"
    }

    storage_os_disk {
        name          = "${element(var.es_vm_name,count.index)}-os"
        caching       = "ReadWrite"
        create_option = "FromImage"
        disk_size_gb  = "${var.vm_disk_size}"
        managed_disk_type = "${var.azure_osdisk_sku}"        
    }

    storage_data_disk {
         name          = "${element(azurerm_managed_disk.datadisk.*.name, count.index)}"
         managed_disk_id = "${element(azurerm_managed_disk.datadisk.*.id, count.index)}"    
         disk_size_gb  = "${element(split("-",lookup(var.node_settings,"${element(var.es_vm_name,count.index)}")),0)}"
         create_option = "Attach" 
         lun           =  "0"
    }
...

Debug Output

NONE

Crash Output

Expected Behavior

Destroying all VMs should not delete associated datadisks.

Actual Behavior

 All OS and Datadisks get deleted when issuing command terraform destroy.  When issuing command like terraform destroy -target=xxxx , datadisk remains intact only OS disk gets deleted.

Steps to Reproduce

1.  Create multiple VMs in Azure with managed datadisks attached using Terraform apply`
2.  When all VMs are up and all datadisks attached , destroy all VMs with  `terraform destroy`
 Result will be all OS and datadisks deleted with all data lost. Expect datadisks should not be deleted. Disks not visble in console Disk section.
3. Create all VMs again with terraform apply.
4. Destroy any one VM with command like terraform destroy -target=xxxx. Result will be VM is destroyed, OS disk is deleted and datadisk will still be intact in Azure console (Disk section).
5. Relaunch VM wth terraform apply -target=xxxx , original datadisk contents attached again and is reusable. 

Additional Context

References

@tombuildsstuff
Copy link
Contributor

hi @ajaybhatnagar

Thanks for opening this issue :)

In the Terraform Configuration referenced above the azurerm_managed_disk resource is specified, meaning the Data Disks are managed by Terraform and will be destroyed when terraform destroy is ran. It's worth noting that the delete_data_disks_on_termination flag is specific to Data Disks managed within the VM Resource (but these disks would still be deleted via the Resource Group being destroyed).

If these data disks need to live long term - as you've mentioned in the comment above, one option is to use taint to only destroy the Virtual Machine - the other would be to provision them in a separate configuration and then use the azurerm_managed_disk data source to reference them. It's worth noting that deleting the parent Resource Group will delete any data disks within it by design of Azure.

As this behaviour is intentional (since these resources are managed by Terraform) - I'm not sure there's anything we can do in this issue and as such I'm going to close this issue for the moment.

Thanks!

@ghost
Copy link
Author

ghost commented Mar 30, 2020

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 30, 2020
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

2 participants