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

2024-03-17

[类似问题]:Terraform 计划在重新运行共享图像库中存储的自定义图像时销毁并替换 Azure VM https://stackoverflow.com/questions/64050815/terraform-plan-destroying-and-replacing-azure-vm-upon-rerun-for-a-custom-image-s

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

      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 的值无效。"目标=“/订阅/xxxxxxxxxxxxxxxxxxxxxxxxxxx/resourceGroups/x-xxx-xxx-xx-xx-xxx/providers/Microsoft.Compute/galleries/xxxxxxx/images/xxxxx_Windows_2019_Mutable/versions/0.xx4.xxx”

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


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

Message:参数imageReference的值无效。

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

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

解决方法:

默认情况下,在 azure 中,每当我们从映像版本创建 VM 时,都会使用托管磁盘创建它。

因此,我尝试使用共享映像直接部署虚拟机,并且部署成功。 这是我的一部分main.tf用于部署虚拟机,我在其中定义了共享映像版本位置,并在获取数据后将其用于虚拟机操作系统磁盘。

# 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,我已经定义了我在我的中使用的变量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文件有我的订阅 ID 和所有共享图库资源名称。

tf_var_arm_subscription_id = "SubscriptionID" 

# Defining values to the variables 

galleryName                = "mysharedgallery" 

galleryImageDefinitionName = "my-image" 

galleryImageVersionName    = "0.0.1" 

我还添加了其他设置,例如虚拟网络等我需要在我的虚拟机中创建它main.tf .

Output

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用共享图像库使用托管磁盘的 Terraform Azure VM 的相关文章

随机推荐