sam local invoke function timeouts when connecting to dynamodb docker compose

1.3k views Asked by At

I am trying to connect a local sam app (hello world example) to a dynamodb docker container running with docker compose. When I try to execute the lambda functions locally with python3 everything works perfect, I am able to write and delete data from tables, but when I try to invoke the same function with

sam local invoke ReadTableFunction --event events/event_read.json --debug

I get this timeout

Connect timeout on endpoint URL: \"http://localhost:8000/

I am able to list my tables with the aws cli at localhost

aws dynamodb list-tables --endpoint-url http://localhost:8000 

{ "TableNames": [ "table_redirections" ] }

I somewhere read that when you refer to localhost inside a docker container you are referring to the actual localhost of the container, not the localhost from your PC, so I think that the problem might be something about connections.

This is my docker compose:

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

How can I make SAM local invoke function be able to talk or recognize the docker containers IP from my dockercompose dynamodb at localhost?

1

There are 1 answers

0
Marc Albrand On BEST ANSWER

Well after some time of research I managed to make the connection with both of the containers.

I decided to set a static IP address to my dynamodb container to prevent having to update each time the IP address in the code. First I ran the command docker-compose up with my file and after that I ran docker network ls to find my dockercompose NETWORK ID, then ran docker network inspect 'MY_NETWORK_ID' to find the current ipv4 address that the db container was using.

Now you shutdown your docker compose with docker-compose down

Once i had the IP address I had to modify a little bit my docker compose like this:

version: '3.7'
services:
  dynamodb-local:
    command: "-jar DynamoDBLocal.jar -sharedDb -dbPath ./data"
    image: "amazon/dynamodb-local:latest"
    container_name: dynamodb-local
    ports:
      - "8000:8000"
    volumes:
      - "./docker/dynamodb:/home/dynamodblocal/data"
    working_dir: /home/dynamodblocal
    hostname: dynamodb
    networks:
      dynamodbNetwork:
        ipv4_address: 172.22.0.2
        aliases:
        - dynamodblocal


networks:
  dynamodbNetwork:
    name: dynamo_net
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: "172.22.0.2/16"

What is happening here is that we are creating a custom subnet for the docker compose with a static IPv4.

Now we can do docker-compose up and invoke our functions with our custom subnet included like this:

sam local invoke ReadTableFunction --event events/event_read.json --debug --docker-network dynamo_net

All solved