Skip to content

Commit

Permalink
fix(aws-s3) Fix tag support for custom resource lambdas (#1724)
Browse files Browse the repository at this point in the history
Add tag support to the inline Lambda by extending `cdk.Resource` and
adding tags. Implemented `renderProperties` to properly format tags.

Fixes #1497
  • Loading branch information
moofish32 authored and Elad Ben-Israel committed Feb 11, 2019
1 parent 13f3df5 commit 016a5d6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,19 @@ export class NotificationsResourceHandler extends cdk.Construct {
.addAction('s3:PutBucketNotification')
.addAllResources());

const resource = new cdk.Resource(this, 'Resource', {
type: 'AWS::Lambda::Function',
const resourceType = 'AWS::Lambda::Function';
class InLineLambda extends cdk.Resource {
public readonly tags: cdk.TagManager = new cdk.TagManager(cdk.TagType.Standard, resourceType);

protected renderProperties(properties: any): { [key: string]: any } {
properties.Tags = cdk.listMapper(
cdk.cfnTagToCloudFormation)(this.tags.renderTags());
delete properties.tags;
return properties;
}
}
const resource = new InLineLambda(this, 'Resource', {
type: resourceType,
properties: {
Description: 'AWS CloudFormation handler for "Custom::S3BucketNotifications" resources (@aws-cdk/aws-s3)',
Code: { ZipFile: `exports.handler = ${handler.toString()};` },
Expand Down
18 changes: 18 additions & 0 deletions packages/@aws-cdk/aws-s3/test/test.notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,24 @@ export = {

test.done();
},
'when notification are added, you can tag the lambda'(test: Test) {
const stack = new cdk.Stack();
stack.apply(new cdk.Tag('Lambda', 'AreTagged'));

const bucket = new s3.Bucket(stack, 'MyBucket');

const topic = new Topic(stack, 'MyTopic');

bucket.onEvent(s3.EventType.ObjectCreated, topic);

expect(stack).to(haveResource('AWS::S3::Bucket'));
expect(stack).to(haveResource('AWS::Lambda::Function', {
Tags: [{Key: 'Lambda', Value: 'AreTagged'}],
Description: 'AWS CloudFormation handler for "Custom::S3BucketNotifications" resources (@aws-cdk/aws-s3)' }));
expect(stack).to(haveResource('Custom::S3BucketNotifications'));

test.done();
},

'bucketNotificationTarget is not called during synthesis'(test: Test) {
const stack = new cdk.Stack();
Expand Down

0 comments on commit 016a5d6

Please sign in to comment.