»
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
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
View the discussion thread.
Recent Posts
-
August 20, 2010
Archive
-
April 11, 2010
-
October 20, 2009
-
October 19, 2009
-
October 18, 2009
-
May 03, 2009
-
May 02, 2009
-
March 29, 2009
-
February 13, 2009
-
December 26, 2008
-
November 28, 2008
-
November 07, 2008
-
October 26, 2008
-
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.