Comments:"How I served 100k users without breaking the server- or a dollar bill. | Adventures (in code)"
URL:http://blogging.alastair.is/how-i-served-100k-users-without-crashing-and-only-spent-0-32/
Or, “The incredible, affordable S3″
I made a silly thing. The Associated Press’s Twitter account had just been hacked and sent out a fake tweet about an explosion in the White House. The Dow Jones immediately dropped 150 points. So I made a silly thing. It’s called “Is My Twitter Password Secure“, and the best description I could probably give it is a PSA on phishing sites. Try it, you might laugh. If you’re like many people, you’ll be so convinced by it that you’ll send me abuse on Twitter. That’s OK.
But anyway, it “went viral”. Tweet after tweet arrived in my “mentions” box as people enjoyed the joke and shared it with their friends. Despite being hammered with requests, the server never slowed and never crashed. Because there was no server. At least, not that I had to worry about, because I hosted the entire thing on Amazon S3. Not only is it super-reliable, it’s also super cheap- serving 758,509 requests cost me… thirty cents.
Of course, you can’t run any dynamic scripting on S3, but you’d be surprised the number of times you don’t have to. For example, the promo site for my taxi app does use dynamic content- the map tile images are generated by an EC2 instance I have running TileStream- but the vast majority of the page runs quite happily on S3. JSONP or CORS mean that you can quite effectively run an ‘API’ server on an EC2 instance, while leaving the majority of your static HTML on an S3 bucket.
While the steps to set this up aren’t complicated, I thought it might be worth creating The Definitive Guide To Hosting A Web Site On S3.
The Definitive Guide To Hosting A Web Site On S3
The steps are actually really quite simple- make a bucket, set up a CNAME, upload your files. But let’s go one by one:
Make a bucket
Every domain name you want to use has to be a different bucket. Make the name of the bucket the exact domain name you wish to use (including the subdomain, like www.):
Then you need to make this bucket publicly browsable. Select your bucket, and open up the Properties tab. Under ‘static hosting’, you just need to check “Enable website hosting”:
You’ll notice that there is also a box for ‘index document’ – you ought to be fine to leave this as it is (no point trying to host PHP files on here, folks) but if you’re one of those people you might need to change it to “index.htm”.
Ta-da! You now have a static web site up and running.
Upload your files
Unfortunately, S3 doesn’t offer anything so simple as FTP access. That said, there are many clients out there set up for S3 uploads- my personal favourite is Cyberduck, it’s donationware, and supports just about every uploading scenario you could wish for. All it needs is your AWS API key and it’ll list all your buckets out for you, and let you drag and drop your files straight into your bucket.
Get a better domain name
That endpoint URL is pretty gross. Thankfully, it’s very simple to get a better one mapped to your S3 site. I’m using Namecheap in these screenshots, but any DNS provider ought to be able to do the same thing. Go to edit your DNS records, and add a CNAME record for your chosen subdomain that points to your gross endpoint URL:
That ‘IP ADDRESS/ URL’ field’s full value is www.ismytwitterpasswordsecure.com.s3-website-us-east-1.amazonaws.com. – that last full stop on the end is important. And the URL redirect above simply directs all users to the www subdomain if they haven’t already entered it.
And that’s it. Your static site is up and running on your pretty domain name. Now you’re free to play horrible tricks on the world without worrying that they’ll crush your server and/or wallet in return. Use this power wisely.