2
votes

[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:

1

1 Answers

2
votes

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 .