terraformvirtual-machinesnapshotterraform-provider-azureazure-managed-disk

Cannot attach an existing OS disk if the VM is created from a platform, user or a shared gallery image


Creating a azure virtual machine and attaching managed disks (Created from snapshots)

Getting the error,

"Cannot attach an existing OS disk if the VM is created from a platform, user or a shared gallery image

Tried tweaks in "create option" as "Copy", "Import" etc, but none worked out

Code as follows,

        provider "azurerm" {
    subscription_id = "abcd"
    features {}
}

resource "azurerm_managed_disk" "example" {
  name                 = "managed-disk-01"
  location             = "East US"
  resource_group_name  = "RG-APP"
  storage_account_type = "Premium_LRS"
  create_option        = "Copy"
  disk_size_gb         = "127"
  source_resource_id   = azurerm_snapshot.example.id
  os_type              = "Windows"
}

resource "azurerm_snapshot" "example" {
  name                = "snapshot"
  location            = "East US"
  resource_group_name = "RG-APP"
  create_option       = "Copy"
  source_uri          = "/subscriptions/abcv/resourceGroups/RG-APP/providers/Microsoft.Compute/disk/AppA_OsDisk_1_0e"
}

data "azurerm_subnet" "subnet" {
   name = "vnet-sn-app"
   resource_group_name = "rg-network"
   virtual_network_name = "vnet"
}

resource "azurerm_network_interface" "example" {
  name                = "example-nic"
  location            = "East US"
  resource_group_name = "RG-APP"

  ip_configuration {
    name                          = "ipconfig"
    subnet_id                     = "${data.azurerm_subnet.subnet.id}"
    private_ip_address_allocation = "Dynamic"
  }
}

resource "azurerm_availability_set" "DemoAset" {
  name                = "example-aset"
  location            = "East US"
  resource_group_name = "RG-APP"
}
resource "azurerm_virtual_machine" "example" {
  name                = "example-machine-01"
  resource_group_name = "RG-APP"
  location            = "East US"
  vm_size                = "Standard_F2"
  availability_set_id = azurerm_availability_set.DemoAset.id
  delete_os_disk_on_termination = false
  delete_data_disks_on_termination = false
  network_interface_ids = [
    azurerm_network_interface.example.id,
  ]
  os_profile {
    computer_name  = "hostname"
    admin_username = "testadmin"
    admin_password = "Password1234!"
  }
  os_profile_windows_config {
    provision_vm_agent ="true"
    }

  storage_os_disk {
    name              = "myosdisk1" 
    managed_disk_id   = "${azurerm_managed_disk.example.id}"
    caching           = "ReadWrite"
    create_option     = "Attach"
  }

  storage_image_reference {
    publisher = "MicrosoftWindowsServer"
    offer     = "WindowsServer"
    sku       = "2016-Datacenter"
    version   = "latest"
  }
}

Please help in guiding to solve this issue. Thank you!


Solution

  • If you specify the create_option to Attach in the storage_os_disk, it means it will create a VM from a specialized VM image. So we don't need to specify the os_profile and storage_image_reference here.

    You could change the template like this:

    resource "azurerm_virtual_machine" "example" {
      name                = "example-machine-01"
      resource_group_name = "RG-APP"
      location            = "East US"
      vm_size             = "Standard_F2"  # this matches the old VM size
      availability_set_id              = azurerm_availability_set.DemoAset.id
      delete_os_disk_on_termination    = false
      delete_data_disks_on_termination = false
      network_interface_ids = [
        azurerm_network_interface.example.id,
      ]
    #   os_profile {
    #     computer_name  = "hostname"
    #     admin_username = "testadmin"
    #     admin_password = "Password1234!"
    #   }
      os_profile_windows_config {
        provision_vm_agent = "true"
      }
    
      storage_os_disk {
        name            = "managed-disk-01"  # this matches new created managed disk name
        managed_disk_id = azurerm_managed_disk.example.id
        caching         = "ReadWrite"
        create_option   = "Attach"
        os_type         = "Windows"    # add this os_type
      }
    
    #   storage_image_reference {
    #     publisher = "MicrosoftWindowsServer"
    #     offer     = "WindowsServer"
    #     sku       = "2016-Datacenter"
    #     version   = "latest"
    #   }
    }
    

    This will create a new Azure VM from that snapshot and you can log in to the new VM by using the old username and password from your existing OS disk.

    Verified result.

    enter image description here