⤺ /text/useful


How to use AWS SimpleDB from Ruby

by Peter Cooper - 2020-05-04

SimpleDB is a simple way to store ‘items’ of data in the cloud using ‘attributes’ and ‘values’ (so think a hash of hashes). If you’ve used DynamoDB, think far simpler still!

Sample data

It’s cheap/free. You get 25 hours (this time is only used up by actual query time) and 1GB of storage for free.

However, it’s a commonly forgotten AWS service, there’s no admin interface for it in the AWS Console, and you don’t see many tutorials or blog posts talking about it. DynamoDB is considered to have succeeded SimpleDB but is more complex to work with.

Would I make SimpleDB the authoritative database of a large scale production app? No. Would I use it to store ephemeral data for experiments, Lambda functions or short lived processes? Perhaps!

Setting up the IAM user and role

First things first when using a new AWS service is to set up the authentication for using it.

Either create a new user in IAM or pick an existing user and apply a policy that allows that user to access all of the SimpleDB resources available within the account and perform all operations against them:

    "Version": "2012-10-17",
    "Statement": [
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": "sdb:*",
            "Resource": "*"

You can also limit the policy to only allowing a user to access a certain subset of SimpleDB resources. If you understand IAM at all, this is easy to do by following this documentation.

Setting up Ruby

You could install the whole of the aws-sdk gem set, but you don’t have to. Add aws-sdk-simpledb to your Gemfile or install it:

gem install aws-sdk-simpledb

There are a few different ways you can supply AWS credentials and region information to the AWS SDK for Ruby.

I prefer to use environment variables (since the technique translates well to deployment) via a .env file and the Dotenv library. So my .env would look like this:


You can also specify these details directly in your Ruby code (example below) which might suit certain use cases like local one off scripts.

Storing and reading data with Ruby

Data within SimpleDB is stored in ‘domains’. Think of these as simple namespaces. You can use IAM policies to limit certain users to certain domains, but our policy above lets us use anything we like because we’re cool and like to live on the edge.

First, we need to get connected and create a domain:

require 'dotenv'
require 'aws-sdk-simpledb'

sdb = Aws::SimpleDB::Client.new

sdb.create_domain(domain_name: "myappdata")

If you decide to specify the access credentials and region on the client line, you can skip the dotenv stuff, and do this instead:

  region: 'us-east-1',
  credentials: Aws::Credentials.new('KEYHERE', 'SECRETHERE')

Once you’ve created a domain, you can “put” stuff to it or “get” stuff from it like so:

  domain_name: "myappdata",
  item_name: "mykey",
  attributes: [
    { name: 'attrname', value: 'hello world' }

Or to get items back out:

res = sdb.get_attributes(
  domain_name: "myappdata",
  item_name: "mykey",

p res.attributes[0].name
p res.attributes[0].value

You can also delete:

  domain_name: "myappdata",
  item_name: "mykey",

I’m not your dad and I’m writing these notes mostly for myself so you’re on your own from this point.

You can learn more in the SimpleDB Developer Guide including how to craft simple SQL-style queries and how to conditionally put or delete data. Enjoy!