From 8e8cf854648e8b471ae15f3753d3ca46982dccf2 Mon Sep 17 00:00:00 2001 From: Charles Fulton Date: Thu, 10 Nov 2022 09:59:39 -0500 Subject: [PATCH] feat(synthetics): expose lifecycle rules for auto-generated aritfact buckets This change exposes the lifecycle rules property for new and existing auto-generated artifact buckets so that workload owners can easily manage growth. fixes #22634 --- .../@aws-cdk/aws-synthetics/lib/canary.ts | 9 ++++++ .../aws-synthetics/test/canary.test.ts | 31 +++++++++++++++++++ .../aws-synthetics/test/integ.canary.ts | 5 +++ 3 files changed, 45 insertions(+) diff --git a/packages/@aws-cdk/aws-synthetics/lib/canary.ts b/packages/@aws-cdk/aws-synthetics/lib/canary.ts index 717bfcbe6082d..e5e06f331ca59 100644 --- a/packages/@aws-cdk/aws-synthetics/lib/canary.ts +++ b/packages/@aws-cdk/aws-synthetics/lib/canary.ts @@ -88,6 +88,14 @@ export interface ArtifactsBucketLocation { * Properties for a canary */ export interface CanaryProps { + /** + * Lifecycle rules for the generated canary artifact bucket. + * + * @default - No rules applied. This has no effect if a bucket is passed + * to `artifactsBucketLocation`. + */ + readonly artifactsBucketLifecycleRules?: Array; + /** * The s3 location that stores the data of the canary runs. * @@ -260,6 +268,7 @@ export class Canary extends cdk.Resource implements ec2.IConnectable { this.artifactsBucket = props.artifactsBucketLocation?.bucket ?? new s3.Bucket(this, 'ArtifactsBucket', { encryption: s3.BucketEncryption.KMS_MANAGED, enforceSSL: true, + lifecycleRules: props.artifactsBucketLifecycleRules, }); this.role = props.role ?? this.createDefaultRole(props); diff --git a/packages/@aws-cdk/aws-synthetics/test/canary.test.ts b/packages/@aws-cdk/aws-synthetics/test/canary.test.ts index aee1ba78cd134..ec3de1acbd8c4 100644 --- a/packages/@aws-cdk/aws-synthetics/test/canary.test.ts +++ b/packages/@aws-cdk/aws-synthetics/test/canary.test.ts @@ -151,6 +151,37 @@ test('An existing bucket and prefix can be specified instead of auto-created', ( }); }); +test('An auto-generated bucket has lifecycle rules', () => { + // GIVEN + const stack = new Stack(); + const lifecycleRules: Array = [ + { + expiration: Duration.days(30), + }, + ]; + + // WHEN + new synthetics.Canary(stack, 'Canary', { + artifactsBucketLifecycleRules: lifecycleRules, + test: synthetics.Test.custom({ + handler: 'index.handler', + code: synthetics.Code.fromInline('/* Synthetics handler code */'), + }), + runtime: synthetics.Runtime.SYNTHETICS_1_0, + }); + + // THEN + Template.fromStack(stack).hasResourceProperties('AWS::S3::Bucket', { + LifecycleConfiguration: { + Rules: [ + { + ExpirationInDays: 30, + }, + ], + }, + }); +}); + test('Runtime can be specified', () => { // GIVEN const stack = new Stack(); diff --git a/packages/@aws-cdk/aws-synthetics/test/integ.canary.ts b/packages/@aws-cdk/aws-synthetics/test/integ.canary.ts index bd5963acf77e6..0ac2ae1200e91 100644 --- a/packages/@aws-cdk/aws-synthetics/test/integ.canary.ts +++ b/packages/@aws-cdk/aws-synthetics/test/integ.canary.ts @@ -41,6 +41,11 @@ new synthetics.Canary(stack, 'MyCanaryOne', { code: synthetics.Code.fromAsset(path.join(__dirname, 'canaries')), }), runtime: synthetics.Runtime.SYNTHETICS_NODEJS_PUPPETEER_3_2, + artifactsBucketLifecycleRules: [ + { + expiration: cdk.Duration.days(30), + }, + ], }); new synthetics.Canary(stack, 'MyCanaryTwo', {