6

I think this is possible? I have a lambda and api gateway defined in a sam template. I use sam-local to start that up. Within my lambda I would like to connect to my local dynamoDB but the lambda keeps timing out. Code looks like:

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

function handler(event, context, callback) {
  dyn.listTables({Limit: 10}, function(err, data) {
    if (err) {
      console.log("Error", err.code)
    } else {
      console.log("Table names are ", data.TableNames)
    }
  })

  let response = {
    statusCode: 200
  }
  callback(null, response)
}

If this code is run outside of a lambda it works fine

4
  • Try adding some more debug lines and set the lambda timeout to 300 seconds (the maximum). You should be able to narrow if down to one line. Commented Jan 19, 2018 at 20:38
  • I get the following error: { Error: connect ECONNREFUSED 127.0.0.1:8000 at Object.exports._errnoException (util.js:1018:11) at exports._exceptionWithHostPort (util.js:1041:20) at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1086:14) message: 'connect ECONNREFUSED 127.0.0.1:8000', code: 'NetworkingError', errno: 'ECONNREFUSED', syscall: 'connect', address: '127.0.0.1', port: 8000, region: 'us-east-1', hostname: 'localhost', retryable: true, time: 2018-01-22T08:54:57.175Z } Commented Jan 22, 2018 at 9:00
  • Sounds like DynamoDB isn't running, or is running on a different port. How are you starting DynamoDB local? Commented Jan 22, 2018 at 9:20
  • java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb which gives: Initializing DynamoDB Local with the following configuration: Port: 8000 InMemory: false DbPath: null SharedDb: true shouldDelayTransientStatuses: false CorsParams: * Commented Jan 22, 2018 at 17:10

4 Answers 4

10

I'm doing the same thing as you. But I run locally my DynamoDB as a docker image using this command. I run this on mac:

docker run -p 8000:8000 amazon/dynamodb-local

In your code change this:

endpoint: new AWS.Endpoint("http://localhost:8000")

to this:

endpoint: new AWS.Endpoint("http://docker.for.mac.localhost:8000")

Now lambda can connect to port and will not timed out.

Sign up to request clarification or add additional context in comments.

1 Comment

So changing the endpoint to the above worked just fine for me, but I'm curious how to I ensure that the container I build my SAM service in is on the same network as the dynamodb container? I'd rather just be able to set the endpoint to http://localhost:8000 and not have to worry about it... Any input would be greatly appreciated.
8

Your DynamoDB is running on the local machine, while the SAM Local is running inside a Docker container.

If you create a Docker container for DynamoDB to run in, and have this in the same Docker network as the SAM Local container, you might have more success.

1 Comment

Can you elaborate as to how to get the SAM local container and the DynamoDB container to run on the same network?
0

you can run DynamoDB locally inside a container, but I wonder how to call it from the SAM's Lambda container (local as well)

docker run -p 8000:8000 amazon/dynamodb-local

Comments

0

For anyone that may wander here how to achieve a connection with dynamodb and lambda, both running in a docker container. Started as a comment but wanted to provide examples.

  • Option 1: use docker compose with both containers as services in docker-compose.yaml file

  • Option 2: with standalone containers, manually create a local docker bridge network and connect both containers to it, here the steps to reproduce:

Create the network

sudo docker network create -d bridge [dynamodb_net]

Start the dynamo container, give it a name so your lambda will be able to to find it

sudo docker run --rm -it --network dynamodb_net -p 8000:8000 --name [dynamo-local] [dynamo-image]

Start your lambda container in the same fashion, you can add name to this container if you want to

sudo docker run --rm -it --network dynamodb_net -p 9000:8080 [lambda-image]

NOTE: the items in [here] can be changed if you need to

EDIT: added ports flag

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.