Calling Lambda Function from another Lambda Function synchronously - Node.js
AWS Lambda functions are very useful when you work with Alexa or SageMaker or for any serverless applications.
But if you have multiple Lambdas & wanted to call one from another, how to do it?
Lets find out.
AWS sdk provides a way to invoke Lambda, so we first need to import AWS sdk.
But if you have multiple Lambdas & wanted to call one from another, how to do it?
Lets find out.
AWS sdk provides a way to invoke Lambda, so we first need to import AWS sdk.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var AWS = require('aws-sdk'); | |
AWS.config.region = 'us-east-2'; | |
var lambda = new AWS.Lambda(); |
Here region is the region where the lambda to call resides.
Now lets add a function to call our second lambda
Here Function name is the name of function to call, InvocationType is RequestResponse for synchronous call and Payload is the data which we need to send.
We now have sdk & function ready, lets call the lambda now,
This will successfully return the result.
One important point is, the Role which the calling Lambda function use, must have InvokeAsync & invokeFunction rights. Go to IAM dashboard & add these Lambda actions.
Hope this helps.
Happy Coding,
Parshuram
Now lets add a function to call our second lambda
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function callLambdaFunction(query, callback) { | |
var params = { | |
FunctionName: 'HelloWorld', // the lambda function we are going to invoke | |
InvocationType: 'RequestResponse', | |
Payload: query | |
}; | |
lambda.invoke(params, function (err, data) { | |
if (err) { | |
console.log("Error - " + err); | |
} else { | |
callback(data.Payload) | |
} | |
}) | |
} |
Here Function name is the name of function to call, InvocationType is RequestResponse for synchronous call and Payload is the data which we need to send.
We now have sdk & function ready, lets call the lambda now,
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var requestData = JSON.stringify('{ "name" : "Parshuram" }'); | |
callLambdaFunction(requestData, (theResult) => { | |
console.log("sent : " + requestData); | |
var output = JSON.parse(theResult); | |
console.log("received : " + output); | |
}); |
This will successfully return the result.
One important point is, the Role which the calling Lambda function use, must have InvokeAsync & invokeFunction rights. Go to IAM dashboard & add these Lambda actions.
Hope this helps.
Happy Coding,
Parshuram
Comments