Call aws lambda from another lambda asynchronously

I need to call aws lambda from another lambda asynchronously. I have a working code for synchronous calls.

exports.handler = (event, context, callback) => { var aws = require('aws-sdk'); var lambda = new aws.Lambda({ region: 'myregion' //change to your region }); console.log("lambda invoke started"); lambda.invoke({ FunctionName: 'testLambda', Payload: JSON.stringify(event, null, 2) // pass params }, function (error, data) { if (error) { console.log("error"); callback(null, 'hello world'); } else { console.log("lambda invoke end"); callback(null, 'hello world'); } }); } 

But in my case, "testLambda" is a function of time. Because I need to exit immediately after calling the testLambda function. Then the code is updated as follows

 exports.handler = (event, context, callback) => { var aws = require('aws-sdk'); var lambda = new aws.Lambda({ region: 'myregion' //change to your region }); console.log("lambda invoke started"); lambda.invoke({ FunctionName: 'testLambda', Payload: JSON.stringify(event, null, 2) // pass params }); console.log("lambda invoke end"); callback(null, 'hello world'); } 

it returns the message correctly. But my testLambda function is not called (no cloud observation logs are created for the test lambda). what is the problem with this code.

+8
asynchronous amazon-web-services aws-lambda asynccallback
source share
2 answers

In the documentation for RequestResponse Lambda (), you will see that by default, the Lambda function is called using the RequestResponse call to RequestResponse . For an asynchronous function call, you must specify the type of the Event call, for example:

 lambda.invoke({ FunctionName: 'testLambda', InvocationType: 'Event', Payload: JSON.stringify(event, null, 2) },function(err,data){}); 
+6
source share

I worked with the latest version of node.js 8.10 on AWS Lambda.
The second lambda did not execute (and the callback function was never called) until I used the async / await mechanism.
Thus, the handler function must be asynchronous, and the call to 'lambda.invoke' must be enclosed in Promise .

here is my working code:

 function invokeLambda2(payload) { const params = { FunctionName: 'TestLambda2', InvocationType: 'Event', Payload: JSON.stringify(payload) }; return new Promise((resolve, reject) => { lambda.invoke(params, (err,data) => { if (err) { console.log(err, err.stack); reject(err); } else { console.log(data); resolve(data); } }); }); } exports.handler = async (event, context) => { const payload = { 'message': 'hello from lambda1' }; await invokeLambda2(payload); context.done(); }; 

Please note that the handler does not expect the second lambda to exit, only to start it and call the callback function.

You can also return Promise from the handler, no need to use await with the second function.

There is no need to import when working with Promises and async / await, except:

 const AWS = require('aws-sdk'); const lambda = new AWS.Lambda(); 
0
source share

All Articles