Please note that this post, first published over a year ago, may now be out of date.
Tired of waiting for your CloudFront changes to propagate? Check out this little hack to speed up your configuration changes
If you’ve deployed a CloudFront distribution before, then you will know first hand how slow this can be.
It gets even more frustrating when you’re tweaking CloudFront behaviour because it’s just not quite working as you would like; best go grab yourself another coffee…
Fortunately, there’s a neat trick you can do to speed up your configuration changes to help you in exactly these scenarios!
CloudFront Invalidations are used to clear cached files at CloudFront Edge locations. What’s quite handy is that wildcard validations to the root also seems to flush CloudFront configuration. The console will still tell you that the status of the distribution is “in progress” and the invalidation itself will take some time, but go ahead and give that URL a refresh. For me, the changes happen pretty much instantly.
How do I invalidate the cache for my CloudFront distribution?
There are a few ways to do this, but I’d suggest either doing it through the console or via the aws-cli for simplicity.
Through the Console
- Sign in the CloudFront console and click on the Distribution ID link for the one that’s updating.
- Click the Invalidations tab
- Click the blue Create Invalidation button
- Enter * as the Object path and hit Invalidate
Through the aws-cli
Just run the following:
$ aws cloudfront create-invalidation --distribution-id ${distributionId} --paths "/*"
You can find more information about the create-invalidation
command here.
It’s worth noting that invalidation requests aren’t free. AWS give you 1,000 paths a month at no additional change, so unless you’re already invalidating your distribution, or making a bucket load of configuration changes, you should be okay. Fortunately, invalidating the entire distribution with /*
counts as a single request. Each additional invalidation path costs $0.005.
Also, if you invalidate all the cache, you will see more traffic hitting your origins. This hack is probably more useful to you when you’re testing CloudFront behaviour before you go live.
So it’s as easy as that! Next time you’re changing CloudFront configuration and you don’t want to hang around watching paint dry, invalidate all the objects in your distribution and save a bucket load of time!
We’ve been an AWS SaaS Services Competency Partner since 2020. If you’re building SaaS on AWS, why not book a free health check to find out what the SaaS SI Partner of the Year can do for you?
This blog is written exclusively by The Scale Factory team. We do not accept external contributions.