Terraform import block - configuration does not exist

Hi Everyone,

I’m struggling with terraform import and need some help. I’m terraforming an Cisco ACI infrastructure and create reseources from a csv file which gets imported in data & local. So far this is working as expected.

I’m on terraform version 1.7.4 and my terraform code looks like this (cleaned up to show only the necessary information)

resource "aci_application_epg" "example" {
  for_each = { for inst in local.vlans : "${inst.tenant}-${inst.vlan}" => inst }
  name                   = each.value.name
}

The csv files contains 3 variables which are tenant, vlan and name. The value of tenant is static and always “MyTenant”. The value for vlan is “1101” and “207” and the value for name is “epg-vl-1101” and “epg-vl-0207”

tenant,vlan,name
MyTenant,1101,epg-vl-1101
MyTenant,207,epg-vl-0207

This gives me multiple instances of the resource each with a key of ${inst.tenant}-${inst.vlan} → “MyTenant-1101” and “MyTenant-207”

Terraform state file looks like this (cleaned up to show only the necessary information)

    {
      "mode": "managed",
      "type": "aci_application_epg",
      "name": "example",
      "provider": "provider[\"registry.terraform.io/ciscodevnet/aci\"]",
      "instances": [
        {
          "index_key": "MyTenant-1101",
          "schema_version": 1,
          "attributes": {
            "name": "epg-vl-1101",
          },
        },
        {
          "index_key": "MyTenant-207",
          "schema_version": 1,
          "attributes": {
            "name": "epg-vl-0207",
          },
     
        }
      ]
    }

So far so good. Since this is a brownfield migration I need to import hundrets of existing epg resources in my terraform configuration / state file.

I tried to solve this with a import block, another csv file and for_each

The csv file contains 1 variable which is the vlan-id

vlan-id
1100
import {
  for_each = local.vlan_import
  to       = aci_application_epg.example["MyTenant-${each.value.vlan-id}"]
  id       = "uni/tn-MyTenant/ap-app-profile/epg-vl-${each.value.vlan-id}"
}

When I issue terraform plan -generate-config-out="generated_resources.tf" I always hit this error:

You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure.
> ╷
> │ Error: Configuration for import target does not exist
> │ 
> │ The configuration for the given import aci_application_epg.example["MyTenant-1100"] does not exist. All target instances must have an associated configuration to be imported.
> ╵

Even if I try to do a manuelly import I hit the same error.

> $ terraform import 'aci_application_epg.example["MyTenant-1100"]' "uni/tn-MyTenant/ap-app-profile/epg-vl-1100"

╷
│ Error: Configuration for import target does not exist
│ 
│ The configuration for the given import aci_application_epg.example["MyTenant-1100"] does not exist. All target instances must have an associated configuration to be imported.
╵

Any help would be highly appreciated.

Hi @oliver.matt,

The error is indicating that you are trying to import into the configuration address aci_application_epg.example["MyTenant-1100"], but that does not exist in your configuration. The only ID’s you are showing are 1101 and 207. A resource must be accounted for in the configuration in order to import it so that it can be managed by Terraform.

Hi @jbardin ,

thank you for your reply. I do know about that, but I thought that with the option -generate-config-out="generated_resources.tf" terraform should generate the resources?

If this doesn’t work how can I generate the configuration for the needed instances of my resource without doing a terraform apply?

The -generate_config-out option can’t work in this case, because you already have a resource block for the instances you’re trying to import. Terraform would have to know how to manipulate the incoming data used in the for_each expressions to find a way to include the new resource instances and their respective configurations, even possibly needing to rewrite the expressions which it can’t do.

If you want to generate configuration, you will need to point the import block at a resource address which does not yet exist.