-5

I'm running a NodeJS lambda function which is triggered by API Gateway.

My goal is to push the data and then send a status response. I think the lambda stops running before the insertData function finishes its execution, because sometimes it works but in most requests it doesn't.

Could someone lend a hand on this?

Here is my code:


// Set a table name that we can use later on
const tableName = "InterestRates"

// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
// Set the region 
AWS.config.update({region: 'us-east-1'});

// Create the DynamoDB service object
var ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
 
exports.handler = async (event) => {
    // TODO implement
    console.log(">>> Running LoanInterestRates")
    
    if(event['resource'] == '/rates'){
        if(event.httpMethod === 'POST'){
            return newRate(event);
        }
    }     
}; 

function insertData(params){
    let status;
    // Call DynamoDB to add the item to the table
    ddb.putItem(params, function(err, data) {
      if (err) {
        status = "Error";
      } else {
        status = "Success";
      } 
    });  
    return status
}

function newRate (event){ 
    const newRate = JSON.parse(event.body);
    var params = {
      TableName: 'InterestRates',
      Item: {
        'termlength' : {N: newRate["length"]},
        'InterestRate': {S: newRate["rate"]}
      }
    }; 
    
     let addNewRate = insertData(params);
     
    
     return  {
        statusCode: 200,
        body: JSON.stringify({ 
            response: addNewRate 
        })
   }
}

I also tried using Async/Await but it didn't work.

5
  • check the answer stackoverflow.com/questions/40192304/… Commented Dec 6, 2022 at 13:18
  • I think the lambda stops running before the insertData function finishes... What makes you think this? What do the logs say? Have you added logging to verify? Are there any errors? Have you verified that the lambda has not timed out? Commented Dec 6, 2022 at 13:19
  • @MetroSmurf yep! I did verify the logs and it's not reporting any error... Commented Dec 6, 2022 at 13:45
  • @OlegUshakov everything is checked and it even works sometimes... Each 5 requests I make, 1 works, the other 4 returns nothing, the "addNewRate" var comes empty. Commented Dec 6, 2022 at 13:48
  • Minor: all your var declarations should be const.
    – jarmod
    Commented Dec 6, 2022 at 16:15

1 Answer 1

2

You lambda function is Async but your code is not. You need to await the completion of your function newRate which in turn should also await the function inserData which should also await your DDB request.

I would advise you to do one of two things:

  1. Learn how JS Async nature works and ensure you understand when you need to await.
  2. Use a synchronous programming language like Python/Boto3 where you will not run into such issues.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.