0
votes

I'm using the terraform azurerm provider 2.0.0 and I'm trying to learn how everything works. I have a bunch of existing azure resources including a bunch of Windows VMs in a Resource Group. It appears that I have to manually import each of them one at a time using the

terraform import azurerm_wndows_virtual_machine.<myvmname> /subscriptions/<mysub>/resourceGroups/<myrg>/providers/Microsoft.Compute/virtualMachines/<myvmname>

command.

However, when I try to do this I get

Error: The "azurerm_windows_virtual_machine" resource doesn't support attaching OS Disks - please use the `azurerm_virtual_machine` resource instead

My understanding is that I need to put the imported resource in the terraform file as an empty element in order for terraform to connect the imported item with an item in the file.

My terraform script looks like this

# Configure the Azure provider
provider "azurerm" {
    version = "=2.0.0"
    subscription_id = "00000000-0000-0000-0000-000000000000"
    tenant_id       = "00000000-0000-0000-0000-000000000000"
}

resource "azurerm_resource_group" "myrg" {

}

resource "azurerm_windows_virtual_machine" "myvmname" {

}

What am I doing wrong?

1
i should add that I successfully imported the resource group which also appears in the file. its the vm that is giving me the error.Zack
Any update on this issue? Please let me know if you need help further. Or if my reply helps you, please accept it.Nancy Xiong
Thanks, Nancy Xiong. I was waiting to see if anyone else had any information also. It seems wrong that I would have to use the old azurerm_virtual_machine to import an existing vm. I thought that was deprecated in favor of the new azurerm_windows_virtual_machine. It seems like I should be able to import an existing machine using the new type as this seems like a common scenario.Zack
Is there any custom image vm? The resource "azurerm_windows_virtual_machine" can work on my side when import a VM deployed from azure vm windows 2016. or try to upgrade your azurerm provider or terraform version?Nancy Xiong
not a custom vm image. I believe I have the latest provider but I'll check and try againZack

1 Answers

0
votes

From this azurerm_windows_virtual_machine terraform document,

Note This resource does not support Unmanaged Disks. If you need to use Unmanaged Disks you can continue to use the azurerm_virtual_machine resource instead.

Note This resource does not support attaching existing OS Disks. You can instead capture an image of the OS Disk or continue to use the azurerm_virtual_machine resource instead.

It seems that you are trying to import a Azure VM that have Unmanaged Disks or attached existing OS Disks. In this case, you need to use azurerm_virtual_machine resource instead.

Your terraform will like this,

# Configure the Azure provider
provider "azurerm" {
    version = "=2.0.0"
    subscription_id = "00000000-0000-0000-0000-000000000000"
    tenant_id       = "00000000-0000-0000-0000-000000000000"
}

resource "azurerm_resource_group" "myrg" {

}

resource "azurerm_virtual_machine" "myvmname" {

}

Virtual Machines can be imported using the resource id, e.g.

terraform import azurerm_virtual_machine.myvmname /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/mygroup1/providers/microsoft.compute/virtualMachines/machine1