terraform state ai_generated true

Error: A resource with the ID already exists

ID: terraform/resource-already-exists

Also available as: JSON · Markdown
91%Fix Rate
93%Confidence
50Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
115 active
1 active

Root Cause

Terraform tries to create a resource that already exists outside of its state.

generic

Workarounds

  1. 95% success Import the existing resource: terraform import <resource_addr> <resource_id>
    Brings the existing resource under Terraform management

    Sources: https://developer.hashicorp.com/terraform/cli/commands/import

  2. 90% success Use import blocks (Terraform 1.5+) for declarative import
    import { to = aws_instance.web; id = 'i-12345' }

Dead Ends

Common approaches that don't work:

  1. Deleting the existing resource manually 70% fail

    May cause downtime; the resource may be in use by other systems

  2. Changing the resource name in Terraform to avoid the conflict 75% fail

    Creates a duplicate resource instead of managing the existing one

Error Chain

Leads to: