-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlambda.js
38 lines (35 loc) · 995 Bytes
/
lambda.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
'use strict';
// CONFIGURATION //////////////////////////////////////////////
var CloudFrontID = 'DISTRIBUTION ID';
///////////////////////////////////////////////////////////////
let aws = require('aws-sdk');
let s3 = new aws.S3();
let cloudfront = new aws.CloudFront();
// S3
exports.handler = (event, context, callback) => {
const bucket = event.Records[0].s3.bucket.name;
const key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' '));
// Cloudfront
params = {
DistributionId: CloudFrontID,
InvalidationBatch: {
CallerReference: new Date().getTime().toString(),
Paths: {
Quantity: 1,
Items: [
'/'+key,
]
}
}
};
cloudfront.createInvalidation(params, function(err, data) {
if (err) {
console.log(err, err.stack);
message = 'ERROR: Failed to invalidate object: s3://'+bucket+'/'+key;
console.log(message);
} else {
message = 'Object invalidated successfully: s3://'+bucket+'/'+key;
console.log(message);
}
});
};