'How I can work with Amazon's Dynamodb Local in Node?

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?



Solution 1:[1]

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);
});

Solution 2:[2]

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.

Solution 3:[3]

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']);
}

Solution 4:[4]

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'
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 RDK
Solution 3 rynop
Solution 4 Kavinda Shehan Rajapakshe