» Amazon EC2 + Chef = Mmmmm

UPDATE

This is a little out of date now, but it still contains some helpful information on getting EC2 setup

Amazon's Elastic Computing Cloud (EC2) has always intrigued me. But now that you can get an EC2 instance for around $50 a month it's an amazing deal I couldn't pass up!

One of the perceived downsides of EC2 is that the instances aren't persistent. That's where Chef comes in! Chef is like a little system admin robot... you tell it how you want your system configured and it will do all the dirty work on the server.

Below I describe how to get started with EC2, use Chef to prepare the server for rails apps, and then use Capistrano to start up a sample rails app! It's really pretty easy!

Getting started with EC2

  • Read the Getting Started Guide
    • Setup an account
    • Setup the tools
  • Generate an SSH Key Pair
    • $ ec2-add-keypair ec2-keypair > ~/.ssh/ec2-keypair # I call it ec2-keypair, you can call it whatever you want
    • $ chmod 600 ~/.ssh/ec2-keypair # Keep it secret – keep it safe
  • Open up some ports on the default group
    • $ ec2-authorize default -p 22 # open up ssh port
    • $ ec2-authorize default -p 80 # open up web port
  • Get AMI instance number from alestic.com... I'm using ami-71fd1a18 (Ubuntu 8.04 LTS Hardy)
  • $ ec2-run-instances ami-71fd1a18 -k ec2-keypair --availability-zone 'us-east-1a'
  • $ ec2-describe-instances # Copy the public-dns, you'll need it later, it looks like ec2-XXX-XXX-XXX-XXX.compute-1.amazonaws.com
  • Create and attach a "db" Elastic Block Storage (EBS) volume. Read more about EBS Volumes here
    • $ ec2-create-volume --availability-zone 'us-east-1a' --size '5' # Grab the volume id
    • $ c2-attach-volume VOLUME_ID_GOES_HERE --device 'sdq2' --instance INSTANCE_ID_GOES_HERE # The instance has to be running for this to work
  • Create and attach a "data" Elastic Block Storage (EBS) volume
    • $ ec2-create-volume --availability-zone 'us-east-1a' --size '5' # Grab the volume id
    • $ c2-attach-volume VOLUME_ID_GOES_HERE --device 'sdq1' --instance INSTANCE_ID_GOES_HERE

You have an EC2 instance!

  • $ ssh -i ~/.ssh/ec2-keypair root@PUBLIC-DNS-OF-YOUR-EC2-INSTANCE # Don't get too excited yet, we need chef to make this tasty
  • $ logout
  • $ ssh-add ~/.ssh/ec2-keypair # Phew, Now you don't have to use the -i option again (until you restart you computer)

Setup chef

  • save this file on your local machine somewhere like /tmp/chef-bootstrap
#!/usr/bin/env ruby

def cmd(cmd)
  puts cmd; system(cmd)
end


puts "--------------"
puts "Chef Chef Chef"
puts

cmd "aptitude -y update"
cmd "aptitude -y install irb ri rdoc libshadow-ruby1.8 ruby1.8-dev gcc g++ curl"

cmd "curl -L 'http://rubyforge.org/frs/download.php/45905/rubygems-1.3.1.tgz' | tar xvzf -"
cmd "cd rubygems* && ruby setup.rb --no-ri --no-rdoc"
 
cmd "ln -sfv /usr/bin/gem1.8 /usr/bin/gem"

cmd "gem install rdoc chef ohai --no-ri --no-rdoc --source http://gems.opscode.com --source http://gems.rubyforge.org"

cmd "yes | mkfs -t ext3 /dev/sdq1"
cmd "yes | mkfs -t ext3 /dev/sdq2"

puts
puts "It seems to have worked!"
puts "------------------------"
  • $ scp /tmp/chef-bootstrap root@PUBLIC-DNS-OF-YOUR-EC2-INSTANCE:/tmp # Send this up to your EC2 Server
  • $ ssh "ruby /tmp/chef-bootstrap" # run it!
  • Chef and all it's dependencies are all setup!

Get cooking with chef

  • Grab my chef-sample cookbook/config from http://github.com/probablycorey/chef-sample
  • edit the file dna.rb (using the dna.sample.rb as a reference) to add apps, users, cronjobs, gems, packages, blah blah blah. Make sure you edit the authorized_keys parameter for each user so you can ssh in as them.
  • rake cook server=root@PUBLIC-DNS-OF-YOUR-EC2-INSTANCE

Put up a sample rails app

  • Grab my rails-sample app from http://github.com/probablycorey/rails-sample
  • edit RAILS_ROOT/config/deploy.rb to make it work with your dna.rb file
  • $ cap deploy:setup
  • $ cap deploy:start
  • $ cap deploy:migrations
  • Visit http://PUBLIC-DNS-OF-YOUR-EC2-INSTANCE You should see amazing things.

Helpful Links

User Comments

Recent Posts

  • Interactive console for iOS! - August 20, 2010
  • Archive

    • Letter to Steve Jobs - April 11, 2010
    • Wax talks to Twitter - October 20, 2009
    • How does iPhone Wax work? - October 19, 2009
    • Setting up iPhone Wax - October 18, 2009
    • Ruby (tinyrb) on iPhone - May 03, 2009
    • Building PCRE static lib for the iPhone - May 02, 2009
    • Amazon EC2 + Chef = Mmmmm - March 29, 2009
    • Objective-c key paths - February 13, 2009
    • POW! - December 26, 2008
    • Abusing Ruby's question mark methods. - November 28, 2008
    • Git hooks make me giddy - November 07, 2008
    • Ruby Equality! equal? eql? == and === - October 26, 2008
    • Ruby, Rails and Google Sitemaps - October 20, 2008
    • Projects

      • Wax Obj-C to Lua bridge for iPhone.
      • Pow a Ruby library for making file & directory manipulation easy.
      • MiniMagick a tiny RMagick replacement.