Skip to content
Error: Resource already managed by Terraform

Error: Resource already managed by Terraform

DodaTech 3 min read

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 import and then redefined in configuration.
  • Someone ran terraform apply twice 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-name

Step 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_server

This 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 refresh

This 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 apply
Does terraform state rm destroy the resource?
No — terraform state rm only removes the resource from the state file. The actual cloud resource continues running. To destroy it you must run terraform destroy or remove it from configuration and run terraform apply with -destroy.
How do I prevent this error in CI/CD pipelines?
Use terraform plan with the -detailed-exitcode flag to detect changes before applying. Also store state files remotely (S3, Consul, Terraform Cloud) and use state locking to prevent concurrent modifications that could cause duplicates.
Can I manually edit the state file to remove a resource?
You can, but it’s not recommended. Manual edits to terraform.tfstate are error-prone. Always prefer terraform state rm or terraform import to modify state safely. If you must edit the file directly, back it up first and run terraform validate afterward.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro