How to create AWS CloudFront invalidations using AWS CLI

Last Updated:

If you’re using CloudFront as your CDN (Content Delivery Network) and you update something on your site, you need to invalidate your distribution cache so that CloudFront fetches the latest updates from your origin. If you do not invalidate, your users may not get your site updates because CloudFront will not download your latest changes to their edge locations instantly. It downloads your updates to AWS edge locations only after the cache TTL (Time to Live) expires.

Login to your AWS account with AWS CLI

If you do not have AWS CLI installed on your machine, please Install AWS CLI first. see Configure AWS CLI.

After having AWS CLI installed, we need our IAM user/role credentials to log in to our AWS account. Please see Configure the AWS CLI for authentication methods. For logging in using IAM Access Key and Access Secret Key, simply run aws configure and provide the necessary parameters.

How to Invalidate specific paths or files

If the changes are done only to a few files, say a few JavaScript files, you may want to invalidate only those specific files. This will save you a few bucks as invalidations cost money. AWS allows free invalidations for only the first 1000 files per month.

For invalidating specific files or paths, run the following:

aws cloudfront create-invalidation --distribution-id $DISTRIBUTION_ID --paths /paths/to/your/files

Make sure to replace the $DISTRIBUTION_ID with your own CloudFront distribution id.

Output:

{
    "Location": "https://cloudfront.amazonaws.com/2020-05-31/distribution/$DISTRIBUTION_ID/invalidation/IL0ADNUV1DGWN",
    "Invalidation": {
        "Id": "IL0ADNUV1DGON",
        "Status": "InProgress",
        "CreateTime": "2021-07-19T15:23:27.528000+00:00",
        "InvalidationBatch": {
            "Paths": {
                "Quantity": 2,
                "Items": [
                    "/assets/js/updatedjsfile.js",
                    "/assets/css/updatedcssfile.css"
                ]
            },
            "CallerReference": "cli-1626708206-303589"
        }
    }
}

Now the invalidation is in progress, it will take some time to update depending on the number of files. You can check the status of an invalidation using get-validation command:

aws cloudfront get-invalidation --distribution-id $DISTRIBUTION_ID --id IL0ADNUV1DGON

Output:

{
    "InvalidationList": {
        "Items": [
            {
                "Id": "IL0ADNUV1DGON",
                "CreateTime": "2021-07-19T15:23:27.528000+00:00",
                "Status": "Completed"
            },
            {
                "Id": "I3KF57KAPVZZT5",
                "CreateTime": "2021-07-19T14:48:29.070000+00:00",
                "Status": "Completed"
            }
 
        ]
    }
}

How to Invalidate the entire website cache

If you need to do this, all you need to do is to pass ‘/*’ to the create-invalidation command.

aws cloudfront create-invalidation --distribution-id $DISTRIBUTION_ID --paths '/*'

The output should be similar to the ones we have already seen.

How to use file versioning to avoid invalidations

As mentioned in the introduction, every time you updated your files, you need to wait for your CloudFront cache to expire before it downloads your updates, unless you use invalidations. However, there is a technique to avoid invalidations.

The way it works is that you include a version number like a serial or date to your files or directories’ names once they are updated. In other words, you rename your files after they are updated and tweak your app configuration to point to the updated file names. This will force CloudFront to download the renamed new files from your origin upon user requests.

For example, instead of using main.css, you rename it to main_1.css and increase the counter every time you make new changes to it. You can use the same technique for directories and all files within it will be downloaded on the AWS edge locations.

This way you only pay for the transfer of your new files to AWS edge locations.

Conclusion

Understanding invalidations is a very useful tip when using AWS CloudFront. In this tutorial, we covered how to invalidate your files and the option to avoid invalidation using versioning which is less expensive and efficient.

RECENT POSTS

Get Ops Pro Tips in Your Inbox!