在 lambda 函数中进行 AWS NodeJS lambda 调用



我试图从另一个lambda函数调用lambda函数,并获得执行其余lambda的结果。

基本功能流程如下

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 )

这是我到目前为止的代码

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 }

我尝试了几种不同的方法,但流程无法正常工作。 交叉 lambda 函数正在执行。 但无法按时得到响应。

我的子 lambda 函数演示代码

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} 

我认为由于我没有太多的 NodeJS 知识,这种情况正在发生。 我不知道如何以正确的方式回拨我猜。任何帮助将不胜感激

您应该将调用第二个 lambda 作为承诺,以便您可以等待它。

const res = await lambda.invoke(params).promise();
// do things with the response

最新更新