Querying DynamoDB with Lambda does nothing

I had some similar issues and did not find many helpful resources. Here's what I ended up doing. Probably someone smarter can tell us if this is bestest.

function getHighScores(callback) {
    var params = {
        TableName : 'sessions',
        ScanFilter: {"score":{"AttributeValueList":[{"N":"0"}], "ComparisonOperator":"GT"}},
    };
    var dynamo = new AWS.DynamoDB();
    dynamo.scan(params, function(err, data) {
        if (err) {
            console.log (err)
            callback(err);
        } else {
            callback(data.Items);
            console.log(data.Items);
        }
    });
} 



getHighScores(function (data) {
    console.log(data);
    context.succeed(data);
});

In summary, having the passback of callback through the main function to the smaller function, allows the application to continue until completing the Dynamo. Keep the context.succeed in the secondary function or continue other function there.


My problem was that the function I was trying to call accepted a callback.

Node.js therefore just continued the execution of the Lambda function and once it reaches the end it tells Lambda to shut down because it's done.

Here's an example of how you can solve it using Promises:

'use strict';

exports.handler = async (event, context) => {

  // WRAP THE FUNCTION WHICH USES A CALLBACK IN A PROMISE AND RESOLVE IT, WHEN
  // THE CALLBACK FINISHES

  return await new Promise( (resolve, reject) => {

    // FUNCTION THAT USES A CALLBACK
    functionThatUsesACallback(params, (error, data) => {
      if(error) reject(error)
      if(data) resolve(data)
    });

  });

};

So, it turns out that the code is correct. The problem is that the dynamodb API uses all those callbacks and basically the function ends BEFORE the data has been retrieved.

The quickest fix is to remove the context.succeed() call and the data will be retrieved. Of course using the async module would help and if you don't want to use that, just add a counter or a boolean to your callback and then wait until the value has changed, indicating that the callback has been called (which kind of sucks if you think of it)


My problem is that my lambda was running in a VPC in order to connect to ElastiCache. This causes any queries to public Internet resources such as DynamoDB and API Gateway to hang indefinitely. I had to set up a NAT Gateway within my VPC in order to access DynamoDB.