Error Importing existing resources into Terraform

2019-07-26 04:02发布

问题:

I am attempting to refactor some of my modules, which requires me to move existing resources into a different state file.

Normally importing resources is monotonous but straightforward.

I do not know how to interpret the following "The number of path segments is not divisible by 2" error when attempting to import any of these resources.

This is happening on all resources I am attempting to import (which I have successfully done many times in the past)

CLI> terraform import azurerm_virtual_machine.east_mm01_vm /resource/subscriptions/XXX/resourceGroups/XXX/providers/Microsoft.Compute/virtualMachines/stguemm01

Acquiring state lock. This may take a few moments...

azurerm_virtual_machine.east_mm01_vm: Importing from ID "/resource/subscriptions/XXX/resourceGroups/XXX/providers/Microsoft.Compute/virtualMachines/stguemm01"...
azurerm_virtual_machine.east_mm01_vm: Import complete!

  Imported azurerm_virtual_machine (ID: /resource/subscriptions/XXX/resourceGroups/XXX/providers/Microsoft.Compute/virtualMachines/stguemm01)
azurerm_virtual_machine.east_mm01_vm: Refreshing state... (ID: /resource/subscriptions/XXX...soft.Compute/virtualMachines/stguemm01)

Error: azurerm_virtual_machine.east_mm01_vm (import id: /resource/subscriptions/XXX/resourceGroups/XXX/providers/Microsoft.Compute/virtualMachines/stguemm01): 1 error(s) occurred:

* import azurerm_virtual_machine.east_mm01_vm result: /resource/subscriptions/XXX/resourceGroups/XXX/providers/Microsoft.Compute/virtualMachines/stguemm01: azurerm_virtual_machine.east_mm01_vm: The number of path segments is not divisible by 2 in "resource/subscriptions/XXX/resourceGroups/XXX/providers/Microsoft.Compute/virtualMachines/stguemm01"

Terraform Version:

CLI> terraform -v
Terraform v0.11.11
+ provider.azurerm v1.20.0

回答1:

your resource ID is wrong (/resource/subscriptions/XXX/resourceGroups/XXX/providers/Microsoft.Compute/virtualMachines/stguemm01). it should be this:

/subscriptions/XXX/resourceGroups/XXX/providers/Microsoft.Compute/virtualMachines/stguemm01