基于共享图像库上提供的自定义图像部署 Azure VM

Pir*_*ian 2 azure azure-virtual-machine terraform

在此输入图像描述

我首先创建了一个简单的标准虚拟机,在Azure其中使用不同的软件为数据科学目的进行了定制。然后我从该虚拟机创建了一个映像,以便我可以使用相同的配置更快地基于自定义映像部署新虚拟机。我已将图像保存在Azure Shared Image Gallery. 有什么方法可以将Terraform脚本中的自定义图像部署到新的图像中吗Resource Group?我知道如何部署普通的标准虚拟机,Terraform但无法找到如何根据共享映像库中保存的自定义映像来部署它。

Nan*_*ong 7

使用 terraform 从 Azure 共享图像库部署自定义图像。您可以使用数据源:azurerm_shared_imageazurerm_windows_virtual_machineazurerm_linux_virtual_machine来管理它并指定source_image_id. 请注意,在部署之前,新创建的虚拟机应与共享映像位于同一区域。如果没有,您可以将此映像复制到所需的区域,请阅读https://learn.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
  
}
Run Code Online (Sandbox Code Playgroud)

结果

在此输入图像描述