Skip to content

Commit

Permalink
feat(synthetics): expose lifecycle rules for auto-generated artifact …
Browse files Browse the repository at this point in the history
…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 aws#22634
  • Loading branch information
mackensen committed Nov 10, 2022
1 parent 93915f1 commit 7d6d05a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/@aws-cdk/aws-synthetics/lib/canary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<s3.LifecycleRule>;

/**
* The s3 location that stores the data of the canary runs.
*
Expand Down Expand Up @@ -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);
Expand Down
31 changes: 31 additions & 0 deletions packages/@aws-cdk/aws-synthetics/test/canary.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<s3.LifecycleRule> = [
{
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();
Expand Down
5 changes: 5 additions & 0 deletions packages/@aws-cdk/aws-synthetics/test/integ.canary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand Down

0 comments on commit 7d6d05a

Please sign in to comment.