Error: Resource already managed by Terraform
Terraform’s “Resource already managed” error means the resource you’re creating already exists in Terraform state, preventing duplicate creation or import.
What It Means
Every resource Terraform manages is recorded in a state file (typically terraform.tfstate). When you try to create a resource whose identifier already exists in the state, Terraform refuses because it would create a conflict between the existing state entry and the new one.
Why It Happens
- A resource was imported with
terraform importand then redefined in configuration. - Someone ran
terraform applytwice for the same resource definition. - State was partially corrupted or duplicated during a failed operation.
- A resource was created outside Terraform and then defined in configuration without importing it first.
- The state file was manually edited incorrectly.
How to Fix It
Step 1: Identify the duplicate resource
Find the resource in your state:
terraform state list | grep <resource-name>This shows all resources currently tracked by Terraform. Compare against the new resource in your configuration.
Step 2: Import the existing resource (if it was created outside Terraform)
If the resource exists in the cloud but not in your configuration, import it:
terraform import <resource_type>.<name> <resource_id>For example, importing an AWS S3 bucket:
terraform import aws_s3_bucket.my_bucket my-existing-bucket-nameStep 3: Remove the resource from state
If you want Terraform to stop tracking a resource without destroying it:
terraform state rm <resource_type>.<name>For example:
terraform state rm aws_instance.web_serverThis removes the resource from state. The actual infrastructure remains untouched.
Step 4: Refresh the state
If the state is stale or out of sync:
terraform refreshThis updates the state file to match real-world infrastructure.
Step 5: Taint and recreate (if the resource needs replacement)
If the resource is corrupted or needs to be rebuilt:
terraform taint <resource_type>.<name>
terraform plan
terraform applyBuilt by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro