Donatas
Donatas

Reputation: 25

How to execute AWS lambda after another lambda

The scenario that I want to achieve with AWS Lambda functions:

  1. Make a call to Lambda and receive a response.
  2. After the response is received I need to execute another Lambda function, which does its own job.

The pain point for me is how to trigger the second Lambda without a second call. I am trying to do this in AWS Step Functions, however, I believe I can not return something from the first function and then carry on with executing the second one, or can I?

Upvotes: 1

Views: 5496

Answers (4)

Frosty
Frosty

Reputation: 698

This can be easily achieved by AWS Step Function itself. You can simply create two Task State each one for your Lambda and if you don't want to pass any of the input in any of the lambda that is fine too because it's not necessary at all.

So in short your simple step function will look like:

enter image description here

This is going to run in sequence.

You can simply define your state like:

"States": {
    "FirstLambda": {
      "Type": "Task",
      "Resource": "<first-lambda-arn>",
      "TimeoutSeconds": 9000,
      "Next": "SecondLambda"
    },
    "SecondLambda": {
      "Type": "Task",
      "Resource": "<second-lambda-arn>",
      "TimeoutSeconds": 9000,
      "End": true
    }
  }

You need not to to pass anything as payload to lambda if you don't want too. and if required you can pass the input of first lambda to another lambda function using "ResultPath":"$.output" in the first state definition above (definition for First Lambda.) and passing that $.output within payload of next one.

Upvotes: 2

Brand
Brand

Reputation: 357

I have a similar setup between some of my lambda functions.

I would just have the calling service send an http request to the designated endpoint of the second lambda function when it recieves a response from the first lambda function.

That of course would require the second lambda function to have an endpoint setup to trigger the job.

Upvotes: 0

Ersoy
Ersoy

Reputation: 9624

You may achieve this by using aws lambda destinations.

For each execution status such as Success or Failure you can choose one of four destinations: another Lambda function, SNS, SQS, or EventBridge. Lambda can also be configured to route different execution results to different destinations.

This post may give you some insights.

Upvotes: 1

ljmocic
ljmocic

Reputation: 1887

Possible solution:

  1. Create SQS queue
  2. Put return object as a message in a queue
  3. set SQS queue as a trigger for the second lambda and get the SQS queue message from event object

You can find more about about it in the docs

Upvotes: 0

Related Questions