Quick Answer
# Add account ID for uniqueness
data "aws_caller_identity" "current" {}
resource "aws_s3_bucket" "data" {
bucket = "myapp-data-${data.aws_caller_identity.current.account_id}"
}
The Error
Error: creating S3 Bucket (my-bucket): BucketAlreadyExists:
The requested bucket name is not available. The bucket namespace is
shared by all users of the system.
Or:
Error: creating S3 Bucket (my-bucket): BucketAlreadyOwnedByYou:
Your previous request to create the named bucket succeeded and you already own it.
What Causes This
S3 bucket names are globally unique across ALL AWS accounts worldwide. If anyone in any account has my-bucket, nobody else can create it.
BucketAlreadyExists— someone else owns this nameBucketAlreadyOwnedByYou— you already have it (import instead of create)
Solution 1: Use Account ID in Name
data "aws_caller_identity" "current" {}
resource "aws_s3_bucket" "data" {
bucket = "myapp-data-${data.aws_caller_identity.current.account_id}"
# Result: myapp-data-123456789012
}
Solution 2: Use Random Suffix
resource "random_id" "bucket" {
byte_length = 4
}
resource "aws_s3_bucket" "data" {
bucket = "myapp-data-${random_id.bucket.hex}"
# Result: myapp-data-a1b2c3d4
}
Solution 3: Naming Convention
locals {
bucket_prefix = "${var.company}-${var.project}-${var.environment}-${var.region}"
}
resource "aws_s3_bucket" "data" {
bucket = "${local.bucket_prefix}-data"
# Result: acme-webapp-prod-us-east-1-data
}
resource "aws_s3_bucket" "logs" {
bucket = "${local.bucket_prefix}-logs"
}
Solution 4: Import Existing Bucket
If BucketAlreadyOwnedByYou — you already own it:
terraform import aws_s3_bucket.data my-existing-bucket
terraform plan # Should show no changes
Solution 5: Use bucket_prefix
Let AWS generate a unique suffix:
resource "aws_s3_bucket" "data" {
bucket_prefix = "myapp-data-"
# Result: myapp-data-20260412abc123 (auto-generated suffix)
}
Note: the full name is only known after apply.
S3 Bucket Naming Rules
| Rule | Valid | Invalid |
|---|---|---|
| Lowercase only | my-bucket | My-Bucket |
| 3-63 characters | app | ab (too short) |
| No underscores | my-bucket | my_bucket |
| No periods (recommended) | my-bucket | my.bucket (breaks HTTPS) |
| Start with letter/number | app-logs | -app-logs |
| Not IP format | my-bucket | 192.168.1.1 |
Hands-On Courses
- Terraform for Beginners on CopyPasteLearn
- Terraform By Example — practical code examples
Conclusion
S3 bucket names must be globally unique across all AWS accounts. Use account ID, random suffix, or a naming convention with company/project/env/region to guarantee uniqueness. If you already own the bucket, import it with terraform import.
