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.