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

标签 azure terraform

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

我尝试使用 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/gallery/xxxxxxx/images/xxxxx_Windows_2019_Mutable/versions/0.xx4.xxx”

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

最佳答案

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

消息:参数 imageReference 的值无效。

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

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

enter image description here

解决方法:

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

enter image description here

因此,我尝试使用共享镜像直接部署虚拟机,并且部署成功。 这是我的 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" 

  } 

} 

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

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

关于azure - 使用共享图像库使用托管磁盘的 Terraform Azure VM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68379563/

相关文章:

azure - 逻辑应用连接器策略引发 "Property Immutable"错误

kubernetes - 如何在 terraform 中导入生成的 Kubernetes 集群的命名空间

sql-server - 使用 terraform 添加 azure SQL Server 登录名

azure - 在 terraform 代码中使用多个版本的 azurerm

terraform - 如何在Terraform中使用动态资源名称?

centos - 使用 libvirt_volume.source 的 URL 时如何指定 HTTP 身份验证(用户、密码)

Azure 模拟器 - 无法加载文件或程序集

Azure Board 应用程序与 MS 团队集成 - 当我添加 Azure 应用程序时,回顾板在 MS Teams 中不可见

python - Azure 函数 (python) 将输出写入输出 blob 路径的动态命名

azure - 创建可以处理多个队列的 Azure.Storage.Queues 客户端