Last active
August 5, 2019 18:51
-
-
Save kevinslin/659f7098032ba8595c3078b58dd61261 to your computer and use it in GitHub Desktop.
Example of bucket notifications in CDK
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const bucket = new Bucket(this, 'fooBucket'); | |
const fn = new lambda.Function(this, 'fooFunc', { | |
runtime: lambda.Runtime.NODEJS_10_X, | |
handler: 'lambda.handler', | |
code: lambda.Code.asset('functions/fooFunc'), | |
timeout: Duration.seconds(60) | |
}); | |
bucket.addEventNotification( | |
EventType.OBJECT_CREATED, | |
new s3n.LambdaDestination(fn) | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
BucketNotificationsHandler050a0587b7544547bf325f094a3db8347ECC3691: | |
Type: AWS::Lambda::Function | |
Properties: | |
Description: AWS CloudFormation handler for "Custom::S3BucketNotifications" resources (@aws-cdk/aws-s3) | |
Code: | |
ZipFile: >- | |
exports.handler = (event, context) => { | |
const s3 = new (require('aws-sdk').S3)(); | |
const https = require("https"); | |
const url = require("url"); | |
log(JSON.stringify(event, undefined, 2)); | |
const props = event.ResourceProperties; | |
if (event.RequestType === 'Delete') { | |
props.NotificationConfiguration = {}; // this is how you clean out notifications | |
} | |
const req = { | |
Bucket: props.BucketName, | |
NotificationConfiguration: props.NotificationConfiguration | |
}; | |
return s3.putBucketNotificationConfiguration(req, (err, data) => { | |
log({ err, data }); | |
if (err) { | |
return submitResponse("FAILED", err.message + `\nMore information in CloudWatch Log Stream: ${context.logStreamName}`); | |
} | |
else { | |
return submitResponse("SUCCESS"); | |
} | |
}); | |
function log(obj) { | |
console.error(event.RequestId, event.StackId, event.LogicalResourceId, obj); | |
} | |
// tslint:disable-next-line:max-line-length | |
// adapted from https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-lambda-function-code.html#cfn-lambda-function-code-cfnresponsemodule | |
// to allow sending an error messge as a reason. | |
function submitResponse(responseStatus, reason) { | |
const responseBody = JSON.stringify({ | |
Status: responseStatus, | |
Reason: reason || "See the details in CloudWatch Log Stream: " + context.logStreamName, | |
PhysicalResourceId: context.logStreamName, | |
StackId: event.StackId, | |
RequestId: event.RequestId, | |
LogicalResourceId: event.LogicalResourceId, | |
NoEcho: false, | |
}); | |
log({ responseBody }); | |
const parsedUrl = url.parse(event.ResponseURL); | |
const options = { | |
hostname: parsedUrl.hostname, | |
port: 443, | |
path: parsedUrl.path, | |
method: "PUT", | |
headers: { | |
"content-type": "", | |
"content-length": responseBody.length | |
} | |
}; | |
const request = https.request(options, (r) => { | |
log({ statusCode: r.statusCode, statusMessage: r.statusMessage }); | |
context.done(); | |
}); | |
request.on("error", (error) => { | |
log({ sendError: error }); | |
context.done(); | |
}); | |
request.write(responseBody); | |
request.end(); | |
} | |
}; | |
Handler: index.handler | |
Role: | |
Fn::GetAtt: | |
- BucketNotificationsHandler050a0587b7544547bf325f094a3db834RoleB6FB88EC | |
- Arn | |
Runtime: nodejs8.10 | |
Timeout: 300 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment