title: How to call an AWS Python Lambda function from another Node.js Lambda function
body: <p>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.</p>
  <p>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.</p>
  <p>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.</p>
  <p>But it seems like the rest of the code lines in the parent lambda execute before finishing the execution of the child lambda.</p>
  <p>I set up the required policy for parent lambda using the below link.<br />
  <a href="https://www.sqlshack.com/calling-an-aws-lambda-function-from-another-lambda-function/" rel="nofollow noreferrer">https://www.sqlshack.com/calling-an-aws-lambda-function-from-another-lambda-function/</a></p>
  <p>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.</p>
  <p>Below is the code block I used in the parent lambda to invoke the child lambda.
  Please note that &quot;returnArr&quot; is the returned array from the child lambda.</p>
  <pre><code>const AWS = require('aws-sdk');
  AWS.config.region = 'ap-southeast-2';
  var lambda = new AWS.Lambda();
  
  exports.handler = async (event, ctx, callback) =&gt; {
      //////////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(&quot;BEGIN&quot;);
          var params = {
                  FunctionName: 'childFunction', // child lambda function written in Pyton 3.6
                  InvocationType: 'RequestResponse',
                  Payload: JSON.stringify({ &quot;sendImg&quot; : img})
              };
              lambda.invoke(params, function(err, data) {
                  console.log(&quot;SENT&quot;);
                  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[&quot;returnArray&quot;];
                  console.log(&quot;Modified img array inside: &quot;,img);
                  }
              })
              console.log(&quot;DONE&quot;);
      }
  
      console.log(&quot;flag : &quot;+flag);
      console.log(&quot;Modified img array outside: &quot;+img);
      ////////Rest of the code///////////////////////
  };
  </code></pre>
  <p>I get the output as follows:</p>
  <p>BEGIN <br />
  DONE <br />
  flag : 0 <br />
  Modified img array outside: [[[2,3,5],[6,7,8]],[[12,13,15],[16,17,18]]]<br />
  SENT<br />
  Modified img array inside: [[[23,31,54],[63,71,86]],[[12,133,115],[162,117,18]]]<br /></p>
  <br />
  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.<br />
  'img' array outside if condition is not modified but 'img' array inside the if condition is modified.
  <p>I want to stop executing the parent lambda until the child lambda returns it's array.</p>
  <p>I think this is happening since I don't have much NodeJS knowledge.<br />
  Could someone kindly tell me where I went wrong?</p>