使用 Terraform,如何在 Azure 上创建使用现有托管磁盘的 VM?

时间:2021-02-15 00:58:48

标签: azure terraform terraform-provider-azure

我从带有 terraform 的 blob 导入了一个托管磁盘。现在我只需要用它创建一个虚拟机(它是一个操作系统磁盘)。怎么样?

我有:

resource "azurerm_managed_disk" "MyDisk" {
  name                 = "MyDisk"
  location             = var.location
  resource_group_name  = azurerm_resource_group.rg.name
  storage_account_type = "Standard_LRS"
  create_option        = "Import"
  storage_account_id   = azurerm_storage_account.temp_storage.id
  source_uri           = "${azurerm_storage_container.images.id}/MyDisk.vhd"
  disk_size_gb         = "32"

  tags = {
    environment = "staging"
  }
}

azurerm_linux_virtual_machine 似乎没有办法获取这个托管磁盘并用它创建一个 VM。有人知道怎么做吗?

非常感谢

2 个答案:

答案 0 :(得分:0)

您可以使用 azurerm_virtual_machine_data_disk_attachment。示例:

resource "azurerm_virtual_machine_data_disk_attachment" "example" {
  managed_disk_id    = azurerm_managed_disk.MyDisk.id
  virtual_machine_id = azurerm_virtual_machine.MyMachine.id
  lun                = "10"
  caching            = "ReadWrite"
}

答案 1 :(得分:0)


# <https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/virtual_machine>
resource "azurerm_virtual_machine" "main" {
  name                  = "VoIP-VM"
  location              = var.location
  resource_group_name   = azurerm_resource_group.VoIP.name
  network_interface_ids = [azurerm_network_interface.VoIP.id]
  vm_size               = "Standard_F2"
  
  # Uncomment this line to delete the OS disk automatically when deleting the VM
  # delete_os_disk_on_termination = true

  # Uncomment this line to delete the data disks automatically when deleting the VM
  # delete_data_disks_on_termination = true

  storage_os_disk {
    name              = "${azurerm_managed_disk.MyDisk.name}"
    caching           = "ReadWrite"
    create_option     = "Attach"
    managed_disk_type = "Standard_LRS"
    managed_disk_id = "${azurerm_managed_disk.MyDisk.id}"
    os_type = "linux"
  }
  
  tags = {
    environment = "staging"
  }
}