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.
Download an S3 client / GUI such as:
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):
- http://bucket-name.s3.amazonaws.com/filename.jpg [example]
- http://s3.amazonaws.com/bucket-name/filename.jpg [example]
Cloud Front CDN
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:
%awsSecretAccessKeys = (
# primary account
primary => {
id => '<Your primary AWS Access Key ID>',
key => '<Your primary Secret Access Key>',
},
# secondary account
secondary => {
id => '<Your secondary AWS Access Key ID>',
key => '<Your secondary Secret Access Key>',
},
);
Next create a text file with the XML instructions needed to create a distribution, it should look something like this:
<?xml version="1.0" encoding="UTF-8"?>
<DistributionConfig xmlns="http://cloudfront.amazonaws.com/doc/2008-06-30/">
<Origin>mybucket.s3.amazonaws.com</Origin>
<CallerReference>20080930090000</CallerReference>
<Comment>Creating my first distribution</Comment>
<Enabled>true</Enabled>
</DistributionConfig>
Replace the origin with your bucket’s url. You’ll need to use the bucket-name.s3.amazonaws.com format. The caller reference is just a timestamp.
Save this file as create_request.xml and then run the following command to execute it:
./cfcurl.pl --keyname -- -X POST -i -H "Content-Type:text/xml; charset=UTF-8" --upload-file create_request.xml https://cloudfront.amazonaws.com/2008-06-30/distribution
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.