terraform state_error ai_generated true

Error: Resource already managed by Terraform or import ID not found

ID: terraform/tf-import-resource-exists

Also available as: JSON · Markdown
85%Fix Rate
87%Confidence
75Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1 active

Root Cause

Terraform import failed because the resource already exists in state or the import ID format is incorrect.

generic

Workarounds

  1. 88% success Use terraform state rm to remove stale state entry before re-importing
    terraform state rm MODULE.RESOURCE_ADDRESS, then terraform import MODULE.RESOURCE_ADDRESS RESOURCE_ID

    Sources: https://developer.hashicorp.com/terraform/cli/commands/state/rm

  2. 85% success Use the import block in Terraform 1.5+ for declarative imports
    Add import { to = RESOURCE_ADDRESS; id = "RESOURCE_ID" } block and run terraform plan

    Sources: https://developer.hashicorp.com/terraform/language/import

Dead Ends

Common approaches that don't work:

  1. Delete the real resource and recreate it with Terraform 85% fail

    Deleting production resources causes downtime; import exists specifically to avoid this

  2. Force-push a new state file to overwrite the existing resource entry 80% fail

    Overwriting state can cause other resources to become orphaned or duplicated

Error Chain

Leads to:
Preceded by:
Frequently confused with: