[英]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.我正在尝试使用 Terraform 使用以前拍摄的快照作为 OS 磁盘来启动 Azure VM (RM)。
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
运行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. Azure 不支持从快照创建 VM。 Instead you should use this snapshot to create a managed disk, then use this managed disk to create a VM.相反,您应该使用此快照创建托管磁盘,然后使用此托管磁盘创建 VM。 See this link .请参阅此链接。
I tested it in my lab and the following tf file works for me:我在我的实验室中对其进行了测试,以下 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"
}
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.首先在同一订阅中创建 VM 的快照。 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.然后在您的 TF 文件中忽略上面指定的源磁盘,只需使用“source_resource_id”下引用的快照路径创建一个副本磁盘,并将其作为 OS 磁盘附加到新 VM。 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".还要确保“storage_os_disk”中对磁盘的引用是“azurerm_managed_disk.copy.id”而不是“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:若要在 Azure 中使用现有快照,首先应创建一个托管磁盘,并将 snap id 作为 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:来自“安德拉德,蒂亚戈”的这 2 篇文章将解释此任务的每个阶段:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.