Scaling Servers with the Cloud: Amazon CloudFront

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:

S3: http://buildinternet.s3.amazonaws.com/images/scaling-servers-amazon/static-file-collage.jpg

CloudFront: http://dhijdbsqgh3cb.cloudfront.net/images/scaling-servers-amazon/static-file-collage.jpg

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.

Default: http://dhijdbsqgh3cb.cloudfront.net/images/scaling-servers-amazon/static-file-collage.jpg

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.

Posted Thursday, November 12th, 2009 · Back to Top

SPONSOR

Add Comment

23 Comments 2 Mentions

  1. Robert S. Author Editor

    Awesome information! Love it and this website. Request if I may, can someone write an article/tutorial on how make a file source control system with Amazon S3 and the different options out there.

    ·

  2. TJ Author Editor

    the tutorials seems good enough.. is there a cost attached to using cloud front? sorry i didn’t bother checking…

    regards
    TJ

    ·

  3. Andy Author Editor

    you will have to pay for CloudFront on top of S3. Data transfer costs start at $0.170 per GB and differ depending on the edge location.

    Check out Amazon CloudFront official website for costs
    http://aws.amazon.com/cloudfront/#pricing

    ·

  4. Andy Author Editor

    @Robert – Amazon S3 team is working on versioning support that will make it possible to implement file source control systems in the cloud an easy task.

    Check out Amazon official forum http://developer.amazonwebservices.com/connect/thread.jspa?threadID=38071&tstart=60

    ·

  5. Greg Author Editor

    re: pricing, ugh! The way they make it look in their literature, the CloudFront cost is not a separate cost but a cumulative one.

    Admittedly, I didn’t go crazy with reading up on it all.

    For my needs, if there’s additional cost for CloudFront, using just S3 would be enough, as per Zach’s recommendation.
    .-= Greg´s last blog ..Coles First Halloween =-.

    ·

  6. Ben Author Editor

    Nothing much to add… I just love this serie, and wish you will show us how to implement this programatically in a new PHP or Ruby on Rails application. Thanks a lot for those articles, you rock.

    ·

  7. zoni Author Editor

    Thanks a lot, you guys are doing amazing job. thumbs uP

    ·

  8. Shawn B. Author Editor

    Alright, well this answers my question from the other article. This seems like a really good system for any website that wants to function on a global scale.

    Thanks for the straight forward article!

    ·

  9. Shawn Bird Author Editor

    This is a little off topic but I just saw you guys on The UX Booth’s article about transparency, you were the first example, good work!

    ·

  10. ihlas temizlik robotu Author Editor

    Great article… Thanks pal

    ·

  11. Rogerio Job Author Editor

    Great post Zach! I don’t find this type of information anywhere.

    Thank you for sharing it!

    Rogerio Job

    ·

  12. Omer Greenwald Author Editor

    Excellent tutorial. especially useful for sites with mass of static image and js files.
    Do you also have experience in using free CDN networks like CoralCDN or Image hosting solutions like ImageShack?

    ·

  13. Ganhar Dinheiro Author Editor

    I liked your post because you explain clearly and objectively.

    Thanks!

    ·

  14. Geoffrey Bullock Author Editor

    That was really useful as I am beginning to research CDNs for adding video courses to a site. I know a lot of folks are using AS3 and Cloudfront, but it’s been a challenge to find a straight forward review on its ease of use. Thanks for taking the time to post this.

    ·

  15. Thiago Santos Author Editor

    Hi
    ‘m brasilian not take this wrong my bad English writing (laughs)
    I would like to thank this post, it really was very important for my pretensions to my site, I will become reader of your blog, thanks

    ·

  16. Como Ganhar Dinheiro Author Editor

    Very good Zach, you were very clear in his words

    ·

  17. Davi – Ganhar dinheiro Author Editor

    Yeah, great post! Thanks!

    ·

  18. Marcus Azzi – Dinheiro Na Internet Author Editor

    Hi Zach! Thanks for the great information on Cloudfront.

    I was exactly looking for a way to improve delivery time of some of my videos, which are sometimes slow if hosted in Amazon S3.

    Really thanks.

    Regards,

    Marcus

    ·

  19. Jorge Author Editor

    Very good information. This is amazing.
    Thanks.

    ·

  20. Dinheiro Author Editor

    Nice post!
    Congratulations on your work

    ·

  21. dinheirocomoganhar Author Editor

    Great post,Thanks for taking the time to post this.

    ·

  22. Como Ganhar Dinheiro Na Internet Author Editor

    Thks a lot for this post, i will sbscribe on your feed !

    ·

  23. James Traughber Author Editor

    I loved as much as you’ll receive carried out right here. The sketch is attractive, your authored subject matter stylish. nonetheless, you command get bought an shakiness over that you wish be delivering the following. unwell unquestionably come more formerly again since exactly the same nearly a lot often inside case you shield this hike.

    ·

 

Build Internet by One Mighty Roar. Since 2008.