I'm facing issues with the creation of Cloudfront invalidations from AWS Lambda.
My case is pretty basic: I have set up a Lambda handler to be triggered by specific S3 objects creations and removals, in order to perform invalidation of cached versions on my Cloudfront distribution. This is the function code, written using nodejs:
const AWS = require('aws-sdk');
exports.handler = async function (event, context) {
const cloudFront = new AWS.CloudFront();
const invalidationParams = {
DistributionId: "XXXX",
InvalidationBatch: {
CallerReference: Date.now().toString(),
Paths: {
Quantity: 2,
Items: [
"/index.html",
"/service-worker.js"
]
}
}
};
cloudFront.createInvalidation(invalidationParams, (error, data) => {
if (error) {
console.log(error, error.stack);
} else {
console.log("Invalidation results", data);
}
});
};
As you can see, nothing too complex. Now, most of the times the handler executes without doing anything, I'm watching logs and nothing gets printed aside from the request id and start and end timestamps, not even Cloudfront errors, which gets me wondering what's going on. After four or five consecutive manual test executions, an invalidation is correctly created, but logs don't report it. Firing it an additional time then prints the invalidation results for the previous run. I find this very odd and confusing.
Is there something I may be missing, judging from context and Lambda code?
Thank you.