Friday, February 18, 2011

Hosting Static Websites on S3: The 99.4% solution

Today, Amazon announced a new feature which allows you to (almost) host a static website entirely on AWS S3. They do this by allowing you to set a default root object for each S3 bucket. Think of the default root object as the equivalent of an index.html default file. This setting tells a web server which file to return to the user when one isn't specifically asked for. For example, when you enter cnn.com in your web browser, CNN's web server automatically returns www.cnn.com/index.html.

Gotcha
In order for this feature to work, you'd have to configure your domain's DNS CNAME (alias) to point to your bucket. But there's a big gotcha and that's the fact that a CNAME record can only be used with subdomains (i.e. www.example.com, blog.example.com, images.example.com) and not the root domain (i.e. example.com). The DNS RFC directs that the root domain must be an A record which can only point to a numeric IP address.

Amazon Permanent Fix
To fix this problem, Amazon would need to set up servers at a single IP address. Load balancing a single IP address is exactly what keeps the entire DNS root servers alive and running. While there may only be 13 different static IP addresses for the world's DNS root servers, each one is supported by many redundant servers located on different continents. In other words, one static IP address can easily map to an unlimited number of physical servers.

Workaround
Since an Amazon fix to this problem may not be in the near future - or it might never come - there is a workaround, but it's not as elegant as it could be.

I've actually been using the following technique for a few years at Adjix. When visiting adjix.com, note that the URL for most of the static web pages at the bottom of the Adjix home page begin with web.adjix.com. As long as things are working, most people never even notice if a URL begins with www, web, blog, or has no subdomain.

1. Create an S3 bucket called www.example.com. Put an index.html file in there along with all of your website's static content and set the index file as the bucket's root object.

2. Configure your DNS CNAME to point to this bucket (i.e. www should point to www.example.com.s3.amazonaws.com.).

3. This final step is unpleasant and inelegant from a technical point. It involves using a third party service where you can configure our domain's root domain to point to a static IP address. I would be more than happy to host this redirect service for your domain's root A record. If you're interested in using this service then please let me know via e-mail.

Nearly anyone who visits your website, by typing the URL directly into their browser, will either enter www.example.com or example.com. Either method will work since they will be redirected to www.example.com/index.html.

While redirects aren't always elegant, you've probably noticed many redirects when logging into your Google account or when accessing a custom domain blog on Posterous. It isn't as clean and quick as it could be, but it doesn't violate any RFCs and it works.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.