Error: creating: Conflict with ...
Terraform’s “Conflict with” error means your cloud provider rejected a resource creation because the same ID already exists outside Terraform state.
What It Means
The API call from Terraform to the cloud provider returned a conflict error. For example, creating an AWS S3 bucket with a name that already exists globally, or creating a Kubernetes namespace that another process already created. The resource exists in the cloud but not in Terraform’s state.
Why It Happens
- A resource similar to what Terraform is creating already exists in the cloud account.
- The resource was created manually through the cloud console or CLI.
- Another Terraform workspace or configuration manages the same resource.
- A previous
terraform destroywas interrupted, leaving the resource alive. - The resource has a globally unique name constraint (like S3 buckets or IAM roles).
- The
importstep was skipped when adopting existing infrastructure.
How to Fix It
Step 1: Identify the conflicting resource
The error message includes the resource details. For example:
Error: creating S3 Bucket: BucketAlreadyExists: The requested bucket name is not available.Note the resource type and name from the error output.
Step 2: Import the existing resource into Terraform state
If the resource already exists and you want Terraform to manage it:
terraform import <resource_type>.<name> <resource_id>Example for an AWS S3 bucket:
terraform import aws_s3_bucket.my_bucket my-existing-bucketStep 3: Remove the resource from configuration and state
If you don’t want Terraform to manage this resource, remove it from your .tf files and remove it from state:
terraform state rm <resource_type>.<name>Step 4: Use a unique name or generate one
For resources with global name constraints, use a random or prefixed name:
resource "aws_s3_bucket" "example" {
bucket = "my-app-${random_id.suffix.hex}"
}
resource "random_id" "suffix" {
byte_length = 4
}Step 5: Use lifecycle prevent_destroy for protection
To prevent accidental deletion of critical resources:
resource "aws_s3_bucket" "critical" {
bucket = "my-critical-bucket"
lifecycle {
prevent_destroy = true
}
}Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro