Automatically invalidate CloudFront cache for site hosted on S3

Have you hosting static content on s3 and using CloudFront as CDN or https endpoint? This lambda can help you.

Create a python lambda, and use this code

Add this policy to lambda role:

Now, go to you bucket and configure a event like this, don’t forget to select you lambda function.

Lambda Event

After create the event add a distribution_id tag in you bucket with the id of you cloud front distribution.

Now every time that you site get updated, the lambda will invalidate all file changed in CloudFront cache.