5

How can we invoke multiple AWS Lambda functions one after the other ?For example if an AWS Lambda chain consists of 8 separate lambda functions and each simulate a 1 sec processing event and then invoke the next function in the chain.

4 Answers 4

12

I wouldn't recommend using direct invoke to launch your functions. Instead you should consider creating an SNS Topic and subscribing your Lambda functions to this topic. Once a message is published to your topic, all functions will fire at the same time. This solution is also easily scalable.

See more information at official documentation Invoking Lambda functions using Amazon SNS notifications

Sign up to request clarification or add additional context in comments.

4 Comments

Doesn't this add unnecessary latency by inserting an extra hop to SNS rather than just invoking the necessary lambda function directly? Also, this doesn't return a callback I assume?
It can possibly add latency. It doesn't return a callback from invoked lambda. Each solution has tradeoffs.
Although using SNS is an interesting idea for this, I'm not sure I can envision how to do this in a chained manner. As mentioned, using SNS would trigger all lambda subscribers at the same time. How would you do chaining with SNS? Have each lambda append a specific code in a msg and have each subscription filter only based on a code? Seems like a lot of work.
You can filter messages on SNS subscription. I haven't used it personally, but it seems like something worth exploring for this use case: docs.aws.amazon.com/sns/latest/dg/message-filtering.html
5

With python:

from boto3 import client as botoClient
import json
lambdas = botoClient("lambda")

def lambda_handler(event, context):
    response1 = lambdas.invoke(FunctionName="myLambda1", InvocationType="RequestResponse", Payload=json.dumps(event));
    response2 = lambdas.invoke(FunctionName="myLambda2", InvocationType="RequestResponse", Payload=json.dumps(event));

Comments

5

A simple way to do it is to use the AWS sdk to invoke the lambda function.

The solution would look different depending on what sdk you use. If using the Node sdk I would suggest promisifying the sdk with a Promise library like for example Bluebird.

The code would look something like:

const Promise = require('bluebird');
const AWS = require('aws-sdk');
const lambda = Promise.promisifyAll(new AWS.Lambda({ apiVersion: '2015-03-31' }));

lambda.invokeAsync({FunctionName: 'FirstLambdaFunction'})
  .then(() => {
    // handle successful response from first lambda
    return lambda.invokeAsync({FunctionName: 'SecondLambdaFunction'});
  })
  .then(() => lambda.invokeAsync({FunctionName: 'ThirdLambdaFunction'}))
  .catch(err => {
    // Handle error response  
  );

The reason why I like this approach is that you own the context of all the lambdas and can decide to do whatever you like with the different responses.

Comments

0

Just call the next Lambda function at the end of each function?

Use http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Lambda_20141111.html#invokeAsync-property if you are using Node.js/JavaScript.

4 Comments

Thanks but I did this using the invoke method because the invokeAsync method is deprecated. (docs.aws.amazon.com/lambda/latest/dg/API_InvokeAsync.html)
The API Invoke method (docs.aws.amazon.com/lambda/latest/dg/API_Invoke.html) allows you to specify the InvocationType as Event which is the current non-deprecated way to asynchronously call Lambda functions, although using an SNS topic is still probably advisable as it has other benefits.
@JaredHatfield, do you have an example of using invoke (not invokeAsync) with InvocationType: 'Event' to chain several lambda calls ? I'm looking for something similar to Promise.all([lambda1, lambda2, ...]).then(...).
I'd recommend looking into Lambda Step Functions now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.