I have an existing resource group on Azure with a VM running on it and have been playing around with Terraform to try and import the resource to my state file.
I ha
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.
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.
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