Using Terraform to import existing resources on Azure

与世无争的帅哥 提交于 2019-11-30 21:22:13
James Thorpe

It looks like you need to fix your script file first - azurerm isn't a valid resource name, did you mean:

resource "azurerm_resource_group" "example" {
    # ...instance configuration...
    name = "MyResourceGroup"    
}

As seen in the output, import is expecting two parameters, ADDR and ID - you're only passing (what I assume is) the ID. You also need to tell terraform which resource in your script it maps to:

terraform import azurerm_resource_group.example \
  /subscriptions/MySubscriptionNumber/resourceGroups/MyResourceGroup

When I copy your CLI, I get the same result with you.

Between azurerm_resource_group.MyResourceGroup and /subscriptions/MySubscriptionNumber/resourceGroups/MyResourceGroup, it needs a space.

The correct format is below:

terraform import azurerm_resource_group.MyResourceGroup /subscriptions/MySubscriptionNumber/resourceGroups/MyResourceGroup

More information about this please refer to this link.

Using the Terraform Azure provider v1.16.0 I got a "Cannot parse Azure ID" error message:

terraform import azurerm_network_security_group.myterraformnsg "subscriptions/<subscriptionId>/resourceGroups/test/providers/Microsoft.Network/networkSecurityGroups/test-nsg"
azurerm_network_security_group.myterraformnsg: Importing from ID "subscriptions/<subscriptionId>/resourceGroups/test/providers/Microsoft.Network/networkSecurityGroups/test-nsg"...
azurerm_network_security_group.myterraformnsg: Import complete!
  Imported azurerm_network_security_group (ID: subscriptions/<subscriptionId>/resourceGroups/test/providers/Microsoft.Network/networkSecurityGroups/test-nsg)

azurerm_network_security_group.myterraformnsg: Refreshing state... (ID: subscriptions/ef37d4b2-686a-494a-9001-5.../networkSecurityGroups/test-nsg)
Error: azurerm_network_security_group.myterraformnsg (import id: subscriptions/<subscriptionId>/resourceGroups/test/providers/Microsoft.Network/networkSecurityGroups/test-nsg): 1 error(s) occurred:

* import azurerm_network_security_group.myterraformnsg result: subscriptions/<subscriptionId>/resourceGroups/test/providers/Microsoft.Network/networkSecurityGroups/test-nsg: azurerm_network_security_group.myterraformnsg: Cannot parse Azure ID: parse subscriptions/<subscriptionId>/resourceGroups/test/providers/Microsoft.Network/networkSecurityGroups/test-nsg: invalid URI for request

Looking into the Azure provider source code I found out that you need to enter the full URL to the Azure resource - like this:

terraform import azurerm_network_security_group.myterraformnsg "https://portal.azure.com/<id>/resource/subscriptions/<subscriptionId>/resourceGroups/test/providers/Microsoft.Network/networkSecurityGroups/test-nsg"
azurerm_network_security_group.myterraformnsg: Importing from ID "https://portal.azure.com/<id>/resource/subscriptions/<subscriptionId>/resourceGroups/test/providers/Microsoft.Network/networkSecurityGroups/test-nsg"...
azurerm_network_security_group.myterraformnsg: Import complete!
  Imported azurerm_network_security_group (ID: https://portal.azure.com/<id>/resource/subscriptions/<subscriptionId>/resourceGroups/test/providers/Microsoft.Network/networkSecurityGroups/test-nsg)
azurerm_network_security_group.myterraformnsg: Refreshing state... (ID: https://portal.azure.com/<id>/networkSecurityGroups/test-nsg)

Import successful!

The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.

Unfortunately, Import will only update the Terraform state.

It will not (yet) update the configuration file.

This makes the Import function less useful, IMO.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!