Lambda function ends after call to dynamoDB using

2019-08-16 18:06发布

问题:

I have a Lambda function reaching out to DynamoDB, utilizing promises. I get no errors, but the function doesn't move past the call to Dynamo. Similar questions I have found seemed to be resolved by using promises, so I feel this is a new question.

I have tried using promises with then statements to wait on DynamoDB to return data. I receive no errors, but my last log statement is right before the call to dynamo.

Here is my dynamo helper function:

let AWS = require("aws-sdk");
let dynamo = new AWS.DynamoDB.DocumentClient({
  region: 'us-east-1'
});
//let dynamo = new AWS.DynamoDB() //This may get rid of the execption but gotta make it work. Think I have to wrap the data witht the types in dynamo
//let dynamoPromise = dynamo.promise()
let dynamoHelper = function() {
  return {
    putObject: function(tableName, object) { //,callback I took callback out of the params
      let params = {
        "TableName": tableName,
        "Item": object
      };

      let dynamoPromise = dynamo.put(params).promise()

      return dynamoPromise;
    },
    getRoom: function(tableName, deviceId) { //,callback I took callback out of the params
      console.log("inside the dynamoHelper.getRoom function")
      let params = {
        "TableName": tableName,
        "Key": {
          "deviceId": deviceId
        },
        "AttributesToGet": [
          "roomNumber"
        ]
      };
      let dynamoPromise = dynamo.get(params).promise() //? 
      console.log("After the get call to dynamo still inside dynamo helper")
      return dynamoPromise;
    }
  }
}();
module.exports = dynamoHelper;

Here is where I call it in index.js:

let dynamoGetPromise = dynamoHelper.getRoom(config.room_lookup_dynamodb_table, deviceId) //took the callback out of the args and put it in then. Will have to do the same for the one that is contained inside the then.
    dynamoGetPromise
      .then(
        function(data) {
          console.log("Data: " + JSON.stringify(data));
          if ('Item' in data) {
            roomNumber = data.Item.roomNumber;
          } else {
            roomNumber = 9999;
          }

...........

That console.log("Data: " + JSON.stringify(data)); statement is never reached, but -again- no errors. Shouldn't be a timeout issue as my Lambda output says billed duration is only 700ms...

Here is my lambda output:

START RequestId: e57c0e7a-bdf2-4e20-869b-44babf834dfd Version: $LATEST 2019-04-04T14:30:52.075Z e57c0e7a-bdf2-4e20-869b-44babf834dfd Warning: Application ID is not set 2019-04-04T14:30:52.193Z e57c0e7a-bdf2-4e20-869b-44babf834dfd { canHandle: [Function: canHandle], handle: [Function: handle] } 2019-04-04T14:30:52.254Z e57c0e7a-bdf2-4e20-869b-44babf834dfd ||| New Session ||| 2019-04-04T14:30:52.254Z e57c0e7a-bdf2-4e20-869b-44babf834dfd Device ID: amzn1.ask.device.AEH2LHYGV7GSPP5THMR5H56AI2OOMAQ7MF54CZ3E6WR433WGS6QAOCYCKJWRJ3TQY5IE76NWR2IKCANB6TJNKLDEZOO2YN6ACUVT33MKSS4CO6R7GJI6GDFLOBOPUA2IXX7RI732UXJ6PDST5KYC7CSQK634K4APEBRNVOKVZIDECOCBBIFB4 2019-04-04T14:30:52.254Z e57c0e7a-bdf2-4e20-869b-44babf834dfd New Intent Time: 2019-04-04T10:30:52-04:00 2019-04-04T14:30:52.254Z e57c0e7a-bdf2-4e20-869b-44babf834dfd inside the dynamoHelper.getRoom function END RequestId: e57c0e7a-bdf2-4e20-869b-44babf834dfd REPORT RequestId: e57c0e7a-bdf2-4e20-869b-44babf834dfd Duration: 693.31 ms Billed Duration: 700 ms Memory Size: 128 MB Max Memory Used: 74 MB

Thanks for any help!

Update:

Added a catch statement directly to the dynamo.get call, and still no more info:

let dynamoPromise = dynamo.get(params).promise().catch(function(reason){console.log("Reason: " + reason)});

Update2:

Tried getting rid of the IIFE, and turning them into async functions held in an object. Same results.

Tried @alex067's suggestion below, but same results.

Update3:

I have solved ~90% of my issue using async/await.

回答1:

I used async and await and this solved the issue of the function not executing all the way. I still have an issue, but I will post a new question for that if necessary.