Quickly add Amazon Cloudfront as a CDN
Quickly add Amazon Cloudfront as a CDN
Join the DZone community and get the full member experience.Join For Free
Insight into the right steps to take for migrating workloads to public cloud and successfully reducing cost as a result. Read the Guide.
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 https://console.aws.amazon.com (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
http://uttergobbledegook.cloudfront.net and you use that in place of the source domain you set earlier. So if your images were at
http://mygreatsite.com/product_images/whisk.jpg now you should be able to request
http://uttergobbledegook.cloudfront.net/product_images/whisk.jpg 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!
There are actually a few things you can do from here:
- Use a
cnamealias 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 , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.