Import existing resource into current state

Hi,

I tried searching here in the forum, but none of the similar topics were answered, so I tried asking.
How can be an existing resources (in my case Azure) imported into the state? The key is that these resources are dynamically created, so using a count and creates multiple object at same time.

I have made some changes on my terraform code (different solution for the same resource setup). It wants to recreate the resources, but they are already exists, and telling me that the existing resource should be imported into the state:

module.vnet.azurerm_subnet.name[1]: Creating...
module.vnet.azurerm_subnet.name[0]: Creating...

│ Error: A resource with the ID "/subscriptions/xxxxxxxx-e960-4d2a-878c-xxxxxxxxxxxx/resourceGroups/XXXX-Connectivity-WEU-NETWORK/providers/Microsoft.Network/virtualNetworks/XXXX-Connectivity-WEU-VNET-01/subnets/GatewaySubnet" already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for "azurerm_subnet" for more information.
│
│   with module.vnet.azurerm_subnet.name[0],
│   on ..\010-Common\Azure\vnet\main.tf line 35, in resource "azurerm_subnet" "name":
│   35: resource "azurerm_subnet" "name" {
│


│ Error: A resource with the ID "/subscriptions/xxxxxxxx-e960-4d2a-878c-xxxxxxxxxxxx/resourceGroups/XXXX-Connectivity-WEU-NETWORK/providers/Microsoft.Network/virtualNetworks/XXXX-Connectivity-WEU-VNET-01/subnets/AzureFirewallSubnet" already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for "azurerm_subnet" for more information.
│
│   with module.vnet.azurerm_subnet.name[1],
│   on ..\010-Common\Azure\vnet\main.tf line 35, in resource "azurerm_subnet" "name":
│   35: resource "azurerm_subnet" "name" {

It is not possible at the moment to delete the subnets wanted to be created, but I want that my newly formatted code works with the import.

There are no any guides on terraform’s documents about importing existing resources into the state. It it possible I am wrong, in this case please share the URL with me.

Thanks!
G

Hi @gabor.varga,

For most resource types in the official providers there is information about how to import at the end of the documentation page. For example, azurerm_subnet’s Import section.

Based on the ids you’ve included in your question, I think to import from the command line you could run these commands:

terraform import "module.vnet.azurerm_subnet.name[0]" "/subscriptions/xxxxxxxx-e960-4d2a-878c-xxxxxxxxxxxx/resourceGroups/XXXX-Connectivity-WEU-NETWORK/providers/Microsoft.Network/virtualNetworks/XXXX-Connectivity-WEU-VNET-01/subnets/GatewaySubnet"
terraform import "module.vnet.azurerm_subnet.name[1]" "/subscriptions/xxxxxxxx-e960-4d2a-878c-xxxxxxxxxxxx/resourceGroups/XXXX-Connectivity-WEU-NETWORK/providers/Microsoft.Network/virtualNetworks/XXXX-Connectivity-WEU-VNET-01/subnets/AzureFirewallSubnet"

If you are using a recent version of Terraform that supports importing through the configuration then an alternative would be to write the same information in a pair of import blocks in your root module:

import {
  id = "/subscriptions/xxxxxxxx-e960-4d2a-878c-xxxxxxxxxxxx/resourceGroups/XXXX-Connectivity-WEU-NETWORK/providers/Microsoft.Network/virtualNetworks/XXXX-Connectivity-WEU-VNET-01/subnets/GatewaySubnet"
  to = module.vnet.azurerm_subnet.name[0]
}

import {
  id = "/subscriptions/xxxxxxxx-e960-4d2a-878c-xxxxxxxxxxxx/resourceGroups/XXXX-Connectivity-WEU-NETWORK/providers/Microsoft.Network/virtualNetworks/XXXX-Connectivity-WEU-VNET-01/subnets/AzureFirewallSubnet"
  to = module.vnet.azurerm_subnet.name[1]
}

If you choose to use this configuration-based option then you can perform the import using the normal terraform apply command.


Both of these approaches are equivalent, but you should choose only one to use.

1 Like

Thanks,

Now it’s clear for me. Do not understand why it is not possible to create proper example guidance…

Hi @gabor.varga,

I’m glad this solution worked for you.

If you have feedback about how the import instructions are presented in this provider’s documentation, you could share that in the provider’s own repository, which is where the provider documentation (and implementation) is maintained.

Thanks!

@gabor.varga,

If you were looking for general examples on using import functionality, the legacy import command is documented here:

The newer configuration driven import is documented here:

And there is a step-by-step tutorial here: