EC2 on Rails
UPDATE: This project is no longer being maintained.
Deploy a Ruby on Rails app on EC2 in five minutes
EC2 on Rails is an Ubuntu Linux server image for Amazon’s EC2 hosting service that’s ready to run a standard Ruby on Rails application with little or no customization. It’s a Ruby on Rails virtual appliance.
If you have an EC2 account and can start EC2 instances you’re five minutes away from deploying your Rails app.
EC2 on Rails is opinionated software: the opinion is that for many rails apps the server setup can be generalized and shared the same way as the web application framework itself. For many people (Twitter, this isn’t for you) the server image can be treated the same way as other shared libraries. And if the day comes when your needs are unique enough that EC2 on Rails can’t be configured to work for you then you can bundle your own image from it or fork the build source and customize it.
But until then, why spend your time configuring servers?
Features of the EC2 image:
- Ready to deploy a Rails app with little or no configuration of the server required
- Automatic backup of MySQL database to S3 (full backup nightly + incremental backup using binary logs every 5 minutes)
- Capistrano tasks to customize the server image, archive and restore the database to/from S3, and more (available as a rubygem)
- Mongrel_cluster behind Apache 2.2, configured according to Coda Hale’s excellent guide
- Ruby on Rails 2.1.0, 2.0.2 and 1.2.6
- Ruby 1.8.6
- MySQL 5
- monit configured to monitor apache, mongrel, mysql, memcached, drive space and system load
- Ubuntu 8.04 LTS “Hardy” base image built using Eric Hammond’s EC2 Ubuntu script
- SSL support
- Amazon AMI tools installed
- MySQL, Apache, and syslog configured to use /mnt for data and logging so you don’t fill up EC2’s small root filesystem
- Automatically archives Rails and Apache logs to S3 nightly.
- 32-bit and 64-bit images available (supports all instance types, small to extra large).
- Created using a build file, full source is available (the EC2 on Rails script is run from Eric Hammond’s EC2 Ubuntu script)
- Can be used as a clustered Rails app running on multiple instances
- Automatically runs hourly, daily, weekly and monthly scripts if they exist in Rails application’s script directory
- Local Postfix SMTP mail server (only available from within the instance, not listening on external network interfaces)
Using the image
This documentation will be improved soon, for now hopefully this covers the basics.
The current AMI id’s in the U.S. region are:
- ami-5394733a (32-bit)
- ami-5594733c (64-bit)
The current AMI id’s in the E.U. region are:
- ami-761c3402 (32-bit)
- ami-701c3404 (64-bit)
I will keep these images available for as long as possible, they will not be deleted for at least a few years.
1. Install the gem
sudo gem install ec2onrails
2. Add the config files to your Rails app
Be sure to customize those files and read the comments.
Also, use the hostname “db_primary” in your database.yml file. After running “cap ec2onrails:server:set_roles” it will resolve to the instance defined in your Capistrano “db” role.
4. Start up one or more instances of the image.
There is nothing EC2 on Rails-specific here yet (though soon there will be a Capistrano task to do this for you), if you’ve started EC2 instances before you can skip this section. Otherwise, I’m not going to lie, this part is complicated and will take a lot more than 5 minutes the first time.
Read the running an instance section in Amazon’s getting started guide.
For the AMI id’s of the current images do
cap ec2onrails:ami_ids from within the app that you configured in the previous step (they’re also listed earlier on this page).
NOTE: Only use the images that match the current version of the gem.
Most basic things can be configured automatically by the Capistrano tasks, but if you want to you can login by ssh as a user named “admin” (has sudo ability) or as “app” (the user that the app runs as, does not have sudo ability). The Capistrano tasks automatically use the app user to deploy the app, and the admin user for server admin tasks that require sudo.
IMPORTANT: Double-check your firewall settings. Be sure that you haven’t allowed public access to any ports other than TCP 22 and TCP 80 (and possibly TCP 443 if you’re going to enable HTTPS). If you’re using multiple instances, be sure to allow them network access to each other.
5. Copy your public key from the server to keep Capistrano happy
This is a workaround for a quirk in Capistrano. Technically all you should need to connect to the server is the private key file, the public key is on the server. But for some reason Capistrano requires that you have both the public key and the private key files together on the client.
There is a Capistrano task that tries to fix this for you. From within the root of your rails app do:
Note, this will only work if you have an external ssh command in the path, it won’t work for most Windows users.
6. Deploy the app with Capistrano
Now that the gem is installed, your deploy.rb is configured and you can start and stop EC2 instances, this is the only thing you’ll need to do from now on.
cap ec2onrails:setup cap deploy:cold
Yes, it’s that easy! The setup task will set the server’s timezone, install any gems and Ubuntu packages that you specified in the config file, and create your database.
That’s it, your app is now running on EC2!!
Capistrano is the most commonly used Rails deployment tool. It comes with many standard “tasks”, and the EC2 on Rails gem includes Capistrano tasks specifically for configuring the server instance.
Capistrano is run from the command-line using the “cap” command, with a task name given as an argument.
In addition to the
cap ec2onrails:setup task, you’ll mostly need just the following Capistrano tasks:
Shows the AMI id’s of the images that match the current version of the gem.
This task configures a newly-launched instance. This is the first thing you should do after starting a new instance. It can be run more than once without ill effect. After running “cap ec2onrails:setup” the next thing to do is run “cap deploy:cold”
Customizes each instance for it’s role(s) (as defined in your Capistrano deploy.rb file). Run this after starting or stopping instances. For now this just makes sure that only the appropriate services (Apache, Mongrel, and/or MySQL) are running. Eventually this will customize settings for the running services also. Note that an instance can have more than one role. If there’s only one instance it will have all roles.
Note that due to the way that Capistrano works all tasks are run against all hosts that are currently defined in the deploy.rb file. So if you start a new instance then add it to your deploy.rb you will need to run “cap ec2onrails:setup” again which will be run on all existing instances.
Database management tasks
Archive the MySQL database to the bucket specified in your deploy.rb. This is for archiving a snapshot of your database into any S3 bucket. For example, you might want to do this before deploying.
Restore the MySQL database from the bucket specified in your deploy.rb. For example, I use this to restore the current production data (from my actual production backup bucket) onto a staging server that has the current production version of my app. I then deploy the new version which tests migrations exactly as they’ll run on the production server.
To get a full list of the Capistrano tasks at any time type
cap -T from with your rails app root.
Building the image
Building the image is not required, most people will simply use the prebuilt public image, but there is also a build script that builds the image. It’s meant to be called by Eric Hammond’s EC2 Ubuntu script.
See the change log.
How to submit patches
Pleae read the 8 steps for fixing other people’s code.
The source code can be checked out anonymously using:
git clone git://github.com/pauldowman/ec2onrails.git
This code is free to use under the terms of the GPL v2.
If you find EC2 on Rails useful please recommend Paul Dowman at Working With Rails.