Delete AWS CloudFront Cache Automatically when Building and Deploying with CodeBuild

I use AWS CodeBuild to build and then deploy this Hugo static site to s3. The site is also sat behind Amazon’s CloudFront CDN and after a sucessful deployment I wanted to delete the cache automatically so I could see the changes from the build immedietly.

This turned out to be relatively simple to do by adding the following line to the buildspec.yml file:

- aws cloudfront create-invalidation --distribution-id={Your CloudFront Distribution Id} --paths '/*'

The important item not noted in the documentation is that you can provide /* to delete everything in the cache but it needs to be quoted to keep it as a string literal '/*'.

comments powered by Disqus