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?
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 randocker network ls
to find my dockercompose NETWORK ID, then randocker 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:
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:All solved