Random S3 Bucket Name Generator with AWS STS

  • Share this:

Code introduction


This function uses the boto3 library to access the AWS STS client, which is used to retrieve the current AWS account ID. It then generates a random S3 bucket name, composed of letters and numbers, ensuring uniqueness.


Technology Stack : boto3, AWS STS

Code Type : Function

Code Difficulty : Intermediate


                
                    
import boto3
import random

def generate_random_s3_bucket_name():
    client = boto3.client('sts')  # Using AWS STS to get the account ID
    account_id = client.get_caller_identity()['Account']
    random_suffix = ''.join(random.choices('abcdefghijklmnopqrstuvwxyz0123456789', k=10))
    return f"bucket-{account_id}-{random_suffix}"                
              
Tags: