0
votes

I want to train on Serverless with a DynamoDB local docker. However, with this code I get an empty list of tables :

import { DynamoDB, Endpoint } from "aws-sdk";

export const handler = async (event, context, cb) => {
   const ddb = new DynamoDB();

   if (process.env["DYNAMO_LOCAL_ENDPT"]) {
     ddb.endpoint = new Endpoint(process.env["DYNAMO_LOCAL_ENDPT"]);
    // the local env variable = http://localhost:8000
   }

   const tables = await ddb.listTables().promise();

  const response = {
    statusCode: 200,
    body: JSON.stringify({
    message: "Hello World!",
    tables,
    }),
  };
  return response;
};

But if I do the same using AWS CLI on Powershell with the command,

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

I get the right response with my 2 tables on my docker.

Can you tell me what's wrong with my code ?

1
I think that when you test in docker, http://localhost:8000 will refer to localhost in the docker, not your host. When you use aws cli, you are using localhost in your host operating system, not in the docker container. - Marcin
I don't use the docker CLI but the AWS CLI on powershell - F_Mekk
So how do you run your lambda code? Also, have you verified that the regions are same when you use AWS CLI and when you run your function? - Marcin
I run it with serverless-offline plugin - F_Mekk

1 Answers

0
votes

The DynamoDB docker region is the same that the one configured in your AWS CLI.

The region in your Serverless Yaml has to be the same (by default it's us-east-1) even if you work locally.