AWS News Blog

Introducing the AWS SDK for Ruby

Voiced by Polly

Ruby is a wonderful programming language. Optimized for ‘developer joy’, it is an object oriented playground for building simple domain specific languages, orchestration tools, and most famously, web applications. In many ways, the Ruby language and Amazon cloud services such as EC2 and S3 have similar goals: to help developers and businesses build innovative products without worrying about the ‘muck’ that can slow development down.

Today we’re bringing Ruby and AWS even closer together, with the AWS SDK for Ruby.

The AWS SDK for Ruby gem

The SDK features a new Ruby gem for accessing a wealth of AWS compute, storage and middleware services whilst handling common tasks such as authentication, request retries, XML processing, error handling and more. With this release of the SDK you can access Amazon EC2S3, SQS, SNS and the Simple Email Service and we’ve included an object-relational mapper for SimpleDB. Our goal is to make building applications easier for Ruby developers with an officially supported and fine tuned development experience.

Getting Started

You can install the AWS SDK for Ruby gem from the command line:

sudo gem install aws-sdk

This will give you access to to a collection of AWS specific classes, such as AWS::EC2 and AWS::S3. After specifying your security credentials, you’re good to go. The best way to do this is via a simple YAML file:

access_key_id: your_access_key 
secret_access_key: your_secret_key 

You can then load the access credentials, and start issuing requests. For example, this code snippet will create a new bucket in S3 and upload a file as an object with public read access:

config = YAML.load(File.read(config_file))
AWS.config(config)

s3 = AWS::S3.new
bucket = s3.buckets.create(bucket_name)

basename = File.basename(file_name)
o = b.objects[basename]
o.write(:file => file_name, :acl => :public_read)

The SDK for Ruby detail page has more information and some great code samples to help you get started.

Rolling on Rails

 The AWS SDK for Ruby also contains an ORM interface to Amazon SimpleDB, called AWS::Record. Let’s see how we can use it as a data store from a Rails application. In this example, we’ll build a very simple social bookmarking site.

First, we’ll create a new Rails application. The AWS for Ruby documentation shows you how to set up your application by hand, but we can automate a lot of this with a simple application template:

rails new tagcloud -m http://ruby-sdk.s3.amazonaws.com/aws.rb

Add your credentials to config/aws.yml, and we can create our first model:

rails generate aws_record bookmark

Open up app/models/bookmark.rb, and we can start defining our model attributes. SimpleDB is a distributed key/attribute store and as such, doesn’t require a schema or database migrations. We can simply define our model attributes in this one place, and we’re ready to roll. Add the following to the class definition:

string_attr :title
string_attr :url
string_attr :tags, :set => true
timestamps 

This gives our Bookmark class three string attributes, a title, a URL, a collection of tags and timestamps. If it doesn’t already exist, we can create a new SimpleDB domain with a simple Rake task:

rake aws:create_domains

As you would expect if you’re used to working with ActiveRecord, we could create a new Bookmark object, set its attributes and save it to SimpleDB with:

b = Bookmark.new(
:title => ‘Amazon EC2’,
:url => ‘http://aws.amazon.com/ec2’,
:tags => [ ‘aws’, ‘cloud’, ‘compute’])

b.save

We could also retrieve and update an object with:

b = Bookmark.find(:first, :where => {:title => ‘Amazon EC2’})
b.tags += [‘elastic’]
b.save

SimpleDB offers a highly available, durable data store without the overhead of managing redundancy, replication or even any servers; a popular choice for online social games or metadata indexing. The AWS SDK for Ruby makes it super simple to get up and running, model your domain and persist data without worrying about migrations, schemas or database servers.

Other language support

Native software development tools can help integrate and automate the ecosystem of AWS services, and the AWS SDK for Ruby joins our library of developer tools for Java, PHP, iOS, Android and .Net.

Related links:

AWS SDK for Ruby Detail Page

AWS SDK for Ruby FAQ

Source and sample code on GitHub

— Matt

Modified 2/1/2021 – In an effort to ensure a great experience, expired links in this post have been updated or removed from the original post.
Jeff Barr

Jeff Barr

Jeff Barr is Chief Evangelist for AWS. He started this blog in 2004 and has been writing posts just about non-stop ever since.