2

[Similar ask] : Terraform plan destroying and replacing Azure VM upon rerun for a custom image stored in Shared Image Gallery

I am trying to create VMs using TFE and managed disks based on a Shared image gallery image however when using :

      storage_image_reference {
        id = var.latest-image-id
      }
      
      storage_os_disk {
        name                = var.storage_os_disk_name
        create_option       = "FromImage"
        managed_disk_type   = var.managed_disk_type 
        disk_size_gb        = var.disk_size_gb
        os_type             = var.os_type
      }

The disk does not go into the state and therefore cannot be updated with a new image

When using :


resource "azurerm_managed_disk" "vmdisk" {
    name                 = var.storage_os_disk_name
    location             = var.location
    resource_group_name  = var.resource_group_name
    storage_account_type = var.managed_disk_type
    create_option        = "FromImage"
    image_reference_id   = var.latest-image-id
    disk_size_gb         = var.disk_size_gb
    tags                 = var.common_tags
}
resource "azurerm_virtual_machine" "vm" {
    storage_os_disk {
    name              = var.storage_os_disk_name
    create_option     = "Attach"
    managed_disk_id   = azurerm_managed_disk.vmdisk.id
}

This errors with :

Error: Error creating/updating Managed Disk "1imutsbdsk0101" (Resource Group "x-xxx-xxx-xxx-xx-xxx"): compute.DisksClient#CreateOrUpdate: Failure sending request: StatusCode=0 -- Original Error: Code="InvalidParameter" Message="The value of parameter imageReference is invalid." Target="/subscriptions/xxxxxxxxxxxxxxxxxxxxxxxxxxxx/resourceGroups/x-xxx-xxx-xx-xx-xxx/providers/Microsoft.Compute/galleries/xxxxxxx/images/xxxxx_Windows_2019_Mutable/versions/0.xx4.xxx"

I haven't seen any actual answer to this issue:

Marcus
  • 21
  • 2

1 Answers1

0

I tested the same scenario in my lab and the error is same for me as well.

Message: The value of parameter imageReference is invalid.

Root Cause: As we tried to export from a SIG Image version to a disk but used a LUN position that does not exist on the image.

When trying to create a managed disk from image version , we are getting parameters invalid as the LUN no’s are not matching which are being used by both the resources .

enter image description here

WorkAround:

By default in azure whenever we create a VM from Image version, it is created with managed disk.

enter image description here

So , I tried deploying the VM directly using the shared imaged and it was successfully deployed. This is a part of my main.tf for deploying the VM where I have defined the shared imaged version location and after getting the data I have used it for the VM OS disk .

# Information about existing shared image version 

data "azurerm_shared_image_version" "asgi" { 

  name                = var.galleryImageVersionName 

  image_name          = var.galleryImageDefinitionName 

  gallery_name        = var.galleryName 

  resource_group_name = "the resource group where your shared Image Version is!!" 

} 
 
# Virtual Machine - Windows 

resource "azurerm_windows_virtual_machine" "avm-01" { 

  name                  = local.vmName 

  computer_name         = "myVm" 

  resource_group_name   = azurerm_resource_group.arg-01.name # new resource group where we are creating all the resources using shared image gallery. 

  location              = azurerm_resource_group.arg-01.location #same as the image version. 

  size                  = "Standard_A1" 

  admin_username        = var.adminUsername 

  admin_password        = var.adminPassword 

  network_interface_ids = [azurerm_network_interface.anic-01.id] 

  source_image_id       = data.azurerm_shared_image_version.asgi.id 

  os_disk { 

    caching              = "ReadWrite" 

    storage_account_type = "Standard_LRS" 

  } 

} 

In variables.tf , I have defined the variables which I am using in my main.tf file .

provider "azurerm" { 

  features {} 

  subscription_id = var.tf_var_arm_subscription_id 

} 

variable "tf_var_arm_subscription_id" { 

    type = string 

    description = "Variable for our resource group" 

} 

variable "resourceGroupName" { 

  type        = string 

  default     = "tf-rg" 

  description = "Resource Group for this deployment." 

} 

variable "location" { 

  type        = string 

  default     = "West US 2" 

  description = "Enter the location for all resources." 

} 

variable "galleryName" { 

  type        = string 

  description = "Name of the Shared Image Gallery." 

} 

variable "galleryImageDefinitionName" { 

  type        = string 

  description = "Name of the Image Definition." 

} 

variable "galleryImageVersionName" { 

  type        = string 

} 

My terraform.tfvars file has my subscriptionID and all the shared gallery resources name.

tf_var_arm_subscription_id = "SubscriptionID" 

# Defining values to the variables 

galleryName                = "mysharedgallery" 

galleryImageDefinitionName = "my-image" 

galleryImageVersionName    = "0.0.1" 

I have also added the other settings as well like vnet etc. which I need to create for my vm in my main.tf .

Output

enter image description here

Note : Please provide the same region for your resources as you have given to your shared image gallery .

Ansuman Bal
  • 9,705
  • 2
  • 10
  • 27
  • Are you able to redeploy with a new image version ? without having to destroy version is the Disk now in the Terraform state? – Marcus Jul 20 '21 at 08:07
  • Hello @Marcus, If I understand you correctly then, yes, we can create new image version from a different VM image without destroying the old version and both the image versions are stored in the terraform state. But if we are trying to deploy the same VM again with the new image version then its not possible without destroying it as it is not supported . – Ansuman Bal Jul 20 '21 at 10:31
  • Microsoft does not support an upgrade of the operating system of a Microsoft Azure virtual machine. Instead, you should create a new Azure virtual machine that is running the supported version of the operating system that is required and then migrate the workload. please refer this : https://support.microsoft.com/en-us/help/2721672/microsoft-server-software-support-for-microsoft-azure-virtual-machines – Ansuman Bal Jul 20 '21 at 10:33
  • This is the problem I am facing, If we deploy many VMs using Terraform apply then when we have a new custom image that may affect one or more VMs we need to be able to do Terraform apply which may destroy the VM and recreate however we cannot we need to do a full terraform destroy which will wipe all VMs. – Marcus Jul 20 '21 at 12:06
  • 1
    Yes , Understood . But that is not supported on azure virtual machine. You need to create another VM using new image separately and then migrate the workload from old to new VM. similar thread: https://stackoverflow.com/questions/68358393/is-it-possible-to-change-the-image-running-on-a-virtual-machine-in-azure-without – Ansuman Bal Jul 20 '21 at 12:45