Amazon Web Services EC2 Ruby Gem

Get Version 0.2.0

→ ‘amazon-ec2’

About

Amazon Web Services offers a compute power on demand capability known as the Elastic Compute Cloud (EC2). The server resources in the cloud can be provisioned on demand by making HTTP Query API calls to EC2.

This ‘amazon-ec2’ Ruby Gem is an interface library that can be used to interact with the Amazon EC2 system and control server resources on demand from your Ruby scripts, or from Ruby on Rails applications.

Important! : Are you upgrading from an earlier release?

This latest release of the gem has undergone a pretty massive re-write. It is no longer compatible with code you may have written that made use of an older version of ‘amazon-ec2’ (< 0.2.0).

Any other gems which depended on ‘amazon-ec2’ < 0.2.0 will also no longer work with the new release. The only project I am aware of that depended on the earlier version of ‘amazon-ec2’ was ‘Capazon’. This project, which allows you to control EC2 from your Capistrano recipies, has now been deprecated and is being reborn as ‘Capsize’. I rewrote the code for Capsize with the assistance of the original creator of Capazon; Jesse Newland. Feel free to check out Capsize to learn more about this exciting new release!

While we apologize for not being able to maintain backward compatibility with the original ‘amazon-ec2’ gem, there were just too many major enhancements that needed to be made and it just was’nt possible. I hope that the new robustness and new features in this new release make any transitional pain worthwhile! Of course the old versions are still out there on RubyForge, and you can still install them if you pass the version string into the gem install command. I just don’t recommend it unless you really have to.

What’s new?

This release (>= 0.2.0) represents a major re-write of this gem and contains contributions from several people who make use of this gem for their own applications. Trust us, its worth the pain of upgrading if you have been using an older version. Some of the major enhancements you will find are:

Installing

This gem follows the standard conventions for installation on any system with Ruby and RubyGems installed. If you have worked with gems before this will look very familiar.

Installation pre-requisites

Amazon Web Services developer account.

You’ll need an account with AWS in order to use this gem at all. That should be your first stop on this tour. Your account must also be enabled for Amazon EC2 usage. After signup you’ll be provided with an ‘Access Key ID’ and a ‘Secret Access Key’. These allow you to authenticate any API calls you make and ensure correct billing to you for usage of their service. Take note of these keys (and keep them safe and secret!).

Gem Dependencies

The following gems should be installed automatically as part of your install of amazon-ec2. Most of them are testing or build dependencies but they should be painless to install even if you don’t plan on running the tests or building this gem manually on your own.

XmlSimple (required)

Mocha (optional for testing)

Rcov (optional for testing)

Test-Spec (optional for testing)

Syntax (optional for building your own copy of the gem and its docs)

RedCloth (optional for building your own copy of the gem and its docs)

Installing the gem

Linux / OS X :

sudo gem install amazon-ec2 --include-dependencies

Microsoft Windows :

gem install amazon-ec2 --include-dependencies

Using the library

Setting up…

The ‘ec2sh’ and ‘ec2-gem-example.rb’ scripts which will be introduced to you shortly expect your AWS EC2 credentials to be stored as shell environment variables which are accessible to those scripts. This makes them convenient to use whenever you need to do a quick query to see what images you have available to you, whats running now, or to start or stop an instance on EC2. You’ll find ‘ec2sh’ to be a very handy tool. I’ll describe only the OS X route for setting up (of course the setup steps will vary depending on your particular system and preferred shell). If you don’t want to do it this way, feel free to copy these scripts from the gem dir to any location where you can run them from and modify them directly to include your credentials.

OS X Setup

Edit the file ~/.bash_login and add the following to the existing contents:


export RUBYOPT="rubygems" 

# For amazon-ec2 and amazon s3 ruby gems
export AMAZON_ACCESS_KEY_ID="YOUR_ACCESS_KEY_ID" 
export AMAZON_SECRET_ACCESS_KEY="YOUR_SECRET_ACCESS_KEY_ID" 

Once you save the file you should close and re-open your command terminal so the new variables are made available. You’ll need to do this close/re-open step for each terminal window you have open (or issue the ‘source ~/.bash_login’ command in each). Make sure that this file is only readable by your user so you don’t inadvertantly expose your credentials to other users on your system.

You can verify that this setup is complete by running the ‘set’ in a command window and seeing that your credentials are in the list of shell variables.

The basics…

The library exposes one main interface module

EC2::Base

This method requires arguments which include your AWS credentials and it will return an object that you can use to make method calls directly against EC2. All the operations for using the EC2 service, including query string header signing, are handled automatically for you. The connection string will look something like this:

@ec2 = EC2::Base.new(:access_key_id => ACCESS_KEY_ID, :secret_access_key => SECRET_ACCESS_KEY)

We have tried to keep the public methods on ‘amazon-ec2’ as close as possible to the AWS EC2 Query API. This similarity allows you to reference the Query API Reference in the EC2 Developer Guide and be able to get started right away. In most cases the methods names only differ in how they are presented. e.g. ‘DescribeImages’ becomes ’#describe_images() in Ruby. Feel free to browse the full RDoc documentation for all classes and methods of ‘amazon-ec2’ if you want more details.

Examples

The best way to become familiar with ‘amazon-ec2’ is to take it for a test drive. We have provided a few simple ways to get you started. There is also some sample code below that should help out in using ‘amazon-ec2’ with a plain Ruby script, or as part of a Ruby on Rails application.

Using the ‘ec2-gem-example.rb’ sample test script

An example Ruby script which exercises the library a bit more is installed for you to check out when you install this gem. You can run this script to verify that everything is setup and working correctly in your environment. Consult the file which is installed at :

[your amazon-ec2 gem dir]/examples/ec2-example.rb

Since we also package this sample file in the gem’s bin/ dir you should also be able to run it from anywhere on your shell path (once you have set your environment variables as described above).

Using the ‘ec2sh’ command shell

The ‘ec2sh’ command shell is actually a standard ‘irb’ Ruby shell, with the main difference being we read your AWS credentials from your environment and pre-configure a connection string for you. This lets you run any EC2 command very simply. This has proven to be a valuable tool during the development of this gem and you should try it out. Since we install this tool in your system path as part of the installation of this gem, you should be able to simply run ‘ec2sh’ from any terminal command prompt on your local system. You’ll see some basic instructions for use, and a few examples when you start ‘ec2sh’. Go ahead and try it out now. We’ll wait…

If your not in front of a terminal shell now (perhaps you’re browsing this site on your iPhone) this is what you would see:


hostname:/tmp/rails/amazon_test glenn$ ec2sh

  'ec2sh' usage :
  This is an interactive 'irb' command shell that allows you to use all 
  commands available to the amazon-ec2 gem.  You'll find this to be a
  great tool to help you debug issues and practice running commands
  against the live EC2 servers prior to putting them in your code.

  The EC2 connection is wired to the class instance '@ec2'.  Make method calls 
  on this to execute commands on EC2.  Adding a #to_s 
  at the end of any command should give you a full String representation of the
  response.  The #xml data is available for each response
  which allows you to view the full and complete XML response returned by
  EC2 without any parsing applied.  This is useful for viewing the 
  hierarchy of an entire response in a friendly way (if XML is friendly
  to you!).  Understanding the hierarchy of the XML response is critical
  to making effective use of this library.

    Examples to try:

      returns : all ec2 public methods
      >> @ec2.methods.sort

      returns : a string representation of ALL images
      >> @ec2.describe_images.to_s

      returns : an Array of EC2::Response objects, each an EC2 image and its data
      >> @ec2.describe_images.imagesSet.item
      >> @ec2.describe_images.imagesSet.item[0] (an OpenStruct of a single item in that array)
      >> @ec2.describe_images.imagesSet.item[0].to_s (a String representation of that OpenStruct item)

      returns : an XML representation of all images
      >> puts @ec2.describe_images.xml

      returns : an XML representation of all images owned by Amazon
      >> puts @ec2.describe_images(:owner_id => 'amazon').xml

>> @ec2.describe_images.imagesSet.item[0].to_s
=> "#<EC2::Response:0x100A465B4 imageId=\"ami-018e6b68\" imageLocation=\"rbuilder-online/phonehome-1.5.6-x86_10132.img.manifest.xml\" imageOwnerId=\"099034111737\" imageState=\"available\" isPublic=\"true\" parent=#<EC2::Response:0x100A469A6 ...>>" 

Using ‘amazon-ec2’ in Ruby scripts

Try out the following bit of code. This should walk through each image returned by a call to #describe_images and print out its key data. Note in the example below that you cannot walk through the results of the #describe_images call with the ’.each’ iterator (You’ll get errors if you try). You need to instead walk through the Array of items which are in the ‘imagesSet’ embedded in the response. This reflects exactly the XML hierarchy of data returned from EC2 which we parse to Ruby OpenStruct objects (EC2::Response).

#!/usr/bin/env ruby

require 'rubygems'
require 'ec2'

ACCESS_KEY_ID = '--YOUR AWS ACCESS KEY ID--'
SECRET_ACCESS_KEY = '--YOUR AWS SECRET ACCESS KEY--'

ec2 = EC2::Base.new(:access_key_id => ACCESS_KEY_ID, :secret_access_key => SECRET_ACCESS_KEY)

puts "----- listing images owned by 'amazon' -----" 
ec2.describe_images(:owner_id => "amazon").imagesSet.item.each do |image|
  # OpenStruct objects have members!
  image.members.each do |member|
    puts "#{member} => #{image[member]}" 
  end
end

Using ‘amazon-ec2’ in Ruby on Rails applications

in config/environment.rb:

# Require the amazon-ec2 gem and make its methods available in your Rails app
# Put this at the bottom of your environment.rb
require 'EC2'
in app/controllers/your_controller.rb:

[some controller code ...]

ec2 = EC2::Base.new(:access_key_id => "YOUR_AWS_ACCESS_KEY_ID", :secret_access_key => "YOUR_AWS_SECRET_ACCESS_KEY")

# get ALL public images
@ec2_images = ec2.describe_images().imagesSet.item

# Get info on all public EC2 images created by the Amazon EC2 team.
@ec2_images_amazon = ec2.describe_images(:owner_id => "amazon").imagesSet.item

[some more controller code ...]

and then you can show off your EC2 image data with some code in app/views/your_view.rhtml:


<h1>EC2 Test#index</h1>

<h1>Sample 1 - debug() view</h1>

<%= debug(@ec2_images_amazon) %>

<h1>Sample 2 - Build a table</h1>

<table border='1'>
  <tr>
    <th>image.imageId</th>
    <th>image.imageLocation</th>
    <th>image.imageOwnerId</th>
    <th>image.imageState</th>
    <th>image.isPublic</th>
  </tr>

  <% for image in @ec2_images_amazon %>
    <tr>
      <td><%=h image.imageId %></td>
      <td><%=h image.imageLocation %></td>
      <td><%=h image.imageOwnerId %></td>
      <td><%=h image.imageState %></td>
      <td><%=h image.isPublic %></td>
    </tr>
  <% end %>
</table>

<h1>Sample 3 - Iterate</h1>

<% @ec2_images_amazon.each do |image| %>
    <% image.each_pair do |key, value| %>
        <% unless key == 'parent' %>
            <%= "#{key} => #{value}" %><br />
        <% end %>
    <% end %>
    <br />
<% end %>

Important notes regarding the structure of EC2::Response Objects

One of the key benefits of this new version of the library is that all responses from EC2 are bundled up in a real data structure and no longer require parsing of text. We use an OpenStruct as the parent for the EC2::Response object and we populate it directly from the XML given to us by EC2 in response to any command we issue. This means that future changes to the API and what is returned by EC2 will largely be handled transparently by the gem. This is a huge benefit. What this means though, is that you may have to do a little homework on what actually gets returned by EC2 as XML. For example, when you make a #describe_images call in ec2sh to EC2 what you will get back will look like:

$ ec2sh
>> puts @ec2.describe_images(:owner_id => 'amazon').xml

<?xml version="1.0"?>
<DescribeImagesResponse xmlns="http://ec2.amazonaws.com/doc/2007-01-19/">
    <imagesSet>
        <item>
            <imageId>ami-20b65349</imageId>
            <imageLocation>ec2-public-images/fedora-core4-base.manifest.xml</imageLocation>
            <imageState>available</imageState>
            <imageOwnerId>amazon</imageOwnerId>
            <isPublic>true</isPublic>
        </item>
        <item>
            <imageId>ami-22b6534b</imageId>
            <imageLocation>ec2-public-images/fedora-core4-mysql.manifest.xml</imageLocation>
            <imageState>available</imageState>
            <imageOwnerId>amazon</imageOwnerId>
            <isPublic>true</isPublic>
        </item>
        <item>
            <imageId>ami-23b6534a</imageId>
            <imageLocation>ec2-public-images/fedora-core4-apache.manifest.xml</imageLocation>
            <imageState>available</imageState>
            <imageOwnerId>amazon</imageOwnerId>
            <isPublic>true</isPublic>
        </item>
        <item>
            <imageId>ami-25b6534c</imageId>
            <imageLocation>ec2-public-images/fedora-core4-apache-mysql.manifest.xml</imageLocation>
            <imageState>available</imageState>
            <imageOwnerId>amazon</imageOwnerId>
            <isPublic>true</isPublic>
        </item>
        <item>
            <imageId>ami-26b6534f</imageId>
            <imageLocation>ec2-public-images/developer-image.manifest.xml</imageLocation>
            <imageState>available</imageState>
            <imageOwnerId>amazon</imageOwnerId>
            <isPublic>true</isPublic>
        </item>
        <item>
            <imageId>ami-2bb65342</imageId>
            <imageLocation>ec2-public-images/getting-started.manifest.xml</imageLocation>
            <imageState>available</imageState>
            <imageOwnerId>amazon</imageOwnerId>
            <isPublic>true</isPublic>
        </item>
    </imagesSet>
</DescribeImagesResponse>

You can see in the XML the structure that you will need to follow when constructing queries for information and parsing responses from EC2.

So, for example, if you wanted to get the image ID of the third image listed in the response above you would need to do:

>> puts @ec2.describe_images(:owner_id => 'amazon').imagesSet.item[2].imageId
ami-23b6534a

EC2 will typically return ‘sets’ of things (imagesSet, reservationSet, etc.) which we map to ruby Arrays (.imagesSet.item in the example above). If you want to iterate over a response set you will need to iterate over this array. The Arrays will typically contain additional EC2::Response objects that represent each individual item. You’ll find that you can use the ‘ec2sh’ to help you understand the structure more completely if you try issuing commands there as a way to practice seeing what will be returned and making sure you get exactly what you want. You can always call the EC2::Response#xml method like I did above to see the exact XML returned which allows you to easily derive the structure for the Ruby OpenStruct object.

Contributing

We can always use your help! Do you have Ruby skills? Do you see a bug or enhancement that you’d like to see fixed? We would love to have your patches for documentation, test cases, or enhancements.

Project Info

This project is managed as a RubyForge project which you can find at http://amazon-ec2.rubyforge.org/ and this is always the best place to find the latest news, report any bugs, submit feature requests, or provide patches.

Learning More

Documentation

Complete RDoc generated documentation can be found at http://amazon-ec2.rubyforge.org/rdoc/

Websites

Amazon Web Services Home Project Home Downloads Browse Code Report Bugs Request Features Submit Patches

Related Projects

Capsize : A Capistrano (>= 2.x) plugin that allows complete control of Amazon EC2 from Capistrano recipes.

Credits

The original sample code for this library was provided by Amazon Web Services, LLC. Thanks to them for providing all of us with samples that got this started. This latest version of amazon-ec2 doesn’t much resemble the original. They got us going though and thanks to the EC2 team for including Ruby in their plans. We hope to see more AWS Ruby code.

Thanks to all the great folks who submitted patches and kept this project rolling. I would especially like to thank Sean Knapp, Kevin Clark, and Randy Bias. Your patches and help are much appreciated.

Thanks to Dr. Nic Williams and his great ‘NewGem’ Ruby Gem Generator. This gem of a Gem helped me package up this code for distribution in a flash! You can find Dr. Nic’s NewGem generator at http://newgem.rubyforge.org/

Contact

Comments, patches, and bug reports are welcome. Send an email to the address below or use the RubyForge forum for this project.

Glenn Rempe, 3rd July 2007