Skip to content

Instantly share code, notes, and snippets.

@i2key
Last active February 3, 2016 15:02
Show Gist options
  • Save i2key/8139de268b4b71bdcf41 to your computer and use it in GitHub Desktop.
Save i2key/8139de268b4b71bdcf41 to your computer and use it in GitHub Desktop.
cloudfrontでwebホスティングしてるときにJS等の特定のファイルのS3へのputObjectをトリガーにそのファイルのキャッシュを消去するAWS Lambda
var aws = require('aws-sdk');
var cloudfront = new aws.CloudFront();
exports.handler = function(event, context) {
var bucket = event.Records[0].s3.bucket.name;
var key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' '));
var params = {
DistributionId: 'CloudFrontのDistibutionId',
InvalidationBatch: {
CallerReference: 'LAMBDA' + new Date().getTime(),
Paths: {
Quantity: 1,
Items: [
'/'+key
]
}
}
};
cloudfront.createInvalidation(params, function(err, data) {
if (err) {
console.log(err, err.stack);
context.fail();
}else{
console.log(data);
context.succeed();
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment