Terraform - 从快照启动Azure虚拟机

时间:2018-01-09 13:32:10

标签: azure azure-storage azure-virtual-machine terraform terraform-provider-azure

我尝试使用Terraform使用以前拍摄的快照作为操作系统磁盘启动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'

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

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

我在我的实验室测试了它,以下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"
}

答案 1 :(得分:0)

我一直在处理同样的问题,并设法从快照创建VM。 首先在同一订阅中创建VM的快照。然后在你的TF文件中忽略上面指定的源磁盘,只需创建一个复制磁盘,其中包含&#34; source_resource_id&#34;下引用的快照的路径。并将其作为操作系统磁盘附加到新VM。以下是我使用过的一个例子。还要确保&#34; storage_os_disk&#34;中的磁盘引用。是&#34; azurerm_managed_disk.copy.id&#34;不是&#34; resource.azurerm_managed_disk.copy.id&#34;。希望这有帮助!

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"