Last active
February 3, 2016 15:02
-
-
Save i2key/8139de268b4b71bdcf41 to your computer and use it in GitHub Desktop.
cloudfrontでwebホスティングしてるときにJS等の特定のファイルのS3へのputObjectをトリガーにそのファイルのキャッシュを消去するAWS Lambda
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
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