Invalidating Cloudfront Cache after Production Release

I am reading about AWS Cloudfront and how does that invalidate the Cache. Read a few articles that all seems to indicate that event triggered from S3 can be subscribed by a Lambda that perform the cache invalidation. For example here and here.

This works for a website hosted in S3.. However I have a web app that is hosted in Docker (Tomcat container) containers on AWS. This Web app gets a new release every month, or sometime more than a month depending upon new requirements.

I believe Cloudfront will also cache the static files for this Website, for example, Images, CSS and JS files. My question is, how do we invalidate the Cloudfront cache for this website after releases? One way I could think of is writing a Script that is part of the Install job to invalidate the cache during release process. Let me know your views and if there is a better way to achieve this?


Related discussions:

(I’m certified but not much experience - currently learning the DevOps side of things)

Quick question - where are you currently hosting your static content? - in the docker container? If it’s in the docker container I believe you would want to set up EventBridge to trigger the Lambda Function every time you deploy your container.

I believe you should be storing your static content in S3 - which allows you to use S3 notifications to trigger the lambda function (via Event Bridge). For example, you can set up the Lambda function to invoke each time a file is created in S3.

Lastly, if you are using CodePipeline (you should be able to automate your docker build from either Github or CodeCommit) you can add a Stage to Invoke your Lambda function.

​

I hope this is helpful.

In the past I’ve invalidated the cache as part of the deployment process. You can simply trigger the invalidation via the AWS CLI or whatever other method you’d like using a new stage in your deployment pipeline and call it a day. If you don’t do this, users will likely see an older deployment for a while, so it’s important to do.