Managing Azure VM Disks and Extensions with Terraform: Handling Dependency and State Removal
When managing Azure Virtual Machines (VMs) with Terraform, especially Windows VMs with extensions and attached data disks, it’s common to encounter challenges around the order of resource destruction and avoiding unnecessary delays. This post summarizes practical solutions to these issues, with example commands and configuration snippets derived from a real-world scenario.
The Problem: Terraform Deletes Extensions and Disks Before the VM
By default, Terraform tracks each Azure VM extension
(azurerm_virtual_machine_extension
) and each data disk attachment
(azurerm_virtual_machine_data_disk_attachment
) as independent resources. When
running terraform destroy
, Terraform deletes these extensions and disk
attachments one by one before deleting the VM.