Howto: Setup CloudFront as a Content Delivery Network

It’s actually incredibly easy to begin using a Content Delivery Network (CDN) such as Amazon’s new CloudFront service, and in this post I’m going to show you how.

Background

So what is a CDN and why use one? Well CDNs are essentially a global network of file servers that work together to serve static content such as images, flash, css and javascript files. They are useful if you want to serve up content faster to your users as the servers are strategically placed at edge locations all around the world and incoming requests are automatically routed to the server closest to the user. This reduces the latency of HTTP requests and makes pages feel “snappier”. They can also be useful to reduce load to your core servers.

Update!

Since writing this post I found there is a firefox extension which gives you a GUI interface into CloudFront. I haven’t tried it yet, but you can read about it here.

How to use Amazon’s S3 and CloudFront CDN

Simple Storage Service

If you haven’t already, signup for a CloudFront account with Amazon Web Services. You’ll also need an S3 account subscription, as the two work hand in hand, but Amazon should set this up automatically.

Login to Amazon Web Services and download your access keys. There are two you need, the access key id and the secret access key. You can find these in Your Account > Access Identifiers.. You’ll then need to configure your chosen client to use these keys.

Open up our S3 client and create a new bucket. You should avoid using underscores in your bucket names (although they will technically work, you won’t be able to create a distribution later via CloudFront). You might want to read the full restrictions on bucket names first. I recommend you follow the additional instructions to conform with the DNS requirements.

Using the S3 client, upload some files. You should then be able to access them at either of the following urls (substituting your bucket name and filename as appropriate):

Now that your files are accessible on S3 the final step is to link your S3 bucket to a CloudFront “Domain Name”. This process is known as creating a distribution, and is actually pretty simple.

First download the CloudFront Curl Perl Script from here. Then set up a .aws-secrets file in your home directory that contains your account keys. Make sure it’s has 600 permissions. The contents of the file will look something like:

This command will return some XML, which, if successful, will contain the domain name you can use to access your files via the CloudFront CDN. NB: It can take a few minutes for this domain to become active in the DNS and so you should wait a while before trying it.

You can then access your files at http://unique-id.cloudfront.net/filename.jpg [example]

And that’s it! You’re now ready to use this domain to host your static files for your sites. You could go a step further by pointing a subdomain of your site as a CNAME record to this domain.