aws Lambda to invalidate CloudFront on S3 upload

[亡魂溺海] 提交于 2019-12-11 06:03:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!