Using Terraform to import existing resources on Azure

前端 未结 3 1213
死守一世寂寞
死守一世寂寞 2020-12-06 11:21

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

3条回答
  •  攒了一身酷
    2020-12-06 11:30

    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//resourceGroups/test/providers/Microsoft.Network/networkSecurityGroups/test-nsg"
    azurerm_network_security_group.myterraformnsg: Importing from ID "subscriptions//resourceGroups/test/providers/Microsoft.Network/networkSecurityGroups/test-nsg"...
    azurerm_network_security_group.myterraformnsg: Import complete!
      Imported azurerm_network_security_group (ID: subscriptions//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//resourceGroups/test/providers/Microsoft.Network/networkSecurityGroups/test-nsg): 1 error(s) occurred:
    
    * import azurerm_network_security_group.myterraformnsg result: subscriptions//resourceGroups/test/providers/Microsoft.Network/networkSecurityGroups/test-nsg: azurerm_network_security_group.myterraformnsg: Cannot parse Azure ID: parse subscriptions//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//resource/subscriptions//resourceGroups/test/providers/Microsoft.Network/networkSecurityGroups/test-nsg"
    azurerm_network_security_group.myterraformnsg: Importing from ID "https://portal.azure.com//resource/subscriptions//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//resource/subscriptions//resourceGroups/test/providers/Microsoft.Network/networkSecurityGroups/test-nsg)
    azurerm_network_security_group.myterraformnsg: Refreshing state... (ID: https://portal.azure.com//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.

提交回复
热议问题