Search code examples
amazon-web-servicesaws-lambdaamazon-dynamodbdocumentclient

AWS Lambda with DynamoDb no results


I am just starting with AWS and can't get the DynamoDB running at all.

I followed a tutorial and created all AWS elements and set priviledge for the DynamoDB in the lambda basic profile.

I am wondering why I do not get any results from the DB or any error messages. I put some console logs in the code to troubleshoot:

var AWS = require("aws-sdk");
AWS.config.update({
    region: "eu-west-1",
});

var docClient = new AWS.DynamoDB.DocumentClient();
var params = {
    TableName: "cooking_table",
    Key:{
        "data_type": "meal"
    }
};


console.log("Scanning table.");
docClient.scan(params, onScan);
console.log("scan done");

function onScan(err, data) {
    console.log("starting to scan");
    if (err) {
        console.error("Unable to scan the table. Error JSON:", 
 JSON.stringify(err, null, 2));
    } else {
        // print all the movies
        console.log("Scan succeeded.");
        data.Items.forEach(function(movie) {
           console.log(movie.data_type);
        });

        // continue scanning if we have more movies, because
        // scan can retrieve a maximum of 1MB of data
        if (typeof data.LastEvaluatedKey != "undefined") {
             console.log("Scanning for more...");
             params.ExclusiveStartKey = data.LastEvaluatedKey;
             docClient.scan(params, onScan);
        }
    }
}

Surprisingly, I do not get any console log entries inside of the function onScan.

In the log I see only the output of those lines:

console.log("Scanning table.");
console.log("scan done");

but no errors.

I don't see the big mistake I am doing.

What is going wrong? Thanks.


Solution

  • Scan is asynchronous action which call callback when completed so your problem is that your lambda is ending before that action is completed that's probably why you see only those 2 logs without any error/problems or result.

    I would suggest to look on Promise as possible solution of that problem.