How to call an AWS Python Lambda function from another Node.js Lambda function

Viewed 384

I'm new to JavaScript and AWS. And I'm struggling with connecting a Python AWS Lambda function to a Node.js AWS Lambda function.

I want to invoke a separate Lambda function (Let's say child lambda) from a middle line of the main Lambda function (Let's say parent lambda) and use the returned value from the child lambda in the parent lambda.

My child lambda is implemented in Python 3.6 and parent lambda is implemented in Node.js 12.x. I want to stop executing the parent lambda until the child lambda returns it's value.

But it seems like the rest of the code lines in the parent lambda execute before finishing the execution of the child lambda.

I set up the required policy for parent lambda using the below link.
https://www.sqlshack.com/calling-an-aws-lambda-function-from-another-lambda-function/

I have an array assigned to variable 'img'. If a condition satisfies I need to invoke the child lambda by passing the Payload as 'img' array to do some extra calculations on that array and return it back to the parent lambda. And finally replace the 'img' variable with the returned array.

Below is the code block I used in the parent lambda to invoke the child lambda. Please note that "returnArr" is the returned array from the child lambda.

const AWS = require('aws-sdk');
AWS.config.region = 'ap-southeast-2';
var lambda = new AWS.Lambda();

exports.handler = async (event, ctx, callback) => {
    //////////code lines for other operations////////////

    let img = [[[2,3,5],[6,7,8]],[[12,13,15],[16,17,18]]];
    let flag=0;
    if(condition){
        console.log("BEGIN");
        var params = {
                FunctionName: 'childFunction', // child lambda function written in Pyton 3.6
                InvocationType: 'RequestResponse',
                Payload: JSON.stringify({ "sendImg" : img})
            };
            lambda.invoke(params, function(err, data) {
                console.log("SENT");
                if (err) {
                console.log(err);
                } else {
                //console.log('Lambda_B said '+ data.Payload);
                let body1;
                flag = 1;
                if(typeof(data.Payload) == 'object') {
                    body1 = data.Payload;
                } else {
                    body1 = JSON.parse(data.Payload);
                }
                img = body1["returnArray"];
                console.log("Modified img array inside: ",img);
                }
            })
            console.log("DONE");
    }

    console.log("flag : "+flag);
    console.log("Modified img array outside: "+img);
    ////////Rest of the code///////////////////////
};

I get the output as follows:

BEGIN
DONE
flag : 0
Modified img array outside: [[[2,3,5],[6,7,8]],[[12,13,15],[16,17,18]]]
SENT
Modified img array inside: [[[23,31,54],[63,71,86]],[[12,133,115],[162,117,18]]]


According to the output you can see that the 2 code lines outside the if condition have executed before the child lambda return the modified array.
'img' array outside if condition is not modified but 'img' array inside the if condition is modified.

I want to stop executing the parent lambda until the child lambda returns it's array.

I think this is happening since I don't have much NodeJS knowledge.
Could someone kindly tell me where I went wrong?

1 Answers

the parent lambda execute before finishing the execution of the child lambda

You are using async handler, so your function finishes before it has a chance to invoke the other function.

To overcome this you can wrap your code in the handler in new Promise as shown in the AWS docs.

Related