How to use DynamoDB locally with Lambda
Asked Answered
S

4

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

Stockbroker answered 19/1, 2018 at 14:7 Comment(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.Incalescent
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 }Stockbroker
Sounds like DynamoDB isn't running, or is running on a different port. How are you starting DynamoDB local?Incalescent
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: *Stockbroker
M
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.

Mook answered 8/2, 2018 at 11:1 Comment(1)
Can you elaborate as to how to get the SAM local container and the DynamoDB container to run on the same network?Lakes
R
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.

Repentance answered 10/4, 2019 at 14:33 Comment(1)
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.Lakes
M
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.

Mook answered 8/2, 2018 at 11:1 Comment(1)
Can you elaborate as to how to get the SAM local container and the DynamoDB container to run on the same network?Lakes
D
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

Darkness answered 4/3, 2019 at 17:55 Comment(0)
G
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

Gavin answered 15/5, 2023 at 19:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.