Make Your Id Unique

RobertoTorinoRobertoTorino
1 min read

There are situations where you want uniqueness but you can not use numbers only letters to distinguish uniqueness, here's an example on how to achieve this:

export function makeUnique() {
    let text = '';
    const rangeChoice = 'abcdefghijklmnopqrstuvwxyz';
    for (let i = 0; i < 5; i++)
        text += rangeChoice.charAt(Math.floor(Math.random() * rangeChoice.length));
    return text;
}

Use it in your solution:

export class BucketStack extends Stack {
    private readonly artifactBucket: IBucket
    constructor(scope: Construct, id: string, props: BucketStackProps) {
        super(scope, id, props);

        this.artifactBucket = new aws_s3.Bucket(this, 'artifact-bucket', {
            bucketName: artifact-bucket-${makeUnique()}`,
            lifecycleRules: [{
                expiration: Duration.days(1),
                enabled: true,
            }],
            versioned: true,
            blockPublicAccess: BlockPublicAccess.BLOCK_ALL,
            removalPolicy: RemovalPolicy.DESTROY,
            autoDeleteObjects: true,
            enforceSSL: true,
            serverAccessLogsBucket: resourcesBucket,
            encryption: BucketEncryption.S3_MANAGED,
        });
    }
}

Stay Safe ☘️

0
Subscribe to my newsletter

Read articles from RobertoTorino directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

RobertoTorino
RobertoTorino

Never think you know it all… OPS Engineer. Working on a secure and robust cloud. Professional Scrum Master.