I’m often creating a new site that’s similar to others and has CloudFront in front of them (yes, think WordPress). Many of these distributions have a bunch of behaviors, and I hate to waste time trying to recreate them (copying and pasting, making sure I didn’t miss a step). Anyway, finally taking a little bit of time, I’ve found it’s straightforward to do it using the AWS CLI:
Steps to export, modify, and import a new CF config file with AWS cli:
- Install the CLI if you haven’t already. It’s relatively quick and straightforward, provided you have your access and secret keys. I’ve been using Version 2 of the CLI with great success. I’m primarily on Windows, so the below steps are respective of that.
- Using the CLI, export the CloudFront config of a distribution you want to copy. In my below example, you’ll need to swap out the distribution ID with one of your real distribution IDs (something like E3xxxxxxxxxx). Also, the c:\aws folder is whatever you want.
aws2 cloudfront get-distribution-config --id YourDistID --output json > c:\aws\sourceCFconfig.json
- Take this json file, sourceCFconfig.json, copy it to a new file, newCFconfig.json.
- Open the new file, edit out the first parent element (be sure to remove the last curly bracket at the end of the file as well).
- Edit the Aliases to reflect your new site info (or remove if you’re not doing HTTPS).
- If you’re using a different origin for this distribution, change that as well, including in the behaviors (make use of find and replace)!
- Near the end of the doc, change the comment to something new:
- Change the ViewerCertificate to a valid certificate ARN (or remove if you’re not using HTTPS).
- Save this file you’ve been editing.
- Import this config and create a new distribution using the below command:
aws2 cloudfront create-distribution --distribution-config file://c:\aws\newCFconfig.json
If no errors are shown, you should see a new distribution in CloudFront.