Linux Virtual Machine(s) and data-disk

I’m trying to provision some linux virtual machines with an extra managed data-disk. I want to partition the disk first time, and keep the data & mount the disk in case the VM is replaced.

Using the Terraform resources:

  • azurerm_linux_virtual_machine
  • azurerm_managed_disk
  • azurerm_virtual_machine_data_disk_attachment

First I tried provisioning the disk with the cloud-init disk-modules. I managed to modify the included epehemeral disk, but the managed disk was not found, since the disk was not finished attaching before the disk-module ran.

I tried several ways to delay the module, but had no success. Ref. userdata file for this.

Then I tried partitioning and mounting the disk with “write_files” & “runcmd”. This works, and I can replace the VM & keep the data, etc. But it seems so overly complex to include in the provisioning for such a small thing. Ref. userdata for this

Are there any documented/better ways of solving this? Ideally I’d like it to work using the disk-module. I.e. delay starting the VM untill disk is attached. I believe this is the case when using the superseeded “azurerm_virtual_machine” + the “storage_data_disk” block.