Error: resource already exists means Terraform is trying to create a resource that already exists in your cloud provider but isnβt tracked in Terraformβs state file.
Why this happens
Terraform maintains a state file that maps your configuration to real infrastructure. When a resource exists in the cloud but not in state β because it was created manually, the state was lost, or another workspace created it β Terraform tries to create it fresh and the cloud provider rejects the duplicate.
What causes this error
- Resource created manually β someone created it in the AWS/GCP console
- State file lost or corrupted β Terraform doesnβt know about existing resources
- Another Terraform workspace created it β different state files
Fix 1: Import the existing resource
terraform import aws_s3_bucket.my_bucket my-bucket-name
terraform import aws_instance.my_server i-1234567890abcdef0
After importing, run terraform plan to verify the config matches the actual resource.
Fix 2: Remove from state and recreate
# If you want Terraform to manage a fresh resource
terraform state rm aws_s3_bucket.my_bucket
# Then delete the actual resource manually, then run terraform apply
Fix 3: Use data sources instead
If you donβt want Terraform to manage the resource, reference it as a data source:
data "aws_s3_bucket" "existing" {
bucket = "my-existing-bucket"
}
Alternative solutions
Use import blocks (Terraform 1.5+) for declarative imports without CLI commands:
import {
to = aws_s3_bucket.my_bucket
id = "my-bucket-name"
}
Prevention
- Never create resources manually that Terraform manages. Use
terraform applyfor all changes. - Use remote state (S3 + DynamoDB) to prevent state conflicts across teams.
Related: Terraform cheat sheet Β· terraform init Failed Β· Terraform State Lock Error Β· Terraform vs Pulumi