Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can an AWS Lambda function call another

I have 2 Lambda functions - one that produces a quote and one that turns a quote into an order. I'd like the Order lambda function to call the Quote function to regenerate the quote, rather than just receive it from an untrusted client.

I've looked everywhere I can think of - but can't see how I'd go about chaining or calling the functions...surely this exists!

like image 385
Silver Avatar asked Jul 30 '15 03:07

Silver


2 Answers

I found a way using the aws-sdk.

var aws = require('aws-sdk'); var lambda = new aws.Lambda({   region: 'us-west-2' //change to your region });  lambda.invoke({   FunctionName: 'name_of_your_lambda_function',   Payload: JSON.stringify(event, null, 2) // pass params }, function(error, data) {   if (error) {     context.done('error', error);   }   if(data.Payload){    context.succeed(data.Payload)   } }); 

You can find the doc here: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Lambda.html

like image 92
Nicolas Grenié Avatar answered Sep 22 '22 14:09

Nicolas Grenié


You should chain your Lambda functions via SNS. This approach provides good performance, latency and scalability for minimal effort.

Your first Lambda publishes messages to your SNS Topic and the second Lambda is subscribed to this topic. As soon as messages arrive in the topic, second Lambda gets executed with the message as it's input parameter.

See Invoking Lambda functions using Amazon SNS notifications.

You can also use this approach to Invoke cross-account Lambda functions via SNS.

like image 43
adamkonrad Avatar answered Sep 22 '22 14:09

adamkonrad