1

我正在尝试使用 Terraform 使用以前拍摄的快照作为 OS 磁盘来启动 Azure VM (RM)。

这就是我所拥有的。

data "azurerm_managed_disk" "windows-workstation_disk" {
  name = "workstation-disk"
  resource_group_name = "joeg"
}

## Workstation machine
resource "azurerm_virtual_machine" "windows-workstation" {
  name                  = "windows-workstation"
  location              = "${var.location}"
  resource_group_name   = "${azurerm_resource_group.rg.name}"
  vm_size               = "Standard_D2s_v3"
  network_interface_ids = ["${azurerm_network_interface.windows-workstation_nic.id}"]

  storage_os_disk {
  name              = "windows-workstation_osdisk"
  managed_disk_id   = "${data.azurerm_managed_disk.windows-workstation_disk.id}"
  create_option     = "Attach"
}

运行时出现以下错误terraform apply

Error applying plan:

1 error(s) occurred:

* azurerm_virtual_machine.windows-workstation: Resource 
'data.azurerm_managed_disk.windows-workstation_disk' not found for variable 
'data.azurerm_managed_disk.windows-workstation_disk.id'

有任何想法吗?

4

2 回答 2

2

Azure 不支持从快照创建 VM。相反,您应该使用此快照创建托管磁盘,然后使用此托管磁盘创建 VM。请参阅此链接

我在我的实验室中对其进行了测试,以下 tf 文件对我有用:

resource "azurerm_resource_group" "test" {
  name = "shuicli"
  location = "East US"
}

resource "azurerm_managed_disk" "source" {
  name = "shuitest"
  location = "East US"
  resource_group_name = "shuicli"
  storage_account_type = "Standard_LRS"
  create_option = "Empty"
  disk_size_gb = "30"

  tags {
    environment = "staging"
  }
}

resource "azurerm_managed_disk" "copy" {
  name = "shuicli"
  location = "East US"
  resource_group_name = "shuicli"
  storage_account_type = "Standard_LRS"
  create_option = "Copy"
  source_resource_id = "<snapshot resource url>"
  disk_size_gb = "32"

  tags {
    environment = "staging"
  }
}

## Workstation machine
resource "azurerm_virtual_machine" "windows-workstation" {
  name                  = "windows-workstation"
  location              = "East US"
  resource_group_name   = "shuicli"
  vm_size               = "Standard_D2s_v3"
  network_interface_ids = ["${azurerm_network_interface.windows-workstation_nic.id}"]

  storage_os_disk {
  name              = "shuitest"
  os_type = "windows"
  managed_disk_id   = "${resource.azurerm_managed_disk.source.id}"
  create_option     = "Attach"
}
于 2018-01-10T02:11:56.383 回答
0

我一直在处理同样的问题并设法从快照创建虚拟机。首先在同一订阅中创建 VM 的快照。然后在您的 TF 文件中忽略上面指定的源磁盘,只需使用“source_resource_id”下引用的快照路径创建一个副本磁盘,并将其作为 OS 磁盘附加到新 VM。下面是我用过的一个例子。还要确保“storage_os_disk”中对磁盘的引用是“azurerm_managed_disk.copy.id”而不是“resource.azurerm_managed_disk.copy.id”。希望这可以帮助!

resource "azurerm_managed_disk" "copy" {
  name = "myOsDisk4"
  location = "North Europe"
  resource_group_name = "${azurerm_resource_group.myterraformgroup.name}"
  storage_account_type = "Standard_LRS"
  create_option = "Copy"
  source_resource_id = "/subscriptions/*************/resourceGroups/My-prod-rg/providers/Microsoft.Compute/snapshots/test-01-c-drive"
  disk_size_gb = "127"

  tags {
    environment = "Prod"
  }
}

# Create virtual machine
resource "azurerm_virtual_machine" "myterraformvm" {
    name                  = "Test-01"
    location              = "North Europe"
    resource_group_name   = "${azurerm_resource_group.myterraformgroup.name}"
    network_interface_ids = ["${azurerm_network_interface.myterraformnic.id}"]
    vm_size               = "Standard_DS1_v2"


    storage_os_disk {
    name              = "${azurerm_managed_disk.copy.name}"
    os_type           = "windows"
    managed_disk_id   = "${azurerm_managed_disk.copy.id}"
    create_option     = "Attach"
于 2018-01-16T11:51:50.683 回答