Cloning an AWS CloudFront Distribution Easily

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:

  1. 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.
  2. 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
  3. Take this json file, sourceCFconfig.json, copy it to a new file, newCFconfig.json.
  4. 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).
  5. Edit the Aliases to reflect your new site info (or remove if you’re not doing HTTPS).
  6. If you’re using a different origin for this distribution, change that as well, including in the behaviors (make use of find and replace)!
  7. Near the end of the doc, change the comment to something new:
  8. Change the ViewerCertificate to a valid certificate ARN (or remove if you’re not using HTTPS).
  9. Save this file you’ve been editing.
  10. 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.

Cloning an AWS CloudFront Distribution Easily

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s