Tuesday, July 17, 2007

Welcome to Amazon's EC2

I thought it was about time I checked out AWS since Werner Vogels recently declared that five years from now we'll all be on AWS. So today I got myself an account on Amazon's EC2. I set up a webserver and installed a php based web application driven by a MySQL database. I'll share the resources and tools I used to get started. Or you can skip to the summary.

First of all, if you don't know what AWS is, check out the Amazon Web Services front page, and specifically the page about EC2. Once you get the preliminaries (and hype) out of the way, your first stop should be this excellent screencast about setting up an EC2 instance from Windows that Mike Culver of the AWS team put together. This should help to bring the lofty concepts down to earth. Ultimately we're just talking about a machine out somewhere in the Amazon cloud. But that didn't stop me from getting a big rush as I followed Mike's tutorial and first logged into my EC2 instance. I hope you get the same experience.

Once I got over the initial excitement about turning Amazon's infrastructure to my own nefarious purposes, I started checking out the installation. I chose to install a preset image from Amazon which got me a Fedora installation with Apache, MySQL, and PHP, the canonical LAMP installation, pre-installed. They've also got Python 2.4, a nice alternative for PHP in lamP.

In about 20 minutes I was up and running with a complete web server and a database backend. This includes the 18 minute screencast from Mike. Of course, all of this is just what comes with the image I picked. But there are plenty of other choices. Try ec2-describe-instances -x all, or check out all the AMIs available at this public catalog of EC2 AMIs. Once you've found an image you want to install, try ec2-create-keypair gsg-keypair and save the result to a file "your_keypair" (or something more appropriate). Now try ec2-run-instances your_instance_id -k your_keypair. Amazon says it can take 10 minutes, but I've gotten up in under five minutes every time.

After checking some of the versions on the AMI I chose, I discovered I'd need to upgrade MySQL at least. While not an EC2 issue (it's just what was on the AMI I chose), I'm no Linux wizard, so some of these upgrades posed a bit of a problem. Fortunately there's the yum, pear, and pecl package management tools. yum upgrade mysql, yum install libtools, pear upgrade pear, pecl install xdebug-beta should set you right up. I had to disable the GPG check to install libtools. See /etc/yum.conf or /etc/yum.d/ and set the gpgcheck option to "0", but be sure to set it back to "1" when you're done installing libtools.

The next problem (as if it's just a minor speed bump) is that EC2 provides volatile storage only. So when you shutdown your instance you'll lose your disk, including configuration and software upgrades. That's in addition to this being a big issue for persistent storage in my database (out of scope for this post!). Fortunately, Amazon provides some nice tools for managing and creating AMIs, which is great for setting up a pre-configured image. Check out these docs creating AMIs for details. One thing they leave out is what to do after you've uploaded your image bundle to S3. Run this command to get an AMI ID: ec2-register my-bucket/image.manifest.xml. The next time you want to bring up your instance try ec2-describe-images -o self and ec2-run-instances your_new_ami_id -k your_keypair.

Whew, that's a lot of info. Let's recap with some useful resources:
Introduction Screencast to EC2
Start here. It'll get you all they way to logging in as root.
AWS developer forums
AWS has an active dev community. Take advantage of it. I haven't had to post a question yet since every question is already answered here.
Some commands you'll need:
  • ec2-describe-images -x all: This'll show you all the public images. Try the -o self option instead of the -x all option to get your private AMIs.
  • ec2-create-keypair gsg-keypair: you'll need this to create an RSA key. Save the result of the command to a file called "your_keypair".
  • ec2-run-instances ami_id -k your_keypair: This'll get your instance up and running, using the AMI with id=ami_id.
  • ec2-describe-instances: Use this to watch your active instances. Remember you're paying $0.10 per instance hour, so make sure you shut these down when you don't need them!
  • ec2-terminate-instance instance_id: When you're done, shut your instances down. Make sure you've gotten everything you want saved uploaded to S3 by the time you do this.
Setting Up Your Own AMI
These docs will walk you through creating a new AMI, either from scratch or (my recommendation) from an existing AMI. They leave out an important step though: once you've uploaded a bundle to S3 run ec2-register my-bucket/image.manifest.xml to register your new AMI for your own execution. It'll start out as private, but you can make it public if you want to share. Instructions are at the same above docs.
Some good blogs and posts about EC2:

That's about all I've got to say for now, except that all of this cost me $0.52. Not bad for a few hours of enjoyment :) I hope I helped to fill in some of the gaps in the documentation and tutorials. I'll be sure to keep you posted on my further experiences with EC2.

No comments: