This is a piece of cake for most of you that are working on tf, I will drop this snippet code where you can create a S3 bucket with an unique name; as you know those requirements should fit in order to create the S3, hence we can use random_string, also we can use random_id, on this case I used random_string.

main.tf

resource "random_string" "randomz" {
  length = 8
  lower  = true
  special = false
  upper = false
}

Above is a resource block where its block label is random_string I defined the block body in that way in order to fit the S3’s requirements.

Then you can call this resource to your S3 block label as below:

resource "aws_s3_bucket" "my_s3_1" {
  bucket = "my-bucket-${random_string.randomz.id}"

  tags = {
    Name = "my bucket"
    Terraform = "true"
  }
}

resource "aws_s3_bucket_acl" "my_s3_1_acl" {
  bucket = aws_s3_bucket.my_s3_1.id
  acl = "private"
}

it’s pretty simple, hope it can be useful for you.