How to Import an Azure VM Data Disk Attachment into Terraform
terraback azure import --method bulk. Terraback writes the matching azurerm_virtual_machine_data_disk_attachment resource block and the Terraform 1.5+ import block for you, so you do not have to run terraform import by hand. The import ID is the virtual machine ID with a synthetic dataDisks segment (<vm_resource_id>/dataDisks/<disk_name>).Import Azure VM Data Disk Attachment with Terraback (recommended)
Terraback reverse-engineers your live infrastructure: it reads the Azure Compute resource with read-only credentials, generates the HCL, and produces the exact import block. Two commands take you from a live Azure VM Data Disk Attachment to managed Terraform.
Scan your Azure account
terraback scan all azure --subscription-id YOUR_IDGenerate import blocks and import into state
terraback azure import --method bulkThe Terraform import block
Terraback emits a Terraform 1.5+ import block like the one below. Because the block lives in your configuration, the import is reviewable in a pull request and repeatable across environments.
import {
to = azurerm_virtual_machine_data_disk_attachment.data
id = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/prod-rg/providers/Microsoft.Compute/virtualMachines/app-vm/dataDisks/app-data-disk"
}Example azurerm_virtual_machine_data_disk_attachment configuration
Here is a realistic Azure VM Data Disk Attachment block. Terraback generates a fuller version from your actual resource attributes; this is a minimal, valid starting point.
resource "azurerm_virtual_machine_data_disk_attachment" "data" {
managed_disk_id = azurerm_managed_disk.app_data.id
virtual_machine_id = azurerm_linux_virtual_machine.app.id
lun = 0
caching = "ReadWrite"
}Gotchas when importing a Azure VM Data Disk Attachment
- The import ID is synthetic: it is the virtual machine resource ID with /dataDisks/<disk name> appended, where the trailing name is the managed disk's name (not a real child ARM resource).
- The attachment is a separate resource from both the VM and the disk; the managed disk imports as azurerm_managed_disk and the VM as azurerm_linux_virtual_machine or azurerm_windows_virtual_machine.
- Do not also declare the disk inside a storage_data_disk / data_disk block on the VM, or Terraform will conflict over the attachment.
- lun must be unique per VM and is immutable; changing it detaches and reattaches the disk.
Doing it manually with terraform import
The native approach is to write the azurerm_virtual_machine_data_disk_attachment block by hand, then run terraform import azurerm_virtual_machine_data_disk_attachment.example <import-id> for every resource, one at a time. That works for a handful of resources, but it does not scale: you author all the HCL yourself and repeat the command for each item. Terraback generates the HCL and the import blocks for your whole account in one pass.
Import other Azure resources
Import your whole Azure account in minutes
Terraback scans 80+ Azure resource types and emits clean Terraform plus import blocks, running locally with read-only credentials. $499 once, no SaaS.