Friday, January 07, 2011

Creating a Custom Origin Server for Amazon CloudFront

At the time of writing, tool support is limited to the REST API? The intention with this piece of work was to take content being served by our origin server; e.g.; and serve it via Amazon CloudFront on

  1. Download
  2. Get any dependencies from CPAN (the script tells you how to do that in case you aren't sure).
  3. Create $HOME/.aws-secrets and chmod 600.
    $ cat /Users/jabley/.aws-secrets
    %awsSecretAccessKeys = (
    # my personal account
    'james-personal' => {
    id => 'foo',
    key => 'bar',

    # my corporate account
    'james-work' => {
    id => 'AWS-ID',
    key => 'AWS-Secret-Key',
  4. Create a file with the request data
    $ cat create-distribution.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <DistributionConfig xmlns="">
    <Comment> CloudFront CDN</Comment>
  5. POST the file
       perl --keyname james-work -- -X POST -H "Content-Type: text/xml;charset=utf-8" --upload-file \
  6. Poll to see when it has finished creating the distribution:
       perl --keyname james-work --
  7. Configure DNS so that is a CNAME for the DomainName value of your newly created CloudFront Distribution.

You should now be able to request a resource using the new CDN name:

$ curl -v -s "" -o /dev/null
* About to connect() to port 80 (#0)
* Trying connected
* Connected to ( port 80 (#0)
> GET /images/foo.png HTTP/1.1
> Host:
> Accept: */*
> User-Agent: curl
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Date: Thu, 06 Jan 2011 18:51:35 GMT
< Server: Apache/2.2.3 (Red Hat)
< Content-Length: 1233
< Cache-Control: max-age=86400
< Content-Type: image/png
< Age: 13311
< X-Cache: Hit from cloudfront
< X-Amz-Cf-Id: b769b423c54e2ffb0a6fb60369e2e0f7b103251ef3e2c549084fb4abe4ef9a236052f8eec40b3a14,80416de274eb8ee87c21ee41c863f9f6f9ef1c251823d9c12c46ab13dc33759dcbb04175b7d4a5a7
< Via: 1.0 (CloudFront), 1.0 (CloudFront)
< Connection: close
{ [data not shown]
* Closing connection #0

Note that Amazon CloudFront doesn't support query strings on resources, so you might need to use some Apache mod_rewrite stuff.


Jeff said...

Howdy James.

Thanks a million for this post - I was struggling with how to format and post the XML request to the CloudFront API, and the perl library did the trick.

In case you're interested, I wrote up a post detailing how I used this tip plus a few other tricks to add a CloudFront CDN layer on top of a Rails stack:

Thanks again!

abautu said...

Hi James,

Thanks for the tip. It worked and save me a few hours to code a similar tool myself.