简体   繁体   English

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

[英]Using Terraform, how do I create a VM on Azure that uses an existing managed disk?

I have imported a managed disk from a blob with terraform.我已经从带有 terraform 的 blob 中导入了托管磁盘。 Now I just need to create a VM with it (it's an OS disk).现在我只需要用它创建一个虚拟机(它是一个操作系统磁盘)。 How?如何?

I have:我有:

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 doesn't seem to have any way to take this managed disk and make a VM with it. azurerm_linux_virtual_machine 似乎没有任何方法可以获取此托管磁盘并使用它创建 VM。 Anyone know how?有谁知道怎么做?

thank you much非常感谢

You can use azurerm_virtual_machine_data_disk_attachment .您可以使用azurerm_virtual_machine_data_disk_attachment Example:例子:

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"
}

# <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"
  }
}

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

相关问题 如何使用nodejs cli创建带有托管磁盘的Azure Linux VM? - How to create azure linux vm with managed disk using nodejs cli? 如何从带有 Terraform 的不同 Azure 帐户中的 blob 创建托管磁盘? - How do I create a managed disk from a blob in a different Azure account w/Terraform? 使用共享图像库的托管磁盘对 Azure VM 进行 Terraform - Terraform Azure VM using Managed Disk using Shared image gallery Terraform 从托管磁盘问题创建 VM - Terraform Create VM from managed disk issue 我想创建 2 azure 虚拟机并使用 terraform 安装 jenkins 和 Sonarqube 有人知道该怎么做吗? - I want to create 2 azure vm and install jenkins and Sonarqube using terraform does anyone know how to do that? 将现有的托管数据磁盘连接到现有的Azure虚拟机 - Attach Existing managed data disk to existing azure VM 如何使用 Terraform 将现有 rbac 角色附加到 Azure VM? - How to attach an existing rbac role to an Azure VM using Terraform? 如何使用非托管磁盘从 VHD 创建 Azure VM? - How do you create Azure VM from VHD with unmanaged disk? Terraform 从托管磁盘映像创建 VM - Terraform creating VM from managed disk image 使用 ARM 模板将现有托管磁盘附加到新 VM - attach an existing managed disk to a new VM using ARM template
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM