Scaling Servers with the Cloud: Amazon CloudFront
This is the second of two articles focused on using Amazon’s cloud services to boost your server’s performance and capabilities. Read the first part.
Once you’ve got files hosted on S3, the next step is to speed up delivery time. Even though loading time through S3 is relatively fast, it still isn’t the most efficient way to load files for a global audience. Even if bandwidth isn’t a problem, high quality images and other media files can make pages take longer to load.
Wouldn’t it be nice to serve these kinds of files faster without spending hundreds on server upgrades?
This is where CloudFront comes in.
What’s the Point of CloudFront?
After the first post, some of you wonder what makes S3 so different from CloudFront. Why not just use CloudFront and take S3 out of the equation?
CloudFront is a distributed network of edge servers that piggybacks off of the files already hosted on S3. Think of S3 as a warehouse, and CloudFront as an express delivery service. Without a central place to keep the original files, CloudFront would have nothing to deliver.
What’s a CDN?
A Content Delivery Network is a way of serving static files faster based on geographic location. This means shorter load times from servers closer to the visitor. A visitor in Asia (for example) gets served by an edge location (closest server) located within their country. The result is a noticeably shorter load time, especially for large files.
Set Up a Distribution
To get started with your own CDN, make sure that you’ve successfully set up a S3 bucket for your site’s files. A CloudFront distribution link to a specific S3 bucket, and shares access to all files inside it. To keep this guide universal, we’ll use Amazon’s web-based Management Console to set up our first distribution.
In the CloudFront tab of the console, click “Create Distribution” from the top menu. You’ll be faced with this screen:
The options are mostly straightforward:
- Origin specifies the S3 bucket that the distribution links to.
- Logging toggles whether or not Amazon will save access logs to an S3 bucket of your choosing. You’re probably not interested.
- CNAMEs (Canonical Name) is where we’ll specific the subdomain that links to this distribution. It is not mandatory (you will get a default URL anyway), but it will make referencing CloudFront files much easier.
- Comments is for notes, but you already knew that.
- Distribution Status toggles whether the distribution is active or not.
How to Access a CloudFront File
Thankfully, you don’t need to be an expert in server administration to make these services work for you. Usage is a simple matter of using a different URL to the already-existing S3 resource. You do not need to upload files differently to use CloudFront. Here’s an example:
The second URL (which we will clean up in the next section) tells Amazon that the file reference needs to get copied and served from an edge location near to the visitor. This means that any future hits from similar visitors will have noticeably decreased load times. Even though S3 is not constantly active (files on edge servers expire hourly), it still needs to be present as a central place for all files.
Dedicate a Subdomain
CloudFront’s new URL isn’t exactly the easiest thing to remember. To keep things neat and memorable, we’re going to assign the CloudFront distribution to a subdomain (e.g. static.buildinternet.com). Any file on S3 that is referenced through this domain is automatically loaded through CloudFront.
With CNAME: http://static.buildinternet.com/images/scaling-servers-amazon/static-file-collage.jpg
Which would you rather memorize? We thought so.
Create a CNAME
You can specify a domain for CloudFront from your server’s administrative panel via the CNAME record. If you’re not well versed on the intricate details of the DNS, I don’t blame you. This article will help summarize the basics and bring you up to speed.
In Media Temple the CNAME is changed through the account admin panel (Domains > yourdomain.com > Edit Zone File). Your host’s setup may be different, but you are ultimately looking for a page to edit the domain’s DNS Zone file.
Make sure to set the type of record to CNAME, and to include a trailing period in the target CloudFront domain. Keep in mind that it will take some time to for the new domain to propagate, so expect a day of waiting before being able to use the new domain.
The Cloud in Action
Keep in mind that just because you can put everything from S3 into CloudFront, it doesn’t mean you should. Even though pricing is cheap, large traffic sites and files can add up. I recommend using S3 for everyday static files and reserving CloudFront for high traffic content or core site files. Expand as needed! There’s no rush.
Integrating AWS might seem complicated at first, but once you’ve got it up and running it’s surprisingly simple. Issues setting up? Did we miss something? Let us know any problems (or success stories) in the comments below.