How can I invoke a lambda function from another lambda and get the response data back into the original function to continue processing that data? I have the main lambda function (A) in Node.js, and the second lambda function (B) that is in python. Here is what I need:
- lambda A (node.js) invokes lambda B and passes arguments to it
- lambda B (python) processes the arguments and generates a response in JSON
- lambda A receives the response and does more processing
- lambda A generates a JSON return/response
Not to burden you with my code, here is a simple example of what I'm trying to do.
Lambda A
AWS.config.region = 'us-east-2';
var lambda = new AWS.Lambda();
var dataPayloadCopy = 'BLANK';
exports.handler = function(event, context){
var params = {
FunctionName: 'arn:aws:lambda:us-east-2:958569232013:function:lambdaB',
InvocationType: 'RequestResponse',
LogType: 'Tail',
Payload: '{ "name" : "Adam" }'
};
var lambdaResponse = lambda.invoke(params, function(err, data) {
if (err) {
console.log("error");
} else {
console.log("Response inside callback function: \n" + JSON.stringify(data.Payload));
//console.log(data.Payload);
dataPayloadCopy = data.Payload;
}
});
console.log(" ------ ");
console.log("Global response data copy: " + JSON.stringify(dataPayloadCopy));
console.log(" ------ ");
console.log("lambdaResponse data: ");
console.log(lambdaResponse.response.data);
console.log(" ------ ");
return {
'statusCode': 200,
'body': JSON.stringify({"message":"life is good"}),
'headers': {
'Content-Type': 'application/json; charset=utf-8',
'Access-Control-Allow-Origin': '*'
},
}
};
Lambda B
import json
def lambda_handler(event,context):
responseMessage = {"message":"Arguments received: name: " + event['name']}
return {
'statusCode': 200,
'body': json.dumps(responseMessage)
}
Steps 1 and 2 work with no problem. I can't accomplish step 3. I see the lambda B response in the data.Payload in the lambda.invoke() callback function and can print it out there, but how can I get that value into the main lambda A function so I can work with it there? I tried to copy data.Payload into a global variable dataPayloadCopy, and also assign lambda.invoke() value to lambdaResponse variable (in code above). Neither way worked. Looks like the lambda B executes when lambda A finishes.
Here is the execution result:
INFO ------
INFO Global response data copy: "BLANK"
INFO ------
INFO lambdaResponse data:
INFO null
INFO ------
INFO Response inside callback function:
"{\"statusCode\": 200, \"body\": \"{\\\"message\\\": \\\"Arguments received: name: Adam\\\"}\"}"
Also, I don't see the lambda A return value in the test results. If I change the handler function in lambda A to be asynchronous exports.handler = async (event) => { and run a test, lambda A return value shows, but there is no printout from the callback function appears as if lambda B was not invoked and/or the return was not received.
I did not have any problem with a similar task if both lambdas are in python. My lambda A code is in Node.js though.