Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DynamoDB query function is not working while trying to access the data

So I tried to fetch data from dynamodb, using the below code, I am getting null and no log is showing after the query function is called.

I tried it locally, it is working locally, only through lambda function it is not working, I also checked if my role has the permission to read dynamodb, it does. So I am clueless on this

"use strict";

const config = require("./config");
const AWS = require("aws-sdk");

AWS.config.update({
  region: "us-east-1"
});
let dynamodb = new AWS.DynamoDB.DocumentClient({
  region: "us-east-1"
});

var getAnswerToQuestion = (questionKey, callback) => {

  var params = {
    TableName: "genral-questions-db",
    KeyConditionExpression: "#questionKey = :question",
    ExpressionAttributeNames: {
      "#questionKey": "question"
    },
    ExpressionAttributeValues: {
      ":question": String(questionKey)
    }
  };
  console.log("Trying to query dynamodb");

  dynamodb.query(params, (err, data) => {
    if(err) {
      console.log (err)
      callback(err);
    } else {
      console.log(data.Items);
      callback(data.Items[0]);
    }
  });
}

module.exports = {
  getAnswerToQuestion
};

logs:

Function Logs:
START RequestId: 6adeddbe-4925-4877-a2c0-d20576145224 Version: $LATEST
2019-09-22T17:35:30.088Z    6adeddbe-4925-4877-a2c0-d20576145224    event.bot.name=bcbsri
2019-09-22T17:35:30.088Z    6adeddbe-4925-4877-a2c0-d20576145224    dispatch userId=pn6yexoq87uej2evt9huhilc5f99bhb7, intentName=GenralQuestionIntent
2019-09-22T17:35:30.088Z    6adeddbe-4925-4877-a2c0-d20576145224    GenralQuestionIntent was called - Srinivas
2019-09-22T17:35:30.089Z    6adeddbe-4925-4877-a2c0-d20576145224    have to query the db hsa
2019-09-22T17:35:30.089Z    6adeddbe-4925-4877-a2c0-d20576145224    Trying to query dynamodb
END RequestId: 6adeddbe-4925-4877-a2c0-d20576145224
REPORT RequestId: 6adeddbe-4925-4877-a2c0-d20576145224  Duration: 596.83 ms Billed Duration: 600 ms Memory Size: 128 MB Max Memory Used: 76 MB  Init Duration: 193.27 ms    

not even getting error, it just does not give back any data. Please help me to resolve this issue

EDIT: here is the code from where I am trying to call the utils method(db)

module.exports =  function(intentRequest) { 
    return new Promise((resolve, reject) => {

        // Resolve question key
        const questionKey = intentRequest.currentIntent.slots.QuestionKey;

        let speechText;

        console.log('have to query the db', questionKey);

        utils.getAnswerToQuestion(questionKey, res => {
            if(res ) {
                speechText = res.answer;
            } else {
                speechText = "Sorry, details about " + questionKey + " was not found";
            }

            const response = {
                fullfilmentState: 'Fulfilled',
                message: { contentType: 'PlainText', content: speechText }
            };
            console.log(response);

            resolve(response);
            return lexResponses.close(intentRequest.sessionAttributes, response.fullfilmentState, response.message);
        });
    });
};
like image 590
Srini Jagadeesh Avatar asked Mar 04 '23 09:03

Srini Jagadeesh


1 Answers

I would bet that this is the most common problem that people see when using Node.js with Lambda.

When a Node.js Lambda reaches the end of the main thread, it ends all other threads. When it reaches the end of the handler, it stops all concurrent promises or async calls that are running.

To make sure that the lambda does not prematurely terminate those threads, wait until those promises are complete by using await.

In your case, use the .promise() method with any AWS requests and then await them:

try {
    const data = await dynamodb.query(params).promise();
    console.log(data.Items);
    callback(data.Items[0]);
}  catch (err) {
    console.log(err);
    callback(err);
}
like image 155
Joey Kilpatrick Avatar answered Apr 07 '23 16:04

Joey Kilpatrick