-
-
Save j3tm0t0/fa6b2bf23cbf454d26e8 to your computer and use it in GitHub Desktop.
CloudFront Invalidation using Lambda via SNS
This file contains 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
console.log('Loading event'); | |
var Q = require('q'); | |
var aws = require('aws-sdk'); | |
var cloudfront = new aws.CloudFront(); | |
exports.handler = function (event, context) { | |
_log('Received event: ', event); | |
if (event.Records[0].EventSource == "aws:sns") | |
{ | |
if(event.Records[0].Sns.Subject == "Amazon S3 Notification") | |
{ // overwrite S3 event object from payload | |
event=JSON.parse(event.Records[0].Sns.Message); | |
} | |
} | |
if(event.Records[0].eventSource != "aws:s3") | |
{ | |
context.fail('unexpected event'); | |
} | |
else | |
{ | |
var bucket = event.Records[0].s3.bucket.name; | |
var key = event.Records[0].s3.object.key; | |
console.log('Bucket: '+bucket); | |
console.log('Key: '+key); | |
cloudfront.listDistributions({}, function (err, data) { | |
var promises = []; | |
if (err) { | |
_log('Error: ', err); | |
context.fail(err); | |
return; | |
} | |
// Find a bucket which uses the backet as a origin. | |
data.Items.map(function (distribution) { | |
var deferred = Q.defer(); | |
var exists = false; | |
distribution.Origins.Items.map(function (origin) { | |
if (exists) return; | |
if (origin.DomainName.indexOf(bucket) === 0) { | |
exists = true; | |
var name = distribution.DomainName; | |
if (distribution.Aliases.Quantity > 0) { | |
name = distribution.Aliases.Items[0]; | |
} | |
console.log('Distribution: ' + distribution.Id + ' ('+ name + ')'); | |
// Parameters for a invalidation | |
var params = { | |
DistributionId : distribution.Id, | |
InvalidationBatch : { | |
CallerReference : '' + new Date().getTime(), | |
Paths : { | |
Quantity : 1, | |
Items : [ '/'+key ] | |
} | |
} | |
}; | |
_log('Params: ', params); | |
// Invalidate | |
cloudfront.createInvalidation(params, function (err, data) { | |
if (err) { | |
_log('Error: ', err); | |
deferred.reject(); | |
return; | |
} | |
_log('Success: ', data.InvalidationBatch); | |
deferred.resolve(); | |
}); | |
} | |
}); | |
if (! exists) deferred.resolve(); | |
promises.push(deferred.promise); | |
}); | |
Q.all(promises).then(function() { | |
context.succeed(''); | |
}); | |
}); | |
} | |
function _log(caption, object) { | |
console.log(caption + JSON.stringify(object, true, ' ')); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment