0

I want to create a API endpoint in Node.js that lists the table names in DynamoDB. I have created a simple table locally and confirmed the table exists with the command

aws dynamodb list-tables --endpoint-url http://host.docker.internal:8000

{
    "TableNames": [
        "SampleTable"
    ]
}

but my lambda returns

{"TableNames":[]}

here's my lambda

const AWS = require('aws-sdk');

const options = {
  apiVersion: '2012-08-10',
  region: 'us-east-1',
};

if (process.env.AWS_SAM_LOCAL) {
  options.endpoint = new AWS.Endpoint('http://host.docker.internal:8000');
}

const ddb = new AWS.DynamoDB(options);

exports.listTablesHandler = async (event) => {
  if (event.httpMethod !== 'GET') {
    throw new Error(`listTables only accept GET method, you tried: ${event.httpMethod}`);
  }
  console.info('received:', event);

  const params = {};
  let response = { statusCode: '500' };
  try {
    response = await ddb.listTables(params).promise();
  } catch (err) {
    console.log(err);
  }

  return response;
};

I expected the Lambda to list the name "SampleTable" in the API response

atAtlas
  • 11
  • 3

1 Answers1

0

You should check if you are using -sharedDb ?

If you use the -sharedDb option, DynamoDB creates a single database file named shared-local-instance.db. Every program that connects to DynamoDB accesses this file. If you delete the file, you lose any data that you have stored in it.

If you omit -sharedDb, the database file is named myaccesskeyid_region.db, with the AWS access key ID and AWS Region as they appear in your application configuration. If you delete the file, you lose any data that you have stored in it.

Leeroy Hannigan
  • 11,409
  • 3
  • 14
  • 31