问题
I have 10-50 files upload at the same time. Lambda triggers an event for every updated file (if file size changed).
So instead of invalidating one by one 50x I'd like to run the wildcard “*”
character once at the end of the upload.
Anyone can shed some light on how to accomplish that? Thanks!
回答1:
If I understood your question correctly, your lambda function knows whether CloudFront should invalidate or not but does not know when should that happen exactly.
In that case, you could return a boolean flag (e.g., mustInvalidate
) as part of the response to indicate that an invalidation request should be created. Then, your application (or whatever initiated the batch upload request) can create the /target-location/*
invalidation request as soon as the upload batch completes.
回答2:
In your Lambda function use the AWS SDK for CloudFront to invalidate the distribution using the sing wildcard '/*'.
var params = {
DistributionId: 'STRING_VALUE', /* required */
InvalidationBatch: { /* required */
CallerReference: 'STRING_VALUE', /* required */
Paths: { /* required */
Quantity: 0, /* required */
Items: [
'STRING_VALUE',
/* more items */
]
}
}
};
cloudfront.createInvalidation(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
Note: Make sure the IAM role attached to the Lambda function has necessary privileges to perform the invalidate action.
来源:https://stackoverflow.com/questions/48690059/aws-lambda-to-invalidate-cloudfront-on-s3-upload