Deploying Rails on EC2

Posted by Bruno
on Thursday, April 05

Who is this EC2, and is he related to that robot from Star Wars?

Um … no. EC2 stands for “Elastic Compute Cloud”; it’s a “web service designed to make web-scale computing easier for developers.” Try telling your mom that.

In mom-speak: EC2 is a way to cheaply and quickly set up virtual computers. That means you could set up a virtual machine running Fedora Core with RMagick, LAME, and ffmpeg, and then add 15 more instances at the click of a mouse.

OK, my mom still won’t care, but if you’re building a YouTube MeToo, then you probably will. Why?

Reasons for using EC2 instead of dedicated or VPS hosting

  • It’s elastic: add capacity almost instantly (even programmatically, if you want)
  • Root access to a real box: “Each instance predictably provides the equivalent of a system with a 1.7Ghz x86 processor, 1.75GB of RAM, 160GB of local disk, and 250Mb/s of network bandwidth”
  • Pay as you go: $0.10 per instance-hour, so you hold on to more of your money for longer (Web 2.0 bootstrappers, take note)

Think: easy staging environments, clustered database servers, on-demand compute scalability, backups to S3 (EC2 to S3 transfer is free)...

There’s plenty of debate about whether this is the smartest, most reliable, or even most cost-effective option for most developers1 . But at the end of the day I moved my applications to EC2 because I like trying new deployment options, and because it’s not hard, and because I wanted to learn how to do it.

At about $72/month per instance (plus bandwidth), it’s cheaper than many VPS options (though not the cheapest), and I like getting charged only for what I use. I’m always playing around with new ideas/applications; with EC2, I can launch an instance, throw an application on it, and take it down two hours later.

I’ve been running Feedmarker and Snapballot on a single EC2 instance for about a month, and so far I’ve had no problems.

Managing your EC2 instances

So, you’ve decided to use EC2, and now you need a production environment. What next?

You need a way to start, stop, monitor and save machine instances. Amazon’s Getting Started Guide is, well, a good place to start. It’ll walk you through setting up the EC2 comand-line tools, launching an instance, terminating it, bundling it, and cleaning up.

But here’s a secret: if you want to skip right to the good stuff, check out RightScale (previously called AWS-Console). It’s a web-based interface to EC2, and it’s free (for now)! After you sign up at RightScale, you’ll be able to launch, monitor, terminate, bundle, and even backup Amazon machine instances, all from the safety of your web browser! Yay! And if you don’t have your own EC2 Beta account, RightScale will even give you ten instance-hours to play with for free.

Another free option is the EC2 UI Firefox extension , which is a browser-based GUI for managing EC2 instances. Or try Capazon, a sweet Capistrano library that does everything you need.

It’s that easy?

Actually yes. Here are the steps, simplified:

  • Using the method of your choice (RightScale, EC2 UI, or command line tools), launch an Amazon Machine instance (it’s easy). You now have a fully functioning machine, with root access. I recommend using the stock Fedora Core 4 base image provided by Amazon.
  • Set your box up up for Rails2.
  • Point your domain at your EC2 instance. If you don’t have a nameserver, you can use EveryDNS to manage this for free.

Things That Might Go Wrong

OK, so your ‘server’ is really just a virtual machine running somewhere in Amazon’s cloud, so if it crashes, dies, barfs, or becomes otherwise incapacitated, Amazon doesn’t guarantee that anything you put on it will be recovered when you restart it. What does that mean? You probably shouldn’t use EC2 to host content, like images, files, mp3s etc. Put that stuff somewhere else, like, say, S3. Same with your database; make sure you’re backing it up frequently to S3 or some other safe place3 .

Now, if you’ve customized your machine instance with all kinds of goodness (gems, Rmagick, mongrel, etc), you’ll want to bundle that sucker up in case it dies and you need to re-launch it, or in case you want to add another 16 of them. Again, here’s where RightScale comes in handy, letting you bundle a running instance and save it to your S3 account with a few clicks.

Also, your EC2 instance doesn’t have a guaranteed, permanent IP address. If it goes down, it might come back up with a new address… so you’ll want to figure out a way to make sure your domains still resolve if that happens4 .

Real World, Please

Is anyone crazy enough to be using this stuff in production? Well..

(Any others?)

Final Thoughts

Should you use EC2 to host your next world-changing web application? The answer is clear: I don’t know. But it’s easy enough and cheap enough to play with that you should at least spend some time checking it out. You could spend a full work-day testing on EC2 and it’d cost you $0.80.

I think small/solo Rails developers in particular should give it a hard look, because with EC2 you can offer your clients a data center in addition to your development skills. And that’s not a bad tool to have in your toolbox.

1 Is EC2 cheap or expensive? Depends on how you look at it: here’s a good cost comparison versus some other common hosting options, and here’s a lively discussion on the costs.

2 Setting up a Rails-ish stack on EC2 is the same as setting it up anywhere else. Here are some helpful links: Link , Link , Capistrano tasks setting up your server , Deprec makes this easy (if you’re on Ubuntu). You get the idea.

3 Here’s a nice tutorial on S3 backups from my friends over at Slingshot Hosting; these instructions would work just as well on your EC2 box (aside: if you’re looking for a more managed environment than EC2, I highly recommend Slingshot).

4 Dynamic DNS with EC2 and ZoneEdit and Domain Forwarding on Amazon’s EC2 with DHIS might help.

Comments

Leave a response

  1. ThorstenApril 06, 2007 @ 11:51 AM

    Bruno, thanks for mentioning RightScale (I’m a co-founder) and my course at UCSB !

    We’ve been having tremendous success in using Amazon’s web services, particularly EC2, and frankly, when you ask “Should you use EC2 to host your next world-changing web application?” the answer for me is crystal-clear: YES. The flexibility offered by EC2 combined with the savings are just tremendous. Two years from now nobody will want to do without this type of hosting.

    The question that is a little more difficult to answer is “should I move to EC2 today” and that depends on how close you are to the “world-changing” part. There are still a number of rough edges in EC2, the persistence (or rather lack thereof) being one of them. We (and others) are working hard at providing solutions that we can make available or sell that will make it a lot easier for others to make the step up onto EC2. So if the “world change” is still a ways off, you may want to try EC2 out but wait a few more weeks before making a decision.

    Just a quick note about the EC2 accounts: the beta is “closed”, but if you sign up you will get in within a few weeks. At least that’s what I’ve been observing. In the meantime, give it a spin on RightScale and drop us an email if you’re caught between the end of the free trial and getting your own EC2 account.

  2. AlexApril 06, 2007 @ 08:25 PM

    Hello,

    I signed up with RightScale (and burned up 45 minutes of machine time, sorry guys!), but couldn’t do anything with the machine unfortunately. The “open SSH client” link advertised in the introductory video is actually not available, and reading the FAQ it turns out it’s only available for “direct” users (who already have an ECC account).

    I tried using Putty with the private key provided on the website (boy, did that involve a lot of copying and pasting!), made sure it looked exactly the same as described on Amazon’s ‘getting started’ pages, but Putty can’t parse that format; I struggled with Puttygen to make it accept and convert the private key file, to no avail (“Couldn’t load private key”), so that has been fruitless overall. Reading the various message boards google turned up didn’t solve it – I did everything like adding a newline, etc.

    All in all, it was mildly disappointing, though I was able to ping the machine and bring up the stock webpage :)

    I’m a bit unsure how RightScale plans to attract users with the (honestly) rather uncomfortable experience of chasing for the missing “SSH client” link, then manually assembling the key file from various bits on the webpage, then struggling to connect. Me using Putty on Windows and not a “real” client on a real OS is not relevant since, like I mentioned, just putting together a key file was a copy-paste extravaganza. :)

    Best of luck to the RightScale people! (And sorry again for eating up almost an hour of machine time)

  3. 工控网April 11, 2007 @ 08:45 AM

    I tried using Putty with the private key provided on the website (boy, did that involve a lot of copying and pasting!), made sure it looked exactly the same as described on Amazon’s ‘getting started’ pages, but Putty can’t parse that format; I struggled with Puttygen to make it accept and convert the private key file, to no avail (“Couldn’t load private key”), so that has been fruitless overall. Reading the various message boards google turned up didn’t solve it – I did everything like adding a newline, etc.

  4. ThorstenApril 28, 2007 @ 08:49 PM

    Alex, I’m sorry you had difficulties. The SSH console is definitely available, however, you have to wait for your instance to actually start running (it shows “pending” while it’s being prepared). I recently switched to putty and didn’t have much of a problem getting the keys in. Sounds like I should write a blog entry to show people how to do it. Maybe we could show the key in putty format, mhh, that would be a nice touch…