TvE 2100

At 2100 feet above Santa Barbara

Launch Your Rails App in Minutes on Amazon EC2 Using RightScale

I previously showed-off our Rails-with-Mephisto server template so now it’s time for the Rails-all-in-One server template which makes it very easy to get your own Rails application running on Amazon EC2. We’ve made this template public, so any RightScale user can use it for free!

Let me show how you can configure and deploy your own Rails app in just minutes by using our publicly available Rails All-In-One server template.

Before we begin…

Before jumping into configuring the server template to launch your app, there are two important choices that we need to make: “How will we deploy application code to the server?” and “How will we initialize the database?”

Well, there are multiple ways in which we can proceed on either question, and the good news is that all are possible using RightScripts and we support several options with the templates we provide. Here’s what our Rails-all-in-one template supports:

  • Application code:
  • Download and install your rails application code from a tarball in S3.
  • Checkout your rails application using an external svn repository.
  • Boot with capistrano already set up, and remotely deploy your app with ‘cap deploy’.

  • DB initialization:

  • Download and apply a mysqldump file from S3.
  • Run a rake task to initialize and/or populate the DB.
  • Boot with an empty DB.

Let’s start with the option of downloading the code and a mysqldump from S3, because these methods are supported out-of-the-box with our template. The example below focuses specifically on this option and I’ll follow-up shortly with a post describing the other options.

Step 1- Prepare your data

The only thing that you need to do in this step is to tar up your Rails application at your base directory, take a mysqldump of the database and store these two files somewhere accessible in S3. For example, to tar up my Rails application in my development machine I could do something like:

# cd /home/rails/myapp
# tar czf /tmp/myapp.tgz .

To generate a file with the initial DB contents, I would first populate or bootstrap the DB with the data I want to start with, and then copy the resulting file to S3. For example, to generate a dump of the “myapp_production” database I could do something like:

# suffix=`date “+%Y%m%d%H%M%S”`
# mysqldump –single-transaction -u root myapp_production | gzip -c > /tmp/myapp_prod_dump-$suffix.gz

Having generated the code tarball and the mysqldump files, we just need to upload them to S3. There’s a million and one ways in which to do that, so everyone can use his tool of choice. For convenience, the RightScale site contains an integrated S3 browser and file manager so that no external tools are necessary to perform these tasks.After logging into RightScale, go to “Manage > Storage”, select the bucket where to put the files (or create one if you don’t have any) and use the form at the bottom of the page to upload the 2 files.

For example, if I wanted to store the files I generated (i.e., “myapp.tgz” and “myapp_prod_dump-20070913224229.gz” ) into the ‘bucket_for_myapp’ bucket, the page showing the listing of the bucket would look (approximately) like:

S3 browser view

Step 2- Configure your template

Now that we have the two files ready in S3, it’s time to configure the template for our Rails application. This is done in “Design > Server Templates”, where we locate the “Rails all-in-one” template in the “Premium/Public” section, and click on the “edit” icon next to it. The edit page looks like this:

server template before edit

As you can see, there are a few variables that you can use to customize to your taste. However we’ve pre-set most of them to match common setups. The net effect is that you can probably get by with filling out only 6 text boxes.

Before I continue with the example of my Rails application called “myapp”, let’s address some security pre-requisites. When you try this with your own RightScale account, you need to make sure that you have an “ssh key” and select an appropriate “security group”. Since you are configuring a web server you must allow traffic to port 80 in the selected security group. If you have never done that before, please refer to the “quick troubleshooting” in our blog entry for reference. Once you’ve selected a valid ssh key and security group, you can proceed to fill out the main template configuration.

Back to the example! There are only 4 things that I need to do: 1. Fill in APPLICATION with the name of my Rails app: “myapp” 1. Fill in APPLICATION_CODE_BUCKET and APPLICATION_CODE_PACKAGE with the location in S3 where I have stored the code tarball. That’s “bucket_for_myapp” and “myapp.tgz” respectively 1. Fill in the DB_SCHEMA_NAME with the name of the mysql database my application needs to use: “myapp_production” 1. Fill in the DB_MYSQLDUMP_BUCKET and DB_MYSQLDUMP_PREFIX with the location in S3 where I have stored the mysqldump file: “bucket_for_myapp” and “myapp_prod_dump-20070913224229.gz”

Here we are ready to hit the save button:

server template ready to save

Step 3 - wait, there is no step 3!

That’s right, since this is a simple example I just need to click on the launch button, sit back and let the RightScale infrastructure take care of the rest:

server launch

But what happens if my cool application needs some special gems to be installed in order to run? No problem, it’s as simple as editing the template again and filling in the OPT_GEMS_LIST with a space-separated list of the gems needed. (Currently the RightScript that installs these custom gems will not handle gem installations that require manual intervention, for example, gems in which one must select the appropriate architecture to install.)

Can’t wait for the server to boot, even though it takes only a couple of minutes. Less than the time to get on the phone with a traditional hosting company sales person and place an order… Soon the server will become “operational” in the taskbar on the left of the screen. Clicking on the instance name brings up the instance information page from which the “dns name” link (something like leads straight to the web site on the instance. Tada! my application is live!

An interesting perk from using this template is that the database gets automatically backed-up to S3 every night. The backup is tagged with a timestamp and stored in the bucket configured in the template (i.e., the DB_MYSQLDUMP_BUCKET and DB_MYSQLDUMP_PREFIX variables). Also, the next time the template is launched again, the initial DB contents will be automatically restored from the latest of the mysqldump backups that match the configured prefix (i.e., the file that matches the prefix and that has the highest number suffix).

As you see, deploying your rails application doesn’t take much at all when using the RightScale infrastructure and the new Rails all-in-one template. Once you’ve done it once, it would probably take less time to configure a brand new application than reading this blog entry. In a follow-up article I’ll explore some of the details of the provided server templates and RightScripts and will show how to take advantage of it to fully customize your Rails application, however non-standard and unique it is.

If you’re interested in learning more about RightScale, please contact us at The Rails all-in-one server template is available with RightScale’s free accounts, but more complex set-ups are reserved for the premium accounts.