skip to Main Content

I am trying to develop a Lambda API to interact with DynamoDB, but I’m having issues with my local testing setup.

I’m using AWS SAM for development, but for some reason my local API server (using sam local start-api) can’t connect to the DynamoDB Local instance running inside Docker. I have been able to call the DynamoDB Local endpoint, http://localhost:8000, with the AWS CLI, however as I understand it due to the network bridge I need to use the container name for the endpoint.

Here’s the error I’m getting:

"errorMessage":"Inaccessible host: `ddb-local' at port `undefined'. This service may not be available in the `localhost' region.

Here’s the config I’m using:

  docClient = new dynamodb.DocumentClient({
    accessKeyId: "0600k",
    secretAccessKey: "k3hwkb",
    region: "localhost",
    endpoint: "http://ddb-local/:8000",
  });

And here’s the docker-compose.yml:

version: '3.8'
services:
  dynamodb-local:
    command: "-jar DynamoDBLocal.jar -sharedDb -optimizeDbBeforeStartup -dbPath ./data"
    image: "amazon/dynamodb-local:latest"
    container_name: ddb-local
    ports:
      - "8000:8000"
    volumes:
      - "./docker/dynamodb:/home/dynamodblocal/data"
    working_dir: /home/dynamodblocal

How can I successfully connect the two services?

3

Answers


  1. Chosen as BEST ANSWER

    For anyone else stuck with the same problem, I found my solution here: connecting AWS SAM Local with dynamodb in docker

    Essentially you need to set the Docker network for your SAM Local API to the same as your DynamoDB Local docker instance, and then set the endpoint to that of your DynamoDB Local container name. Here are the settings that worked for me:

    DynamoDB Document Client:

      docClient = new dynamodb.DocumentClient({
        accessKeyId: "0600k",
        secretAccessKey: "k3hwkb",
        region: "eu-west-2",
        endpoint: "http://ddb-local:8000",
      });
    

    DynamoDB Docker Compose file

    version: '3.8'
    services:
      dynamodb-local:
        command: "-jar DynamoDBLocal.jar -sharedDb -optimizeDbBeforeStartup -dbPath ./data"
        image: "amazon/dynamodb-local:latest"
        container_name: ddb-local
        ports:
          - "8000:8000"
        volumes:
          - "./docker/dynamodb:/home/dynamodblocal/data"
        working_dir: /home/dynamodblocal
        networks:
          dynamodb-local-network: {}
    
    networks:
      dynamodb-local-network:
        name: dynamodb-local-network
    

    Add the following flag to your start-api command:

    --docker-network dynamodb-local-network
    

  2. The Dynamodb local endpoint should be http://ddb-local:8000.

    Also, you may need to set a valid region name e.g us-west-2

    Login or Signup to reply.
  3. There is a problem with images built after "amazon/dynamodb-local:1.13.5"

    Just change this line:
    image: "amazon/dynamodb-local:latest" by
    image: amazon/dynamodb-local:1.13.5 in your Docker Compose file.

    Now you can check if http://localhost:8000/shell/ is running well.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search