Integrating Amazon CDN in 4 Simple Steps

The news that Amazon is now offering a low-cost CDN has likely excited many people. Aptly named CloudFront, this service is stirring up a lot of attention. In our overview of the new service, we outlined the basic steps to use CloudFront.

In this post, we thought we would give you a little more detail on those steps to help you get started sooner.

Using Amazon CloudFront is a good idea if you have a lot of content that is fairly static and doesn't need to be dynamically generated every time someone requests it. Images are a primary example of content that is static for the most part. You may even have some web pages that don't change often -- like Contact Us or About Us.

These things lend themselves well to being served by a Content Delivery Network like Amazon CloudFront. And setting up the service is fairly straight forward as you will soon see.

Getting Started

A few housekeeping items we need to cover before we dig into the real work.

  • You need to have an Amazon Web Services Account, an Amazon S3 Account and a CloudFront Account ? we?ll explain how to get these in step 1 below.
  • Basic Technical Knowledge Required: XML, Web Services
  • Development language experience in one of the following languages: Ruby, Java, PHP, Python or .NET

Common Terms

You should be familiar with the following terms used by Amazon Web Services:

  • Object: Objects are the files you want delivered using CloudFront. These can include images, css files, web pages, media files. Files delivered via CloudFront must be publicly readable.
  • Origin Server: The origin server is the home of the original version of your objects. CloudFront location servers will retrieve the objects needed from the origin server.
  • Distribution: A distribution links the Amazon S3 bucket that contains your objects with a domain name assigned by CloudFront.

4 Steps to Integrate Amazon CloudFront

We aren't going to focus on the exact coding required to integrate with CloudFront -- Amazon does a fine job of this themselves. What we want is to explain the steps you would take and point out things that you need to be aware of before taking the next step of actually getting started.

Here are the steps at a glance:

  1. Get and set up Amazon S3 and CloudFront accounts
  2. Create your Amazon S3 bucket
  3. Create your distribution and get domain name
  4. Decide what you want to use CloudFront for and create the appropriate links

Step 1: Setting Up your Amazon S3 and CloudFront Accounts

To access any web service AWS offers, you must first create an AWS account. This account is required to use any of the AWS products. Signing up provides you with a couple of access key identifiers (Access Key ID and Secret Access Key) which are required to sign up for S3 and CloudFront.

The Access Key ID is used to identify yourself as the true sender of requests and the Secret Access Key is a secure key that only you see. Because the Access Key is not a secure key, you create a digital signature using your secret key that helps confirm you are the true requester.

You will need to place these keys somewhere in code files to be used when coding to talk to the web services for either S3 or CloudFront.

Once you have the AWS account, you can then sign up for both Amazon S3 and CloudFront.

You need to sign up for Amazon S3 because this is where you will store your objects for CloudFront to access (your Origin Server).

Step 2: Creating your Amazon S3 Bucket

Now you are getting into the coding aspect of setting up this service. You can develop your applications in any number of programming languages and access the Amazon Web Services. Amazon provides a number of language specific toolkits to help you work with their web services -- so you aren't starting from scratch. Toolkits for PHP, Java, C# and Perl are available for helping you integrate S3 web service requests.

Creating a Bucket/Origin Server:

To use CloudFront, you must have your objects stored within an S3 bucket - your CloudFront Origin Server. So you need to create that bucket first and place your objects in it. Amazon has a really good tutorial on getting started with S3 so we aren't going into the details here.

The important things to remember, however, are that your bucket name must be unique and appropriate as it is displayed in the URLs that are used to access the objects within it. A bucket can hold any number of objects and each object can be up to 5GB in size.

The Amazon Simple Storage Service Developer Guide provides you all the information you need to know to add, replace and delete objects as well as a lot of other functionality.

It is important to note that objects added to the S3 bucket are stored in multiple locations/data centers to ensure their availability.

Step 3: Creating your CloudFront Distribution

In order for CloudFront to know where the origin server is located, you create a Distribution. As we said earlier, a distribution links your Amazon S3 buck or Origin Server with a CloudFront domain name.

Again, Amazon has a simple to follow tutorial with code examples on how to create a distribution. They also point to a number of language specific toolkits to work with CloudFront as well including Java, .NET and Ruby. So we won't get into specific coding details here -- except to point out that it is not complicated.

You get the domain name as a result of creating the distribution and it may look something like this:

When you create a distribution, it is propagated throughout the CloudFront system, so it could take a little while to happen. Once you have confirmed the propagation is complete, you can start to create your links.

Step 4: Preparing the Links

The easiest step of the entire process is to create the links using the domain name that was given to you in step 4. Here's an example of a link using the domain name above:

<img src="">

Can't get much more straightforward than that, can it?

The hardest part of preparing the links is deciding which objects you want served by CloudFront.

Other Considerations:

A few things to consider when creating your objects to be served by CloudFront:

  • Object Expiration: CloudFront caches a copy of your object. By default, your object is set to be cleared from the cache of the location server every 24 hours. You can make that period longer by setting the Cache-Control, Pragma, or Expires header of your object, but you can't shorten it.
  • Object Versioning: Take some time to consider how you want to version your objects. CloudFront caches a copy of your object on any number of location servers and doesn't expire that object for at least 24 hours. So if you find you need to change something and get it pushed out as soon as possible, you may have a problem. Hence the need for good versioning.
    Amazon recommends you use versioning to help with the expiry problem. So instead of cmswire.jpg, we name it cmswire1.jpg and each subsequent update accordingly. When we want to update the image, we just adjust the links calling it (in some cases not a small task) and the new image is pushed out as soon as the first request is made for it.

What Happens Next?

If you want to create a distribution to run some tests, it's probably a good idea. Note that although you can use the same distribution for your live site, you can't switch the Amazon S3 bucket accessed by the distribution. So you might be safer to create separate distributions with their own S3 bucket (you can have 100 buckets and 100 distributions).

From the surface, it appears that working with CloudFront is fairly straightforward. You only want to use it for assets within your website or application that don't change often, so you need to examine your assets carefully and make proper decisions.

You also need to consider versioning your objects, although not every object needs versioning. Because making updates to your code can become time consuming, you only want to version those objects you expect you may need to be updated immediately.

Hopefully, you have found this article useful in helping understand how to integrate the Amazon CloudFront CDN into your website/application. You can find a lot of material on the Amazon CloudFront website to help you learn even more.