Updating a CloudFront distribution is a common task for AWS administrators and developers. At times it’s necessary to do this using the AWS Command Line Interface (CLI). This article will guide you through the process of updating a CloudFront distribution using the AWS CLI.
Introduction
In this article we'll walk through the process of updating a CloudFront distribution via the AWS Command Line Interface (CLI). This involves storing the distribution configuration, extracting the ETag and DistributionConfig JSON objects, updating the DistributionConfig, and applying the updated configuration to the CloudFront distribution.
Prerequisites
- AWS CLI installed and configured with appropriate permissions [1]
- jq, a command-line JSON processor, installed [2]
- Please note, other command-line processors can be used in place of jq. Modify commands as needed to use with your preferred processor.
- A CloudFront distribution to update with the associated distribution ID
Procedure
1. Store the current distribution configuration to a local JSON file.
aws cloudfront get-distribution-config --id AAAAAAAAAAAAAA > current-distribution-config.json
Replace AAAAAAAAAAAAAA with your actual distribution ID in this and all future steps.
2. Record the ETag of the distribution.
The ETag is a version identifier for the CloudFront distribution. We're using the ETag to ensure our distribution configuration is the current version when applying the update.
jq '.ETag' < current-distribution-config.json
This command will return the ETag, for example: "BBBBBBBBBBBBBB"
3. Extract the current distribution configuration and save it to a JSON file.
jq '.DistributionConfig' < current-distribution-config.json > new-distribution-config.json
For an example of the full output, please review the CloudFront CLI documentation [3].
Keep the current-distribution-config.json
file as a reference or recovery option.
4. Modify the distribution config.
Open new-distribution-config.json
in your preferred text editor and make the desired changes. Modifications can also be made programmatically with tools such as sed
or jq
, but this is outside the scope for this article.
5. Apply the updated configuration to your CloudFront distribution.
aws cloudfront update-distribution --id AAAAAAAAAAAAAA --distribution-config “file://new-distribution-config.json” --if-match BBBBBBBBBBBBBB
Replace BBBBBBBBBBBBBB
with the ETag obtained in step 2. The --if-match
argument means our configuration will only be updated if the current ETag for the distribution matches our recorded ETag. This ensures the configuration we've updated is still current.
Conclusion
In this article we demonstrated how to use the AWS CLI to update a CloudFront distribution. Updating CloudFront distributions using the AWS CLI provides an efficient method for managing your distributions that can be integrated to automation tooling. Remember to review changes and test them in a non-production environment before deploying.
Resources
[1] https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html
[2] https://github.com/jqlang/jq
[3] https://docs.aws.amazon.com/cli/v1/userguide/cli_cloudfront_code_examples.html