DynamoDB docClient scan does not respond

479 Views Asked by At

I use DynamoDB with nodeJS on a Lambda function using serverless. When I scan item from my local computer it works but when I deploy my function scan does not respond. No errors

const docClient = new AWS.DynamoDB.DocumentClient({
  apiVersion: "2012-08-10",
});

const checkApiKey = async (apiKey, ) => {
  try {
    log.debug("before scan");
    let result = await docClient
      .scan({
        "MY_TABLE",
        FilterExpression: "#apiKey = :apiKey",
        ExpressionAttributeNames: {
          "#apiKey": "apiKey",
        },
        ExpressionAttributeValues: { ":apiKey": apiKey },
      })
      .promise();
     log.debug("after scan");
  } catch (error) {
    log.error("Can not get dynamo object", { message: error.message });
    throwError(error);
  }
};

When I call this function on AWS, I can see in my log before scan but I don't see after scan nor error message from catch.

DynamoDB operations like "create" works fine.

I have been looking for a solution for several days ... Without success

2

There are 2 best solutions below

3
Seth Geoghegan On

I'm not sure if it's causing your problem, but the first thing that stuck out to me is how you are defining the table name in the call to scan:

 .scan({
        "MY_TABLE",
        ...

According to the docs, that should be a key/value pair

 .scan({
        TableName: "MY_TABLE",
        ...

If you are using the Serverless Framework, do you get different results if you run the function local vs remote?

For example, running the function locally from the command line:

sls invoke local --function <FUNCATION NAME from serverless.yml>

vs running the function remotely (in AWS) from the command line

sls invoke --function <FUNCATION NAME from serverless.yml>
6
elbik On

Please try to add the callbacks for the promise of await docClient.scan().promise()

I mean:

let result = await docClient
    .scan({
        TableName: "MY_TABLE",
        FilterExpression: "#apiKey = :apiKey",
        ExpressionAttributeNames: {
            "#apiKey": "apiKey",
        },
        ExpressionAttributeValues: { ":apiKey": apiKey },
    })
    .promise()
    .then(data => console.log)
    .catch(error => console.error)

and check the result, maybe then the picture became clear.