Cloudfront Create Invalidation Using Lambda nodejs
S3에 html/js 업로드를 한 후에 강제로 빠르게 적용시킬때, 웹 콘솔에서도 가능하겠지만, 배포 후에 강제로 깅제로 적용시키려면 아래 lambda 를 codepipleline 내의 deploy 이후에 수행되도록 한다.
var AWS = require('aws-sdk');
exports.handler = async(event) => {
const cloudfront = new AWS.CloudFront();
const timestamp = new Date().getTime() + '';
const invalidationParams = {
DistributionId: 'XXXXXXXXXXXX', /* required */
InvalidationBatch: { /* required */
CallerReference: timestamp, /* required */
Paths: { /* required */
Quantity: 1, /* required */
Items: [
'/*',
/* more items */
]
}
}
};
const codepipeline = new AWS.CodePipeline()
console.log(JSON.stringify(event, null, 2))
var jobId = event["CodePipeline.job"].id
var params = {
jobId: jobId
}
try {
const response = await cloudfront.createInvalidation(invalidationParams).promise();
console.info('성공')
console.log(response);
return codepipeline.putJobSuccessResult(params).promise()
} catch(ex) {
console.info('실패')
console.error(ex);
return codepipeline.PutJobFailureResult(params).promise()
}
}
Read other posts