本文介绍了Terraform Azure VM 使用托管磁盘和共享映像库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[类似问题]:

解决方法:

默认情况下,当我们从映像版本创建 VM 时,它是使用托管磁盘创建的.

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

#Informationaboutexistingsharedimageversion数据azurerm_shared_image_version"asgi"{名称=var.galleryImageVersionNameimage_name=var.galleryImageDefinitionName画廊名称=var.画廊名称resource_group_name=你的共享镜像版本所在的资源组!!"}#VirtualMachine-Windows资源azurerm_windows_virtual_machine"avm-01"{名称=local.vmName计算机名称=我的虚拟机";resource_group_name=azurerm_resource_group.arg-01.name # 新资源组,我们在其中使用共享图像库创建所有资源.location=azurerm_resource_group.arg-01.location #与镜像版本相同.尺寸=标准_A1";admin_username=var.admin用户名admin_password=var.adminPasswordnetwork_interface_ids=[azurerm_network_interface.anic-01.id]source_image_id=data.azurerm_shared_image_version.asgi.id操作系统磁盘{缓存=读写"storage_account_type=标准_LRS";}}

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

提供者azurerm"{特征 {}subscription_id=var.tf_var_arm_subscription_id}变量tf_var_arm_subscription_id"{类型=字符串描述=资源组的变量";}变量resourceGroupName"{类型=字符串默认=tf-rg";描述=用于此部署的资源组."}可变的位置"{类型=字符串默认=WestUS2";description=输入所有资源的位置."}变量galleryName"{类型=字符串描述=共享图像库的名称."}变量galleryImageDefinitionName"{类型=字符串描述=图像定义的名称."}变量galleryImageVersionName"{类型=字符串}

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

tf_var_arm_subscription_id=SubscriptionID"#为变量定义值画廊名称=我的共享画廊";galleryImageDefinitionName=我的图像";GalleryImageVersionName=0.0.1"

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

输出

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

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

解决方案

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 .

WorkAround:

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

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

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

这篇关于Terraform Azure VM 使用托管磁盘和共享映像库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 14:48