Tech machine

Don't Forget: You Can Use Amazon SimpleDB For Free!

Ottobre 3, 2009Nat0 Comments

We polled the attendees at a recent Amazon SimpleDB webinar and found that over half of them didn't know that they could start using the service for free. That's a shame because SimpleDB is easy to use, scales easily to handle high request rates, and is available in our US and EU regions.

You can keep up to 1 gigabyte of data in SimpleDB without paying any storage fees. You can transfer 1 GB of data and use up to 25 Machine Hours to process your queries each month. This should be sufficient to allow you to issue about 2 million PutAttribute or Select calls per month.

We have pages of SimpleDB sample code and libraries, plenty of
SimpleDB articles and tutorials, and some really good
SimpleDB documentation.

I've spent the last couple of months writing a book on AWS programming (to be published by SitePoint later this year) and I had a lot of fun with SimpleDB. The book is targeted at web developers, so I wrote all of the code in PHP and used the Tarzan toolkit (soon to be renamed Cloud Fusion). I had fun creating examples to show how to store RSS feeds, AWS usage information, and EBS snapshot metadata in SimpleDB.

Here are some examples of just how easy it is to store and retrieve data using SimpleDB, PHP, and Tarzan.

The first step is to include the class file:


Create a new domain and verify that the call succeeded:

$SDB = new AmazonSimpleDB();
$Res = $SDB->create_domain("my_domain");
if ($Res->isOK())
  // Succeeded
  // Failed

Store some items (error checking omitted to keep this post brief):

$Item1 = array("Name" => "Jeff", "Sex" => "M", "Age" => 49);
$Item2 = array("Name" => "Carmen", "Sex" => "F");
$Items = array("Jeff" => $Item1, "Carmen" => $Item2);

$Res = $SDB->batch_put_attributes("my_domain", $Items, true);

In this example I took advantage of SimpleDB's flexible schemaless model to avoid disclosing my wife's age.

Here's how to retrieve all items with Sex set to "F":

$Res = $SDB->select("select * from my_domain where Sex='F'");

The select method returns an object of type TarzanHTTPResponse. Digging in to this object is as simple as referencing $Res->body->SelectResult->Item:

foreach ($Res->body->SelectResult->Item as $Item)

The item is an object of type SimpleXMLElement. Here's what it looks like:

SimpleXMLElement Object
  [Name] => Carmen
  [Attribute] => Array
      [0] => SimpleXMLElement Object
          [Name] => Name
          [Value] => Carmen

      [1] => SimpleXMLElement Object
          [Name] => Sex
          [Value] => F

Here's how to access the SimpleDB attributes of the item:

foreach ($Res->body->SelectResult->Item as $Item)
  print("Key: " . $Item->Name . ":");
  foreach ($Item->Attribute as $Attribute)
    print("(" . $Attribute->Name . ": " . $Attribute->Value . ")");

What you can't see from the code, but which is important nevertheless, is that this code, if used at the core of a busy application, can handle thousands of storage or retrieval requests per second without any additional effort on my part. In addition, I don't have to worry about hardware failures, replicas, full disks, improper indexing, and so forth. I can focus on my application and on meeting the needs of my users.

— Jeff;