4

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
4c74356b41
  • 69,186
  • 6
  • 100
  • 141
Kurt P
  • 113
  • 2
  • 5

2 Answers2

4

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
4c74356b41
  • 69,186
  • 6
  • 100
  • 141
  • 1
    weird, could swear I tried that. You, sir (or madam), are my hero. that was the issue! – Kurt P Dec 27 '18 at 20:43
  • I'm having the same issue and my line reads `The number of path segments is not divisible by 2 in "subscriptions/xxx.....` . So, this answer does not really help me. I don't know how I could change it to `/subscriptions...`. v2.93.0 terraform – djangofan Jan 21 '22 at 22:50
  • create a new question then – 4c74356b41 Jan 22 '22 at 08:15
  • I did: https://stackoverflow.com/questions/70808969/terraform-apply-error-the-number-of-path-segments-is-not-divisible-by-2-azure I might also bounty it. – djangofan Jan 23 '22 at 18:23
0

I encountered such an issue when trying to import an SSH key into a Terraform module.

I had the error below:

module.ssh_public_key.azurerm_ssh_public_key.main: Importing from ID "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.Compute/SshPublicKeys/mySshPublicKeyName1"...

Error: parsing Resource ID "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.Compute/SshPublicKeys/mySshPublicKeyName1": ID was missing the `sshPublicKeys` element

Terraform Azure documentation specified to use this:

terraform import azurerm_ssh_public_key.example /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.Compute/SshPublicKeys/mySshPublicKeyName1

However, there is a typo error with the SshPublicKeys. The first S should be lowercase and not uppercase, that is, sshPublicKeys and not SshPublicKeys

So we will have:

terraform import azurerm_ssh_public_key.example /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.Compute/sshPublicKeys/mySshPublicKeyName1
Promise Preston
  • 24,334
  • 12
  • 145
  • 143