将现有资源导入 Terraform 状态文件时出错

我正在尝试重构我的一些模块,这需要我将现有资源移动到不同的状态文件中。

通常导入资源是单调但简单的。

在尝试导入任何这些资源时,我不知道如何解释以下 "The number of path segments is not divisible by 2" 错误。

这发生在我尝试导入的所有资源上(过去我已经成功完成了很多次)

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"

地形版本:

CLI> terraform -v
Terraform v0.11.11
+ provider.azurerm v1.20.0
stack overflow Error Importing existing resources into Terraform State File
原文答案
author avatar

接受的答案

您的资源 ID 错误 ( /resource/subscriptions/XXX/resourceGroups/XXX/providers/Microsoft.Compute/virtualMachines/stguemm01 )。应该是这样的:

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

答案:

作者头像

我在尝试将 SSH 密钥导入 Terraform 模块时遇到了这样的问题。

我有以下错误:

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 指定使用这个:

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

但是, SshPublicKeys 存在拼写错误。第一个 S 应该是小写而不是大写,即 sshPublicKeys 而不是 SshPublicKeys

所以我们会有:

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