šÆ I want to keep this post short and to the point. So, we will not go into details of CloudFront and creating a CloudFront distribution but we will go directly into how to invalidate the cache in CloudFront using the management console. But before we see the HOW, letās understand the WHY.
Why do we need to Invalidate CloudFront Cache?
š Now use this: āInvalidating caching from CloudFront will remove the cached content from the edge locations.ā
ā You might be wondering why we want to remove the cached content when we want it to be cached to reduce the latency.
š¤ Yes, itās true that we want to serve content to our end users as fast as possible, but we also donāt want to serve outdated content to them, right?
š So, whenever there is a change, we want our users to see the latest updates as quickly as possible. For this particular reason, we would invalidate the content from CloudFront.
š« After invalidation, the viewer request for which the content isnāt currently in an edge cache ā is called a āCache-Miss.ā In this case, CloudFront must get the object from your origin.
šÆ On the other hand, a viewer request for which the object is served from a CloudFront edge cache ā is called a āCache-Hit.ā
š Now, letās learn the HOW part.
Steps to Invalidate CloudFront Cache Manually
Step 1: Access the CloudFront Console
- š Log in to your AWS Management Console.
- š Navigate to the CloudFront service by searching for āCloudFrontā in the AWS services search bar.
Figure ā Search CloudFront in AWS Management Console:
Step 2: Select Distribution
- š In the CloudFront dashboard, youāll find a list of your distributions.
Figure ā Showing CloudFront Distribution in AWS Console:
- š Select the distribution for which you want to manually invalidate the cache by clicking on its ID.
Figure ā CloudFront Distribution:
Step 3: Invalidations Tab
- š Within the selected distributionās details page, navigate to the āInvalidationsā tab.
Figure ā Showing Past Invalidations:
Step 4: Create Invalidation
- š Click the āCreate Invalidationā button to initiate the cache invalidation process.
Figure ā Showing Create Invalidation Screen:
Step 5: Specify Paths
- š In the āPaths to Invalidateā field, youāll need to specify the files or paths that you want to invalidate.
- For example, letās say you want to invalidate all the images in a specific folder:
/images/*
. - You can also use wildcard characters (_) to invalidate multiple files or paths, like:
/_.html
to invalidate all HTML files.
Step 6: Initiate Invalidation
- š After specifying the paths, click the āInvalidateā button to kick off the invalidation process.
Figure ā Showing object path /index.html to remove from CloudFront Cache:
Step 7: Monitor Invalidation Status
- š Youāll be directed to the list of invalidations, where you can monitor the status of your invalidation request.
Figure ā Showing Invalidation in Progress for /index.html:
- ā³ Keep in mind that it might take some time for the cache to be fully invalidated across all CloudFront edge locations.
Figure ā Showing Invalidation is completed:
Step 8: Verification
- š Once the invalidation status shows as āCompleted,ā you can verify the cache invalidation by accessing the URLs you specified.
- š CloudFront will now fetch fresh content from your origin server for the invalidated paths.
Example:
š Letās consider a scenario: You have a CloudFront distribution serving static assets for your website, and youāve recently made changes to your CSS files. To ensure that your users see the most up-to-date styles, you decide to manually invalidate the cache for all CSS files. Hereās how you can do it:
- Access the CloudFront Console.
- Select your distribution.
- Go to the āInvalidationsā tab.
- Click āCreate Invalidation.ā
- Specify the path as
/*.css
. - Initiate the invalidation.
- Monitor the invalidation status until itās completed.
- Visit your website and observe the updated styles fetched from the origin server.
ā ļø Keep in mind that cache invalidation may take some time. Itās essential to exercise caution with invalidations, as excessive use can impact your distributionās performance and increase the load on your origin server.
All Done! š We are all set.
Feel free to share your thoughts or ask questions and connect with me on LinkedIn š©āš»