Skip to content

Instantly share code, notes, and snippets.

@lporras
Created October 6, 2023 15:08
Show Gist options
  • Save lporras/b698935da406cb548a4bf27fdfaa95c4 to your computer and use it in GitHub Desktop.
Save lporras/b698935da406cb548a4bf27fdfaa95c4 to your computer and use it in GitHub Desktop.
Create Buckets in s3 with CloudFormation
import * as cdk from 'aws-cdk-lib';
import { CfnOutput } from 'aws-cdk-lib';
import { Bucket, CfnBucket } from 'aws-cdk-lib/aws-s3';
import { Construct } from 'constructs';
class L3Bucket extends Construct {
constructor(scope: Construct, id: string, expiration: number){
super(scope, id);
new Bucket(this, id, {
lifecycleRules: [{
expiration: cdk.Duration.days(expiration)
}]
});
}
}
export class CdkStarterStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// create an s3 bucket 3 ways:
// L1 constract
new CfnBucket(this, 'MyL1Bucket', {
lifecycleConfiguration: {
rules: [{
expirationInDays: 1,
status: 'Enabled'
}]
}
});
const duration = new cdk.CfnParameter(this, 'duration', {
default: 6,
minValue: 1,
maxValue: 10,
type: 'Number'
})
// L2 Constract
const myL2Bucket = new Bucket(this, 'MyL2Bucket', {
lifecycleRules: [{
expiration: cdk.Duration.days(duration.valueAsNumber)
}]
});
//console.log('bucket name: ' + myL2Bucket.bucketName)
new CfnOutput(this, 'MyL2BucketName', {
value: myL2Bucket.bucketName
})
// L3 Constract
new L3Bucket(this, 'MyL3Bucket', 3);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment