Friday, January 31, 2014

Introduction to DynamoDB with Node.js

To get started with DynamoDB and the AWS SDK for Node.js, leveraging IAM Roles in a simple t1.micro is a great place to start.

First, create an IAM Role with a policy which permits all DynamoDB APIs for now:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Action": [
        "dynamodb:*"
      ],
      "Effect": "Allow",
      "Resource": "*"
    }
  ]
}

Once an instance is launched in that role, install Node and load the AWS SDK module:

$ npm install aws-sdk
$ node
>

Because we deployed the EC2 instance in a Role, the SDK automaticlly assumes the IAM Role for temporary credential management. So let's load the SDK and point to the desired region:

> var AWS = require('aws-sdk');
undefined  
> AWS.config.update({region: 'us-west-2'});
undefined  

Having already created the sample tables from documentation based on the public DynamoDB documentation, it's fairly easy to use the quick sample to query the tables in the region:

> var db = new AWS.DynamoDB();
undefined  
> db.listTables(function(err, data) {
...    console.log(data.TableNames);
... });
{ service: ... }
> [ 'Forum', 'ProductCatalog', 'Reply', 'Thread' ]

And there we go, a simple start to the use of Node.js with IAM Roles in EC2 instance to list existing tables in a specific region.

No comments:

Post a Comment