Skip to content

Commit

Permalink
feat(synthetics): lifecycle rules for auto-generated artifact buckets (
Browse files Browse the repository at this point in the history
…aws#26290)

A re-roll of aws#22863. Thanks for getting this started @mackensen!

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

Co-authored-by:  Charles Fulton fultonc@lafayette.edu

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
kaizencc authored and bmoffatt committed Jul 28, 2023
1 parent 78bfe33 commit 0790b3f
Show file tree
Hide file tree
Showing 13 changed files with 9,746 additions and 10,824 deletions.
24 changes: 21 additions & 3 deletions packages/@aws-cdk/aws-synthetics-alpha/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ To illustrate how to use a canary, assume your application defines the following
```console
% curl "https://api.example.com/user/books/topbook/"
The Hitchhikers Guide to the Galaxy

```

The below code defines a canary that will hit the `books/topbook` endpoint every 5 minutes:
Expand Down Expand Up @@ -81,7 +80,6 @@ The Canary code will be executed in a lambda function created by Synthetics on y

To learn more about Synthetics capabilities, check out the [docs](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Synthetics_Canaries.html).


### Canary Schedule

You can specify the schedule on which a canary runs by providing a
Expand All @@ -104,7 +102,6 @@ const schedule = synthetics.Schedule.cron({

If you want the canary to run just once upon deployment, you can use `Schedule.once()`.


### Canary DeleteLambdaResourcesOnCanaryDeletion

You can specify whether the AWS CloudFormation is to also delete the Lambda functions and layers used by this canary, when the canary is deleted.
Expand Down Expand Up @@ -237,3 +234,24 @@ new cloudwatch.Alarm(this, 'CanaryAlarm', {
comparisonOperator: cloudwatch.ComparisonOperator.LESS_THAN_THRESHOLD,
});
```

### Artifacts

You can pass an S3 bucket to store artifacts from canary runs. If you do not,
one will be auto-generated when the canary is created. You may add
[lifecycle rules](https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-lifecycle-mgmt.html)
to the auto-generated bucket.

```ts
const canary = new synthetics.Canary(this, 'MyCanary', {
schedule: synthetics.Schedule.rate(Duration.minutes(5)),
test: synthetics.Test.custom({
code: synthetics.Code.fromAsset(path.join(__dirname, 'canary')),
handler: 'index.handler',
}),
runtime: synthetics.Runtime.SYNTHETICS_NODEJS_PUPPETEER_4_0,
artifactsBucketLifecycleRules: [{
expiration: Duration.days(30),
}],
});
```
11 changes: 11 additions & 0 deletions packages/@aws-cdk/aws-synthetics-alpha/lib/canary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,16 @@ export interface CanaryProps {
* @default false
*/
readonly enableAutoDeleteLambdas?: boolean;

/**
* Lifecycle rules for the generated canary artifact bucket. Has no effect
* if a bucket is passed to `artifactsBucketLocation`. If you pass a bucket
* to `artifactsBucketLocation`, you can add lifecycle rules to the bucket
* itself.
*
* @default - no rules applied to the generated bucket.
*/
readonly artifactsBucketLifecycleRules?: Array<s3.LifecycleRule>;
}

/**
Expand Down Expand Up @@ -269,6 +279,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
29 changes: 29 additions & 0 deletions packages/@aws-cdk/aws-synthetics-alpha/test/canary.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,35 @@ test('An existing role can be specified instead of auto-created', () => {
});
});

test('An auto-generated bucket can have lifecycle rules', () => {
// GIVEN
const stack = new Stack();
const lifecycleRules = [{
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_NODEJS_PUPPETEER_3_8,
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::S3::Bucket', {
LifecycleConfiguration: {
Rules: [
{
ExpirationInDays: 30,
},
],
},
});
});

test('An existing bucket and prefix can be specified instead of auto-created', () => {
// GIVEN
const stack = new Stack();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
{
"version": "32.0.0",
"files": {
"86e6ecbc86f6febc745cde7ce633979efa869d6d493b12f2c9d8641d70caa7c2": {
"fe2373df587bda7e4bf68910b7be30b2042493413a6f6f2c43efab4f184d3bad": {
"source": {
"path": "asset.86e6ecbc86f6febc745cde7ce633979efa869d6d493b12f2c9d8641d70caa7c2.bundle",
"path": "asset.fe2373df587bda7e4bf68910b7be30b2042493413a6f6f2c43efab4f184d3bad.bundle",
"packaging": "zip"
},
"destinations": {
"current_account-current_region": {
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
"objectKey": "86e6ecbc86f6febc745cde7ce633979efa869d6d493b12f2c9d8641d70caa7c2.zip",
"objectKey": "fe2373df587bda7e4bf68910b7be30b2042493413a6f6f2c43efab4f184d3bad.zip",
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
}
}
},
"802ff37ba76829d10db21bce3564f84bf2d4fb57f7db19c7e2534fb1dce44680": {
"5c382b4e425866a69d59a5cffba84fb128c5482ea37a4da5b0ddb7a5a803d15e": {
"source": {
"path": "IntegCanaryTestDefaultTestDeployAssert3AD5A094.template.json",
"packaging": "file"
},
"destinations": {
"current_account-current_region": {
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
"objectKey": "802ff37ba76829d10db21bce3564f84bf2d4fb57f7db19c7e2534fb1dce44680.json",
"objectKey": "5c382b4e425866a69d59a5cffba84fb128c5482ea37a4da5b0ddb7a5a803d15e.json",
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"outputPaths": [
"CanaryRuns.0.Status.State"
],
"salt": "1688622716625"
"salt": "1688756012414"
},
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete"
Expand Down Expand Up @@ -295,7 +295,7 @@
"S3Bucket": {
"Fn::Sub": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}"
},
"S3Key": "86e6ecbc86f6febc745cde7ce633979efa869d6d493b12f2c9d8641d70caa7c2.zip"
"S3Key": "fe2373df587bda7e4bf68910b7be30b2042493413a6f6f2c43efab4f184d3bad.zip"
},
"Timeout": 120,
"Handler": "index.handler",
Expand Down Expand Up @@ -401,7 +401,7 @@
"S3Bucket": {
"Fn::Sub": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}"
},
"S3Key": "86e6ecbc86f6febc745cde7ce633979efa869d6d493b12f2c9d8641d70caa7c2.zip"
"S3Key": "fe2373df587bda7e4bf68910b7be30b2042493413a6f6f2c43efab4f184d3bad.zip"
},
"Timeout": 120,
"Handler": "index.isComplete",
Expand Down Expand Up @@ -443,7 +443,7 @@
"S3Bucket": {
"Fn::Sub": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}"
},
"S3Key": "86e6ecbc86f6febc745cde7ce633979efa869d6d493b12f2c9d8641d70caa7c2.zip"
"S3Key": "fe2373df587bda7e4bf68910b7be30b2042493413a6f6f2c43efab4f184d3bad.zip"
},
"Timeout": 120,
"Handler": "index.onTimeout",
Expand Down Expand Up @@ -480,7 +480,7 @@
"outputPaths": [
"CanaryRuns.0.Status.State"
],
"salt": "1688622716626"
"salt": "1688756012414"
},
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete"
Expand Down Expand Up @@ -627,7 +627,7 @@
"outputPaths": [
"CanaryRuns.0.Status.State"
],
"salt": "1688622716626"
"salt": "1688756012415"
},
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete"
Expand Down Expand Up @@ -774,7 +774,7 @@
"outputPaths": [
"CanaryRuns.0.Status.State"
],
"salt": "1688622716626"
"salt": "1688756012415"
},
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete"
Expand Down Expand Up @@ -921,7 +921,7 @@
"outputPaths": [
"CanaryRuns.0.Status.State"
],
"salt": "1688622716627"
"salt": "1688756012415"
},
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete"
Expand Down Expand Up @@ -1068,7 +1068,7 @@
"outputPaths": [
"CanaryRuns.0.Status.State"
],
"salt": "1688622716627"
"salt": "1688756012416"
},
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete"
Expand Down

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0790b3f

Please sign in to comment.