Terraform "creating S3 Bucket: BucketAlreadyExists" in CI
S3 bucket names are globally unique across all AWS accounts. Terraform tried to create a bucket whose name is already owned -- by someone else (BucketAlreadyExists) or by you (BucketAlreadyOwnedByYou).
What this error means
apply fails creating an aws_s3_bucket with BucketAlreadyExists or BucketAlreadyOwnedByYou. It surfaces when a static bucket name collides globally, or when a bucket you already own is not in Terraform state.
Error: creating S3 Bucket (my-app-bucket): operation error S3: CreateBucket,
https response error StatusCode: 409, BucketAlreadyExists: The requested bucket
name is not available. The bucket namespace is shared by all users of the system.
with aws_s3_bucket.assets,
on s3.tf line 1, in resource "aws_s3_bucket" "assets":Common causes
Name already taken globally
S3 bucket names share one global namespace; a common static name is likely owned by another account already.
Bucket you own is not in state
BucketAlreadyOwnedByYou means the bucket exists in your account but Terraform has no record of it, so it tries to create it.
How to fix it
Make the name unique or use a prefix
Add an account/region suffix or let AWS generate the name with bucket_prefix.
data "aws_caller_identity" "current" {}
resource "aws_s3_bucket" "assets" {
bucket = "my-app-assets-${data.aws_caller_identity.current.account_id}"
}
# or: bucket_prefix = "my-app-assets-"Import a bucket you already own
- If you own the bucket (BucketAlreadyOwnedByYou), import it instead of recreating.
- Run terraform import aws_s3_bucket.assets my-app-bucket.
- Re-run plan to confirm no recreation is proposed.
How to prevent it
- Derive bucket names from account ID/region for global uniqueness.
- Use bucket_prefix to let AWS append a unique suffix.
- Import pre-existing buckets rather than letting apply recreate them.