Choosing an AWS bucket name and how to create a random bucket name
An Amazon S3 bucket name must be globally unique, as the S3 namespace is shared with all AWS accounts. This means no two buckets should have the same name.
By using Terraform, you can achieve this using the random_id
resource. byte_length
defines the number of random bytes to produce, and in this case, there are 8 bits of random bytes, which means it will add 8 extra bits at the end of bucket, as illustrated in the following code snippet:
resource "random_id" "my-random-id" { byte_length = 8 }
Then, you can pass random_id
to the aws_s3_bucket
resource to add randomness to the bucket, as illustrated in the following code snippet:
resource "aws_s3_bucket" "my-bucket" { bucket = "my-bucket-${random_id.my-random-id.dec}" }
By choosing the random_id
resource, you can simplify and automate your S3 bucket random bucket name creation.