使用共享图像库的托管磁盘对 Azure VM 进行 Terraform

时间:2021-07-14 14:02:11

标签: azure terraform

[类似提问]:Terraform plan destroying and replacing Azure VM upon rerun for a custom image stored in Shared Image Gallery

我正在尝试使用 TFE 和基于共享图库图像的托管磁盘创建 VM,但是在使用时:

      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
      }

磁盘未进入状态,因此无法使用新映像更新

使用时:


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
}

此错误:

错误:创建/更新托管磁盘“1imutsbdsk0101”(资源组“x-xxx-xxx-xxx-xx-xxx”)时出错:compute.DisksClient#CreateOrUpdate:发送请求失败:StatusCode=0 -- 原始错误: " Code="InvalidParameter" Message="参数 imageReference 的值无效。 Target="/subscriptions/xxxxxxxxxxxxxxxxxxxxxxxxxxxx/resourceGroups/x-xxx-xxx-xx-xx-xxx/providers/Microsoft.Compute/galleries/xxxxxxx/images/xxxxx_Windows_2019_Mutable/versions/0.xx4.xxx"

我还没有看到这个问题的任何实际答案:

1 个答案:

答案 0 :(得分:2)

我在实验室中测试了相同的场景,我的错误也是一样的。

消息:参数 imageReference 的值无效。

根本原因:因为我们尝试从 SIG 映像版本导出到磁盘,但使用了映像上不存在的 LUN 位置。

尝试从映像版本创建托管磁盘时,我们得到的参数无效,因为 LUN 编号不匹配,这两种资源都在使用。

enter image description here

解决方法:

默认情况下,每当我们从 Image 版本创建 VM 时,azure 都是使用托管磁盘创建的。

enter image description here

所以,我尝试使用共享镜像直接部署虚拟机并成功部署。 这是我的 main.tf 的一部分,用于在我定义共享映像版本位置的位置部署 VM,并在获取数据后将其用于 VM OS 磁盘。

# 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" 

  } 

} 

variables.tf 中,我定义了我在 main.tf 文件中使用的变量。

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 

} 

我的 terraform.tfvars 文件包含我的订阅 ID 和所有共享库资源名称。

tf_var_arm_subscription_id = "SubscriptionID" 

# Defining values to the variables 

galleryName                = "mysharedgallery" 

galleryImageDefinitionName = "my-image" 

galleryImageVersionName    = "0.0.1" 

我还添加了其他设置以及 vnet 等,我需要在 main.tf 中为我的虚拟机创建这些设置。

输出

enter image description here

注意:请为您的资源提供与共享图片库相同的区域。