1

I have created standard VM in Azure which I have customized with different software on it. Then I have created an Azure Image from this VM and successfully imported that into the Azure Compute Gallery as an Image Version, so that I can deploy new VM based on the customized image faster with same configuration. I am able to deploy VMs of this nature using Terraform (with a single OS disk only), but now the source Image Version has multiple Data Disks included as well. Is it possible to deploy this customized image using Terraform including the Data Disks? And name each data disk with a custom name?

data "azurerm_shared_image_version" "existing" {
  provider            = azurerm.compute_gallery
  name                = var.compute_gallery.version
  image_name          = var.compute_gallery.definition
  gallery_name        = var.compute_gallery.name
  resource_group_name = var.compute_gallery.resource_group_name
}

resource "azurerm_windows_virtual_machine" "windows_virtual_machine" {
  name                  = var.virtual_machines[each.key].name
  location              = var.location
  resource_group_name   = var.resource_group_name
  admin_password        = "<__LOCALADMINPASSWORD__>" 
  admin_username        = "<__LOCALADMINUSERNAME__>"
  size                  = var.vmsize
  network_interface_ids = [azurerm_network_interface.network_interface.id]
  timezone              = "GMT Standard Time"
  provision_vm_agent    = true
  source_image_id       = data.azurerm_shared_image_version.existing.id
 
  os_disk {
    caching              = "ReadWrite"
    storage_account_type = "Standard_LRS"
    name                 = lower(join("", ["disk-", var.virtual_machine_name, "-os"]))
  }

Thank you

4
  • Why don't you attach disks directly to VM? why it needs to be an image? Images are for booting OSes. Data disks don't boot, they just act as storage. You create VM & attach disks using azurerm_virtual_machine_data_disk_attachment? Commented Mar 25, 2022 at 7:16
  • Thanks for the comment. The gallery image is built for a cluster node, and contains 5 data disks, all configured with correct drive letters, block unit size, formatted, and initialized with a small amount of base data. By managing this complete VM as a single Gallery Image Version, means easy to replicate a single resource across multiple regions, and doesn't require additional config of the VM after a deployment - as soon as deployed, VM is ready to go. I can successfully achieve this with PowerShell, but Terraform does not seem to support an image that contains data disks = problem. Commented Mar 25, 2022 at 11:24
  • 2
    NOTE - Terraform resource azurerm_windows_virtual_machine_scale_set supports deploying from a gallery image version with data disk. I have this working successfully, however a normal VM using the azurerm_windows_virtual_machine resource does not support the data disks weirdly.... Commented Mar 25, 2022 at 11:42
  • 1
    I found Hashicorp GitHub issue 6117 that is tracking the resolution of this. Commented Mar 28, 2022 at 8:10

1 Answer 1

0

Won´t solve your case but maybe others. If you remove the data disks it is working. This seems to happen because of DarrenS issue.

Nevertheless for me I finally even realized that i created a datadisk when trying to automate it with terraform. Seems to be a default setting (should be a comment but i have to less reputation for commenting)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.