danmcc
danmcc

Reputation: 411

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?

Upvotes: 41

Views: 27648

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

Upvotes: 2

rynop
rynop

Reputation: 53499

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

Upvotes: 2

Arshad
Arshad

Reputation: 449

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.

Upvotes: 10

aaaristo
aaaristo

Reputation: 2169

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

Upvotes: 58

Related Questions