根据共享图像库中可用的自定义图像部署 Azure VM

Deploy Azure VM based on customize Image available on Shared Image Gallery

我首先在 Azure 中创建了一个简单的标准 VM,我为数据科学目的对其进行了定制,上面装有不同的软件。然后我从这个 VM 创建了一个映像,这样我就可以使用相同的配置更快地部署基于自定义映像的新 VM。我已将图像保存在 Azure Shared Image Gallery 下。 是否可以通过任何方式将此自定义映像从 Terraform 脚本部署到新的 Resource Group?我知道如何从 Terraform 部署普通的标准 VM,但无法找到如何根据保存在共享图像库中的自定义图像来部署它。

使用 terraform 从 Azure 共享图像库部署自定义图像。你可以使用 Data Source: azurerm_shared_image and azurerm_windows_virtual_machine or azurerm_linux_virtual_machine to manage it with specify the source_image_id. Please note that the newly created VM should be in the same region as the shared image before you deploy it. If not, you could replicate this image to your desired region, read https://docs.microsoft.com/en-us/azure/virtual-machines/shared-image-galleries#replication

例如,从通用共享映像部署 Windows VM:

provider "azurerm" {
  features {}
}


data "azurerm_shared_image" "example" {
  name                = "my-image"
  gallery_name        = "my-image-gallery"
  resource_group_name = "example-resources-imageRG"
}

resource "azurerm_resource_group" "example" {
  name     = "example-resources"
  location = "xxxxximageregion"
}

resource "azurerm_virtual_network" "example" {
  name                = "example-network"
  address_space       = ["10.0.0.0/16"]
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
}

resource "azurerm_subnet" "example" {
  name                 = "internal"
  resource_group_name  = azurerm_resource_group.example.name
  virtual_network_name = azurerm_virtual_network.example.name
  address_prefixes     = ["10.0.2.0/24"]
}

resource "azurerm_network_interface" "example" {
  name                = "example-nic"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name

  ip_configuration {
    name                          = "internal"
    subnet_id                     = azurerm_subnet.example.id
    private_ip_address_allocation = "Dynamic"
  }
}

resource "azurerm_windows_virtual_machine" "example" {
  name                = "example-machine"
  resource_group_name = azurerm_resource_group.example.name
  location            = azurerm_resource_group.example.location
  size                = "Standard_F2"
  admin_username      = "adminuser"
  admin_password      = "P@$$w0rd1234!"
  network_interface_ids = [
    azurerm_network_interface.example.id,
  ]

  os_disk {
    caching              = "ReadWrite"
    storage_account_type = "Standard_LRS"
  }

  source_image_id = data.azurerm_shared_image.example.id
  
}

结果