I have the following code for a Lambda function:
console.log(\'Loading function\');
var aws = require(\'aws-sdk\');
var ddb = new aws.DynamoDB();
function getUs
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)
});
});
};