0

I recently discovered DynamoDB Local and started building it into my project for local development. I decided to go the docker image route (as opposed to the downloadable .jar file.

That being said I've gotten image up and running and have created a table and can successfully interact with the docker container via the aws cli. aws dynamodb list-tables --endpoint-url http://localhost:8042 successfully returns the table I created previously.

However, when I run my lambda function and set my aws config like so.

const axios = require('axios')
const cheerio = require('cheerio')
const randstring = require('randomstring')
const aws = require('aws-sdk')
const dynamodb = new aws.DynamoDB.DocumentClient()

exports.lambdaHandler = async (event, context) => {

    let isLocal = process.env.AWS_SAM_LOCAL

    if (isLocal) {
        aws.config.update({
            endpoint: new aws.Endpoint("http://localhost:8042")
        })
    }

(which I have confirmed is getting set) it actually writes to the table (with the same name of the local dynamodb instance) in the live AWS Webservice as opposed to the local container and table.

It's also worth mentioning I'm unable to connect to the local instance of DynamoDB with the AWS NoSQL Workbench tool even though it's configured to point to http://localhost:8042 as well...

Am I missing something? Any help would be greatly appreciated. I can provide any more information if I haven't already done so as well :D

Thanks.

RoflWaffle17
  • 171
  • 4
  • 14
  • Does it make a difference if you use `endpoint: new AWS.Endpoint('http://localhost:8042')`? – jarmod Mar 16 '20 at 00:34
  • let me give that a go! To clarify, you're suggesting `if (isLocal) { aws.config.update({ AWS.endpoint: "http://localhost:8042" }) }` – RoflWaffle17 Mar 16 '20 at 01:21
  • Oh I see what you mean, Let me give it a-go! – RoflWaffle17 Mar 16 '20 at 01:25
  • @jarmod that unfortunately didn't work. It's still writing to the my live table out in AWS DynamoDB Webservice. – RoflWaffle17 Mar 16 '20 at 01:32
  • Is your client object created before or after modifying the config to indicate local endpoint? The endpoint should be set first. Or simply pass the new AWS.Endpoint to the AWS.DynamoDB constructor. – jarmod Mar 16 '20 at 01:36
  • I will update my original post showing the top of my code. – RoflWaffle17 Mar 16 '20 at 01:39
  • 1
    Ok, your client is created first. That won’t work because subsequent changes to config don’t modify existing client objects, only clients that you create after the config change. So, create your client object in the event handler, after modifying the config endpoint. – jarmod Mar 16 '20 at 01:50
  • Awesome!!! `{"statusCode":400,"message":"connect ECONNREFUSED 127.0.0.1:8042"}` Not totally working, but a step in the right direction. – RoflWaffle17 Mar 16 '20 at 02:02
  • @jarmod if you'd like to formally make your last comment an answer I will mark it as the suggested solution! – RoflWaffle17 Mar 16 '20 at 02:14
  • I’d try restarting the local server. Or see if this helps: https://stackoverflow.com/questions/33801460/dynamo-db-local-connection-refused – jarmod Mar 16 '20 at 02:18

1 Answers1

0

SDK configuration changes, such as region or endpoint, do not retroactively apply to existing clients (regular DynamoDB client or a document client).

So, change the configuration first and then create your client object. Or simply pass the configuration options into the client constructor.

jarmod
  • 71,565
  • 16
  • 115
  • 122