Search code examples
terraformvirtual-machineazure-rm

Problems in creating vm from shared image in Azure


Cross posted in HashiCorp Forum: https://discuss.hashicorp.com/t/problems-in-creating-vm-from-shared-image/52437

Here is the code I am using for creating a Windows VM from Shared Image gallery:

resource "azurerm_virtual_machine" "vm" {
  name                  = "myvm" 
  location              = "East US"
  resource_group_name   = "MyResGroup"
  vm_size               = "Standard_D4as_v5"

  network_interface_ids =  [azurerm_network_interface.nic.id]

  storage_image_reference {
    id = "/subscriptions/4a4.....673/resourceGroups/rg-vm-images/providers/Microsoft.Compute/galleries/vmsgallary/images/testvmimage"
  }

  storage_os_disk {
    name               = "my_os_storage_disk"
    caching            = "ReadWrite"
    create_option      = "FromImage"
    managed_disk_type  = "Standard_LRS"
  }

  os_profile {
    computer_name  = "myvm"
    admin_username = "azureuser"
    admin_password = "MyComplexPassword"
  }

  os_profile_windows_config {
  }
}

It gives the following error (after 40/50 mins or so):

OS Provisioning for VM ‘xxx’ did not finish in the allotted time. However, the VM guest agent was detected running. This suggests the guest OS has not been properly prepared to be used as a VM image (with CreateOption=FromImage). To resolve this issue, either use the VHD as is with CreateOption=Attach or prepare it properly for use as an image:
    
    Instructions for Windows: https://azure.microsoft.com/documentation/articles/virtual-machines-windows-upload-image/
    Instructions for Linux: https://azure.microsoft.com/documentation/articles/virtual-machines-linux-capture-image/
    Deleting and recreating the virtual machine may resolve the issue.

Here is the information of the shared image: enter image description here

OS type: Windows OS state: Generalized

enter image description here

Note that I have been successful in creating a Linux VM using the same code (except those that are only applicable to Linux).

terraform version: 1.3.9 azurerm provider: 3.46.0


Solution

  • Check the following where I can create windows VM successfully:

    I have used resource "azurerm_windows_virtual_machine"

    resource "azurerm_shared_image_gallery" "example" {
      name                = "exampleimagegallery"
       resource_group_name = data.azurerm_resource_group.example.name
      location            = "eastus"
      description         = "Shared images and things."
    
      tags = {
        Hello = "There"
        World = "Example"
      }
    }
    
    resource "azurerm_shared_image" "example" {
      name                = "kavyamyimage"
      gallery_name        = azurerm_shared_image_gallery.example.name
       resource_group_name = data.azurerm_resource_group.example.name
      location            = "eastus"
      os_type             = "Windows"
    
      identifier {
        publisher = "MicrosoftWindowsServer"
        offer     = "WindowsServer"
        sku       = "2016-Datacenter"
      }
    
    
    data "azurerm_shared_image" "existing" {
      name                = "kavyamyimage"
      gallery_name        = azurerm_shared_image_gallery.example.name
      resource_group_name = data.azurerm_resource_group.example.name
    }
    
    
    output "sharedimageid" {
      value = data.azurerm_shared_image.existing.id
    }
    resource "azurerm_network_interface" "example" {
      name                = "kaexample-nic"
     location            = data.azurerm_resource_group.example.location
      resource_group_name = data.azurerm_resource_group.example.name
      ip_configuration {
        name                          = "example-ip-config"
        subnet_id                     = azurerm_subnet.example.id
        private_ip_address_allocation = "Dynamic"
      }
    }
    
    resource "azurerm_virtual_network" "example" {
      name                = "kaexample-virtual-network"
      address_space       = ["10.0.0.0/16"]
      location            = data.azurerm_resource_group.example.location
      resource_group_name = data.azurerm_resource_group.example.name
    }
    
    resource "azurerm_subnet" "example" {
      name                 = "kkexample-subnet"
      address_prefixes     = ["10.0.0.0/24"]
      virtual_network_name = azurerm_virtual_network.example.name
      resource_group_name  = data.azurerm_resource_group.example.name
    }
    
    
    First I executed above with terraform apply and then deployed as below, as the vm required shared image to be created prior and if they are parallelly trying to be created and the vm depends on image creation , there will be conflicts.
    
    Else, add depends _on parameter to the resource block such that VM that depends on image creation first .
    
    resource "azurerm_windows_virtual_machine" "example" {
      name                = "example-machine"
      resource_group_name = data.azurerm_resource_group.example.name
      location            = "eastus"
      size                = "Standard_F2"
      admin_username      = "adminuser"
      admin_password      = "P@$$w0rd1234!"
      network_interface_ids = [
        azurerm_network_interface.example.id,
      ]
      
    
      os_disk {
        caching              = "ReadWrite"
        storage_account_type = "Standard_LRS"
      }
    
      source_image_reference {
        publisher = "MicrosoftWindowsServer"
        offer     = "WindowsServer"
        sku       = "2016-Datacenter"
        version   = "latest"
      }
      //source_image_id = data.azurerm_shared_image.existing.id
     depends_on = [
        data.azurerm_shared_image.existing
      ]
      
    }
    

    enter image description here

    enter image description here

    Reference: azurerm_virtual_machine | terraform registry