简体   繁体   中英

Terraform - launching an Azure virtual machine from a snapshot

I'm trying to use Terraform to launch an Azure VM (RM) using a previously taken snapshot as the OS Disk.

Here's what I have.

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

I'm getting the following error when running 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'

Any ideas?

Azure does not support creating a VM from a snapshot. Instead you should use this snapshot to create a managed disk, then use this managed disk to create a VM. See this link .

I tested it in my lab and the following tf file works for me:

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

I have been working on the same issue and managed to create a VM from a snapshot. First create a snapshot of a VM within the same subscription. Then in your TF file ignore the source disk specified above and just create a copy disk with the path to the snapshot referenced under "source_resource_id" and attach that to the new VM as the OS disk. Below is an example that i have used. Also make sure the reference to the disk in "storage_os_disk" is "azurerm_managed_disk.copy.id" not "resource.azurerm_managed_disk.copy.id". Hope this helps!

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"

To use an existing snapshot in Azure, first a managed disk should be created with snap id as source_resource_id:

resource "azurerm_managed_disk" "some_name" {
  # option, copy and create a new disk from snapshot
  create_option = "Copy"
  # Snapshot ID
  source_resource_id = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/your_resource_group/providers/Microsoft.Compute/snapshots/my_snapshot"
  <other_parameters...>
}

These 2 articles from "Andrade, Thiago" at medium will explain each stages of this task:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM