I need to invoke aws lambda from another lambda asynchronously. i have a working code for synchronous calls.
exports.handler = (event, context, callback) =&g
I wanted a similar solution as above. Though step functions are now recommended when using multiple lambda functions over lambda.invoke
, I used the following code snippet to asynchronously call two other lambda functions from my base lambda function.
var AWS = require('aws-sdk');
AWS.config.region = 'ap-southeast-1';
var lambda = new AWS.Lambda();
exports.handler = async(event) => {
await invokeLambda(event);
const response = {
statusCode: 200,
body: JSON.stringify('success'),
};
return response;
};
//Invoke Multiple Lambda functions
async function invokeLambda(event) {
const function1 = {
FunctionName: 'dev-test-async-lambda-1',
InvocationType: 'Event',
Payload: JSON.stringify(event)
};
const function2 = {
FunctionName: 'dev-test-async-lambda-2',
InvocationType: 'Event',
Payload: JSON.stringify(event)
};
await lambda.invoke(function1).promise();
await lambda.invoke(function2).promise();
}
Let me know if I can improve this.