0

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 ?

Farouk M
  • 1,185
  • 7
  • 17
  • 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 Oct 24 '20 at 08:24
  • I don't use the docker CLI but the AWS CLI on powershell – Farouk M Oct 24 '20 at 08:29
  • 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 Oct 24 '20 at 08:31
  • I run it with serverless-offline plugin – Farouk M Oct 24 '20 at 08:34

1 Answers1

0

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.

Farouk M
  • 1,185
  • 7
  • 17