1

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

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

3 Comments

thanks. but it giving me below error ERROR Error: Uncaught (in promise): HttpErrorResponse: {"headers":{"normalizedNames
504 OK","error":{"errorMessage":"2020-02-07T20:44:42.421Z d4d05a74-fd9b-42a2-9d60-ffb3f04ba8a2 Task timed out after 10.01 seconds"
any way after i change 10 sec to 5 sec it start working. thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.