Reputation: 219
I've trying to call lambda function from another lambda function and get result to execute rest of the lambda.
Basic flow of function is below
X - main lambda function
- process A (independent)
- process C (need input from process B)
- process D
- return final dataset
Y - Child lambda function
- process B ( need input from process A and respond back to X )
This is my code so far
var AWS = require('aws-sdk');
AWS.config.region = 'us-east-1';
var lambda = new AWS.Lambda();
const GetUserCheckoutData: Handler = async (userRequest: EmptyProjectRequest, context: Context, callback: Callback) => {
const dboperation = new UserController();
const usercheckoutdata = new CheckOutInfo();
const addresscontroller = new AddressController();
const ordercontroller = new OrderController();
const paypalcreateorder = new PayPalController();
const userid = await dboperation.getUserID(userRequest.invokeemailAddress);
usercheckoutdata.useraddressdetails = await addresscontroller.GetListOfAddressByUserID(userid);
var orderlist = new Array<Order>();
orderlist = [];
orderlist = await ordercontroller.GetCurrentOrder(userid);
console.log("Order Complete");
var params = {
FunctionName: 'api-ENGG-SellItem', // the lambda function we are going to invoke
InvocationType: 'RequestResponse',
LogType: 'Tail',
Payload: '{ "orderlist" : xxxxxxx }'
};
lambda.invoke(params, (err:any, res:any) => {
if (err) {
callback(err);
}
console.log(JSON.stringify(res));
callback(null, res.Payload);
});
usercheckoutdata.orderID = await paypalcreateorder.CreateOrder(userid , orderlist);
usercheckoutdata.orderPreview = await ordercontroller.OrderPreview(userid);
//callback(null,usercheckoutdata);
};
export { GetUserCheckoutData }
I tried a few different ways but flow is not working properly. cross lambda function is executing. but cannot get the response on time.
My child lambda function demo code
import { Handler, Context } from "aws-lambda";
const SellItem: Handler = (event, context, callback) => {
context.callbackWaitsForEmptyEventLoop = false;
console.log("Other Lambda Function");
setTimeout(() => {
callback(null, "My name is Jonathan");
}, 1000 * 10); // 10 seconds delay
}
export {SellItem}
I think since I don't have much NodeJS knowledge this is happening. don't know how to put call back in right way I guess. Any help will be appreciated
Upvotes: 1
Views: 1162
Reputation: 8887
You should make your call to the second lambda a promise, so you can await it.
const res = await lambda.invoke(params).promise();
// do things with the response
Upvotes: 2