Automatically invalidate CloudFront cache for site hosted on S3

Yago Nobre
Sep 10, 2017 · 1 min read

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.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store