Terraform import block fails because the target resource has no matching config
Fix it fast
Most common cause, 1 of 3No resource block exists for the address in to
Write the resource block by hand with the same type and name used in to.
resource "aws_s3_bucket" "this" {
bucket = "example-bucket"
}
# or let Terraform draft it (new file only)
terraform plan -generate-config-out=generated.tfShort answer
Terraform reports Configuration for import target does not exist when the to address in an import block names no resource block, or names an instance key or module path that the configuration never creates. Add the resource block, drive the import and the resource from the same for_each collection, and include module.<name> in to. Applies to Terraform 1.7 to 1.17.
Which cause do you have?
Run a plan and note the address printed under the error, then search every .tf file for a resource block with that type and name. No match points to cause 1. A match in the root module whose keys differ from the import points to cause 2. A match inside a module folder points to cause 3.
terraform plan -no-color 2>&1 | grep -A4 "import target does not exist"
grep -rn --include=*.tf 'resource "aws_s3_bucket" "this"' .Then find the line you see in this list and open its fix:
- 1No resource block exists for the address in to
The grep finds no resource block with that type and name; for an import without for_each, the detail text also suggests terraform plan -generate-config-out=generated.tf - 2The import for_each keys differ from the keys the resource creates
The detail says all target instances must have an associated configuration, and the address ends in a key such as ["staging"] - 3The resource sits inside a module but to leaves out the module path
The resource block is in a module folder, while to starts directly with the resource type
What does the error look like?
Error: Configuration for import target does not existThe configuration for the given import target aws_s3_bucket.this does not exist. If you wish to automatically generate config for this resource, use the -generate-config-out option within terraform plan.All target instances must have an associated configuration to be imported.
Cause 1 of 3
No resource block exists for the address in to
You seeThe grep finds no resource block with that type and name; for an import without for_each, the detail text also suggests terraform plan -generate-config-out=generated.tf
Check
Configuration for import target does not exist, followed by the config generation hint, comes from a to address whose resource type and name appear in no .tf file. Terraform will only import into a resource it can find in configuration, unless you ask it to write one.
grep -rn 'resource "aws_s3_bucket" "this"' --include=*.tf .Fix
Write the resource block by hand with the same type and name used in to. Or run a plan with -generate-config-out and a file name that does not exist yet; Terraform drafts a block for each import target missing from the configuration, and you review it before committing.
resource "aws_s3_bucket" "this" {
bucket = "example-bucket"
}
# or let Terraform draft it (new file only)
terraform plan -generate-config-out=generated.tfVerify
The plan lists the resource as an import instead of failing.
terraform planConfirmed by developer.hashicorp.com
Cause 2 of 3
The import for_each keys differ from the keys the resource creates
You seeThe detail says all target instances must have an associated configuration, and the address ends in a key such as ["staging"]
Check
With for_each on the import block, each.key builds instance addresses like aws_s3_bucket.this["staging"]. Terraform checks every one of those against the instances the resource block expands to. When the resource loops over another map or set, even one holding the same bucket names under different keys, those addresses never line up.
echo 'keys(local.buckets)' | terraform console
grep -n "for_each\|count" main.tfFix
Feed one collection to both blocks, as the import reference does, so every key the import names also exists on the resource.
locals {
buckets = {
staging = "bucket1"
uat = "bucket2"
}
}
import {
for_each = local.buckets
to = aws_s3_bucket.this[each.key]
id = each.value
}
resource "aws_s3_bucket" "this" {
for_each = local.buckets
bucket = each.value
}Verify
Plan shows one import per key, for example aws_s3_bucket.this["staging"] will be imported.
terraform plan | grep -i importConfirmed by developer.hashicorp.com
Cause 3 of 3
The resource sits inside a module but to leaves out the module path
You seeThe resource block is in a module folder, while to starts directly with the resource type
Check
An import block's to argument is the full address the object will have in state. For a resource declared in a child module, that address begins with module.<name>, plus the module's own key when the module uses for_each. Without that prefix Terraform looks for the resource in the root module and finds nothing.
grep -rln 'resource "aws_s3_bucket" "this"' --include=*.tf .Fix
Prefix to with the module address. When the module itself is repeated, take its key from the import loop too.
import {
for_each = local.buckets
id = each.value.id
to = module.group[each.value.group].aws_s3_bucket.this[each.value.key]
}Verify
The plan names module.group[...] addresses as imports and the error is gone.
terraform planConfirmed by developer.hashicorp.com
How do I stop it coming back?
- Keep import blocks in an imports.tf beside the resources, built from the same locals the resources loop over.
- Require Terraform 1.7 or newer in required_version when import blocks use for_each, so older binaries fail early with a clear message.
- After the import is applied, either delete the import block or update its to address in the same change whenever you rename keys or move the resource into a module.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: Dispatch Pro, Raheeb, Loade Driver.