Cloud Zone is brought to you in partnership with:

Lorna Jane Mitchell is a PHP developer, blogger, trainer and evangelist from Leeds in the UK. She is active with and her local user group PHP North West, and writes for a variety of outlets, including her own blog at She is an active member of the PHP and open source communities and contributes to the event feedback project. When she's not at her computer, Lorna enjoys yarn craft, hobby electronics, and her home renovation project. Lorna is a DZone MVB and is not an employee of DZone and has posted 84 posts at DZone. You can read more from them at their website. View Full User Profile

Quickly add Amazon Cloudfront as a CDN

  • submit to reddit

Right now I'm working on an application which is experiencing lots of interest - and therefore lots of load! We needed to look at ways of trying to bring down the pressure on the servers, and decided to use a CDN (Content Delivery Network) for our image files. It was surprisingly painless to implement once I got into it so here it is in case it's helpful.

The way it works is that you point your image URLs to Amazon, and Amazon knows how to map those onto the images which continue to live where they are on your webserver and to be accessible by their own URLs. If Amazon gets a request for an image it doesn't have, it grabs it from your server and serves it - but then caches them all so the next request (or 10,000 requests!) will hit Amazon's servers and not yours. It also helps a bit because the domain is different and there is a limit on how many parallel requests browsers will make to any given domain, so you get more parallel requests on your page too.

Configurable Image Paths

I was able to switch out my image paths very easily because all images in the system had a path like $config['productImagePath'] - mostly this can help on dev platforms if they're not all the same, but also it helps if you need to move your images at a later date, as I had known could happen (and it did!). If this isn't already in place, you need to go find every image and make its URL configurable.

Set up Amazon Cloud Front

Head over to your Amazon Web Services console (it'll prompt you to sign up or log in as appropriate) and look for "Amazon CloudFront" in the "Storage" section.

Click the "create distribution" button and configure a few things:

  • Delivery method: web
  • Origin domain name: [domain where the images are served from right now]
  • Price class: choose which regions you need your images served from, it affects the cost

Then you need to wait for a few minutes while the cogs turn and it all gets created.

Testing Your Setup

You will get a URL for your CloudFront that looks like and you use that in place of the source domain you set earlier. So if your images were at now you should be able to request in your browser. Once it's cached, the response time should be very quick too.

Now go back and where your configuration settings once pointed to a URL on your own site, they will now point to the path under the CloudFront domain ... you probably need different distributions for your various staging/live platforms, and these can be enabled and disabled differently.

I found this was a very painless way of getting a CDN into place - no changes to the development platforms, minor config changes to the staging/live platforms, and the images stay in the same place that they always lived!

Whatever Next?

There are actually a few things you can do from here:

  • Use a cname alias to make a nicer URL such as
  • Tweak the caching rules to make sure they make sense for your application, they are very VERY configurable
  • If the images you're serving are assets rather than upload files, consider deploying them straight to S3 and setting up your CloudFront to server from there

Did this work for you? What did I miss? And did Amazon update their interface since I wrote this post (this happens often). Please leave a comment :)

Published at DZone with permission of Lorna Mitchell, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)