Bridging Rails to Amazon SimpleDB using ActiveResource

With SimpleDB, Amazon added the long-awaited database-like service to it's web services portfolio. A database was the one thing missing for building a complete web hosting stack with Amazon's services.

Using SimpleDB together with Ruby on Rails was the thing I immediately wanted to try. Some problems had to be dealt with first:

  1. Amazon does not provide any Ruby code for SimpleDB access
  2. SimpleDB has quite some limitations as detailed in a previous post

As Lars Schenk outlines on his Blog at least three different projects on RubyForge are addressing problem number one - only one of them has actual code, though. And that is aws-sdb by Tim Dysinger from Hawaii.

OK, using Tim's aws-sdb gem, one can get access to SimpleDB using Ruby. But using SimpleDB as a drop-in-replacement for a relational database and connecting Rails' ActiveRecord to it would require a fairly complex adapter - I am not sure, if this can be done at all, actually.

Looking at Rails, another interface comes to mind: ActiveResource. ActiveResource was written to connect model objects to RESTful web services as their datastore. That sounds like a fit. The APIs are different, but the functionality needed by ActiveResource can be provided by SimpleDB - it's all just CRUD after all.

All that's needed would be a adapter, a proxy, a proxy server? Yes, that's right. As it turned out, it's not too hard to write one, so here it is :-)

Announcing AWS SDB Proxy

My AWS SDB Proxy is a HTTP proxy server bridging ActiveResource calls from Rails to Amazon's SimpleDB Web Service allowing it to be used as a storage backend for Rails applications.

The proxy will listen for web service calls initiated by ActiveResource models and forward the requests to SimpleDB using the aws-sdb gem.

Install the AWS SDB Proxy Plugin from RubyForge as usual:

script/plugin install http://rug-b.rubyforge.org/svn/aws_sdb_proxy

Then follow the instructions provided in the README.

Features and Limitations

SimpleDB (and thus AWS SDB Proxy) do not use any pre-defined schema. Every record can potentially have different attributes. SimpleDB also has no data types associated with it's attributes, all data will be stored as strings.

AWS SDB Proxy adds a special _resource attribute, allowing storage of multiple models within the same SimpleDB domain. Record ids are generated using a SHA512 hash function to make key collisions extremely unlikely.

Recommend Martin Rehfeld on Working With RailsIf you like this plugin, please consider recommending me on Working with Rails. Thank you!

Related Posts