23 February 2020
Setup the initial SAM app using the SAM cli
sam init
When I did this I used the nodejs10.x quick start and selected the web backend. This gives you an app with 3 lambda functions
and a single dynamodb table
I am also assuming you are running on some brand of Linux.
In order to get the lambdas to be able to talk to the local dynamodb, we need to put them both in the same docker network. Then update the endpoint used by the dynamodb client in the lambdas. The minimal steps required to do this are
docker network create sam-demo
--network
option. Also give it a useful name like dynamo, this name will be used for networking to it from the lambdas.
docker run -d --name dynamo \
-p 8000:8000 --network sam-demo \
amazon/dynamodb-local
Then you can create your table in dynamo
aws dynamodb create-table \
--table-name SampleTable \
--attribute-definitions \
AttributeName=id,AttributeType=S \
--key-schema AttributeName=id,KeyType=HASH \
--provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1 \
--endpoint-url http://localhost:8000
const options = { endpoint: "http://dynamo:8000" };
const docClient = new dynamodb.DocumentClient(options);
Note: if you are running on Mac or Windows then I think the endpoint url will be slightly different here.
--docker-network
option.
sam local invoke getAllItemsFunction \
-e events/event-get-all-items.json \
--docker-network sam-demo
The key points here are putting the dynamodb local and the sam lambdas or api on the same docker network. Outside docker you can connect to dynamo using 127.0.0.1:8000 or localhost:8000, but within docker space you need to refer to it using the name you gave it, eg dynamo.