繁体   English   中英

使用 terraform 创建 Azure VM 时是否需要版本?

[英]Is version mandatory while creating an Azure VM using terraform?

因此,从过去 3 周开始,我一直在使用 terraform,并一直在尝试使用它在我们的 Azure 帐户中创建自托管的 GitHub Actions 运行器。

我们在 Azure Compute Gallery 中有一个共享的 Windows VM 映像,我计划将其用作 GA 运行器的基础映像。 我注意到这些共享的 Windows VM 映像通常没有附加任何版本,它们只是附加了发布者、报价和 SKU。

我还通过从 VM 创建新映像来检查是否有人错过了将版本附加到 VM 的验证,但没有共享映像实际上没有附加版本。

是的,他们确实有版本,但它没有像 Microsoft 平台映像那样附加。

共享图像示例:

在此处输入图像描述

现在我发现在 terraform 中,可以同时使用azurerm_windows_virtual_machineazurerm_virtual_machine资源来创建跑步者。

我用它们来测试跑步者的创建,下面是使用的 terraform 代码:

data "azurerm_shared_image" "win19_gold_image" {
  provider            = azurerm.gi
  name                = "Windows-2019_base"
  gallery_name        = data.azurerm_shared_image_gallery.cap_win_gold_image_gallery.name
  resource_group_name = "gi-rg"
}

resource "azurerm_virtual_machine" "win_runners_gold_image_based" {
  provider              = azurerm.og
  name                  = "ga-win-gold-1"
  location              = "East US"
  count                 = "1" # if I need to increase the number of VMs.
  resource_group_name   = data.azurerm_resource_group.dts_rg.name
  network_interface_ids = [azurerm_network_interface.azure_win_runner_gold_nic[count.index].id,]
  vm_size               = "Standard_D4ads_v5"

  delete_os_disk_on_termination = true
  delete_data_disks_on_termination = true

  storage_image_reference {
    publisher = data.azurerm_shared_image.win19_gold_image.identifier[0].publisher
    offer     = data.azurerm_shared_image.win19_gold_image.identifier[0].offer
    sku       = data.azurerm_shared_image.win19_gold_image.identifier[0].sku
    # Here I get the error: Error: compute.VirtualMachinesClient#CreateOrUpdate: Failure sending request: StatusCode=400 -- Original Error: Code="InvalidParameter" Message="The value of parameter imageReference.version is invalid." Target="imageReference.version"
  }
  
  storage_os_disk {
    name              = "ga-win-gold-os-disk-1"
    caching           = "None"
    create_option     = "FromImage"
    managed_disk_type = "StandardSSD_LRS"
  }
  
  os_profile {
    computer_name  = "ga-win-gold-1"
    admin_username = "svc"
    admin_password = var.WINDOWS_ADMIN_PASS
  }
  
  os_profile_windows_config {
    enable_automatic_upgrades = true
    provision_vm_agent        = true
  }
  
  storage_data_disk {
    name              = "ga-win-gold-data-disk-1"
    caching           = "None"
    create_option     = "Empty"
    disk_size_gb      = var.disk_size_gb
    lun               = 0
    managed_disk_type = "StandardSSD_LRS"
  }
}

或者

data "azurerm_shared_image" "win19_gold_image" {
  provider            = azurerm.gi
  name                = "Windows-2019_base"
  gallery_name        = data.azurerm_shared_image_gallery.cap_win_gold_image_gallery.name
  resource_group_name = "gi-rg"
}

resource "azurerm_windows_virtual_machine" "azure_win_runner" {
  provider                          = azurerm.og
  name                              = "vm-github-actions-win-${count.index}"
  resource_group_name               = data.azurerm_resource_group.dts_rg.name
  location                          = "East US"
  size                              = var.windows-vm-size
  count                             = "${var.number_of_win_az_instances}"
  network_interface_ids             = [
    azurerm_network_interface.azure_win_runner_nic[count.index].id,
  ]
  computer_name                     = "vm-ga-win-${count.index}"
  admin_username                    = var.windows-admin-username
  admin_password                    = var.WINDOWS_ADMIN_PASS

  os_disk {
    name = "vm-github-actions-win-${count.index}-os-disk"
    caching              = "None"
    storage_account_type = "StandardSSD_LRS"
  }

  source_image_reference {
    publisher = data.azurerm_shared_image.win19_gold_image.identifier[0].publisher
    offer     = data.azurerm_shared_image.win19_gold_image.identifier[0].offer
    sku       = data.azurerm_shared_image.win19_gold_image.identifier[0].sku
    version   = data.azurerm_shared_image.win19_gold_image.identifier[0].version # says this object does not have a version attached to it.
    # or version = "latest" or any other correct version string will throw error at time of apply that such a version does not exist.
  }

  enable_automatic_updates = true
  provision_vm_agent       = true
}

如果我使用的是azurerm_virtual_machine ,那么如果我忽略storage_image_reference中的版本,我会收到错误消息:

Error: compute.VirtualMachinesClient#CreateOrUpdate: Failure sending request: StatusCode=400 -- Original Error: Code="InvalidParameter" Message="The value of parameter imageReference.version is invalid." Target="imageReference.version"

如果我添加版本,那么我会收到错误

Error: Unsupported attribute. 
This object does not have an attribute named "version".

使用azurerm_windows_virtual_machine时,如果我删除版本参数 terraform 抱怨需要版本,并且当提供诸如1.0.0latest之类的刺痛时,在应用(terraform apply)时它会抱怨这样的版本不存在。

如果我从data.azurerm_shared_image.cap_win19_gold_image中提取版本,它会抱怨这个对象没有版本。

如果版本是强制性的,但如果版本不适用于 azure 共享图像,我对如何使用 terraform 使用共享图像创建 VM 感到困惑。 请告知我错过了什么?

任何帮助,将不胜感激。

谢谢, 塞卡

似乎得到了你需要使用另一个资源 [1] 和另一个数据源 [2] 的图像版本:

data "azurerm_image" "win19_gold_image" {
  name                = "Windows-2019_base"
  resource_group_name = "gi-rg"
}

resource "azurerm_shared_image_version" "win19_gold_image" {
  name                = "0.0.1"
  gallery_name        = data.azurerm_shared_image.win19_gold_image.gallery_name
  image_name          = data.azurerm_shared_image.win19_gold_image.name
  resource_group_name = data.azurerm_shared_image.win19_gold_image.resource_group_name
  location            = data.azurerm_shared_image.win19_gold_image.location
  managed_image_id    = data.azurerm_image.win19_gold_image.id
}

然后在azurerm_windows_virtual_machine资源的source_image_reference块中:

  source_image_reference {
    publisher = data.azurerm_shared_image.win19_gold_image.identifier[0].publisher
    offer     = data.azurerm_shared_image.win19_gold_image.identifier[0].offer
    sku       = data.azurerm_shared_image.win19_gold_image.identifier[0].sku
    version   = azurerm_shared_image_version.win19_gold_image.name
  }

看起来name参数实际上是图像 [3] 的版本:

name -(必需)此映像版本的版本号,例如 1.0.0。 更改此设置会强制创建新资源。


[1] https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/shared_image_version

[2] https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/image

[3] https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/shared_image_version#name

大家好,遇到这个问题的人,

我找到了解决我的问题的方法。 我所要做的就是定义一个azurerm_shared_image_version数据,然后使用azurerm_windows_virtual_machine中的source_image_id代替source_image_reference{}块。

以下是我所做的:

data "azurerm_shared_image_gallery" "win_gold_image_gallery" {
  provider            = azurerm.gi
  name                = "golden_image_gallery"
  resource_group_name = "gi-rg"
}

data "azurerm_shared_image" "win19_gold_image" {
  provider            = azurerm.gi
  name                = "Windows-2019_base"
  gallery_name        = data.azurerm_shared_image_gallery.win_gold_image_gallery.name
  resource_group_name = data.azurerm_shared_image_gallery.win_gold_image_gallery.resource_group_name
}

data "azurerm_shared_image_version" "win19_gold_image_version" {
  provider            = azurerm.gi
  name                = "latest" # "recent" is also a tag to use the most recent image version
  image_name          = data.azurerm_shared_image.win19_gold_image.name
  gallery_name        = data.azurerm_shared_image.win19_gold_image.gallery_name
  resource_group_name = data.azurerm_shared_image.win19_gold_image.resource_group_name
}

resource "azurerm_windows_virtual_machine" "azure_win_gi_runner" {
  provider                          = azurerm.dep
  name                              = "vm-github-actions-win-gi-${count.index}"
  resource_group_name               = data.azurerm_resource_group.dts_rg.name
  location                          = "East US"
  size                              = var.windows-vm-size
  count                             = "${var.number_of_win_gi_az_instances}"
  network_interface_ids             = [
    azurerm_network_interface.azure_win_gi_runner_nic[count.index].id,
  ]
  computer_name                     = "ga-win-gi-${count.index}"
  admin_username                    = var.windows-admin-username
  admin_password                    = var.WINDOWS_ADMIN_PASS

  os_disk {
    name = "vm-github-actions-win-gi-${count.index}-os-disk"
    caching              = "None"
    storage_account_type = "StandardSSD_LRS"
  }

  source_image_id = data.azurerm_shared_image_version.win19_gold_image_version.id 
# This is the thing I was missing.

  enable_automatic_updates = true
  provision_vm_agent       = true

  tags = {
    whichVM = var.gh_windows_runner
    environment = var.environment 
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM