40

Amazon offers a local simulator for their Dynamodb product but the examples are only in PHP.

These examples mention passing the parameter "base_url" to specify that you're using a local Dynamodb, but that returns this error in Node:

{ [UnrecognizedClientException: The security token included in the request is invalid.]
  message: 'The security token included in the request is invalid.',
  code: 'UnrecognizedClientException',
  name: 'UnrecognizedClientException',
  statusCode: 400,
  retryable: false }

How do I get Dynamodb_local working in Node?

danmcc
  • 401
  • 1
  • 4
  • 5

4 Answers4

57

You should follow this blog post to setup your DynamoDB Local, an then you can simply use this code:

var AWS= require('aws-sdk'),
dyn= new AWS.DynamoDB({ endpoint: new AWS.Endpoint('http://localhost:8000') });

dyn.listTables(function (err, data)
{
   console.log('listTables',err,data);
});
aaaristo
  • 2,079
  • 14
  • 11
  • 1
    You may also be interested in https://github.com/aaaristo/dyngodb, in which case you can simply issue: _dyngodb --local_ – aaaristo Nov 09 '13 at 11:21
  • Thanks! It looks like the PHP SDK uses "base_url" where the Node SDK uses "endpoint". – danmcc Nov 09 '13 at 13:38
  • 7
    For others new to AWS like I was, after line 1 I needed to add `AWS.config.update({ accessKeyId: "myKeyId", secretAccessKey: "secretKey", region: "us-east-1" });` – Mark Rajcok Feb 16 '14 at 04:47
  • 2
    I suggest you to use the environment variables (AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY,AWS_REGION) instead of using AWS.config.update. http://aws.amazon.com/developers/getting-started/nodejs/ – aaaristo Feb 20 '14 at 15:50
  • I can't create a table with this code. `dyn.createTable({ TableName: 'myTbl', AttributeDefinitions: [ { AttributeName: 'aaa', AttributeType: 'S' }, ], KeySchema:[ { AttributeName: 'aaa', KeyType: 'HASH' } ] }, function() { dyn.listTables(function(err, data) { console.log(data) }); });` – user2503775 Feb 24 '15 at 12:44
  • please look https://stackoverflow.com/questions/48224061/how-to-query-local-dynamodb-using-dynamoose – Sabreena Jan 12 '18 at 10:57
8

For Node please do as below:

const AWS = require('aws-sdk');
const AWSaccessKeyId = 'not-important';
const AWSsecretAccessKey = 'not-important';      
const AWSregion = 'local';
const AWSendpoint = 'http://localhost:8000' // This is required
AWS.config.update({
    accessKeyId: AWSaccessKeyId,
    secretAccessKey: AWSsecretAccessKey,  
    region: AWSregion,
    endpoint: AWSendpoint
});

Ensure that DynamodDB is running on port 8000.

RDK
  • 4,540
  • 2
  • 20
  • 29
Arshad
  • 409
  • 4
  • 7
2

Here is how I do it, same code works local or inside AWS.

Simply leverage existence of env var DYNAMO_LOCAL_ENDPT="http://localhost:8000"

import { DynamoDB, Endpoint } from 'aws-sdk';

const ddb = new DynamoDB({ apiVersion: '2012-08-10' });

if (process.env['DYNAMO_LOCAL_ENDPT']) {
  ddb.endpoint = new Endpoint(process.env['DYNAMO_LOCAL_ENDPT']);
}
rynop
  • 50,086
  • 26
  • 101
  • 112
2

If you are using Nodejs with Typescript, this code might not work. Because there is no property call endpoint.

AWS.config.update({
  accessKeyId: AWSaccessKeyId,
  secretAccessKey: AWSsecretAccessKey,  
  region: AWSregion,
  endpoint: AWSendpoint 
});

You can use either,

dyn= new AWS.DynamoDB({ endpoint: new AWS.Endpoint('http://localhost:8000') });

or

AWS.config.dynamodb = {
  endpoint: new Endpoint('http://localhost:8000'),
  region: 'us-east-1'
}