I have two AWS Lambda functions doing different things and I want to run the second Lambda only after the first one is completed. It's maybe something simple and I'd like to know how I can do that? I'm aware I can use Step Functions but I haven't used it much. Is this achievable without using Step Functions?
- Step Functions is the proper way to coordinate this but be aware that a Lambda function is just code running in AWS and it can do pretty much anything. It can use the AWS Lambda API to invoke a second Lambda function, for example, passing a given payload and indicating whether or not to wait for the completion of that second Lambda functionjarmod– jarmod2022-03-09 00:05:22 +00:00Commented Mar 9, 2022 at 0:05
- What does the first Lambda do? Depending on what it does, there might be better ways to run a second Lambda than using Step Functions. Though in most cases, Step Functions would be the way to go.Noel Llevares– Noel Llevares2022-03-09 02:11:30 +00:00Commented Mar 9, 2022 at 2:11
3 Answers
Why don't you use Lambda Destinations ?
You can configure your lambda to trigger another lambda in case your first lambda was successfully executed. In case of failure (e.g. if an error is raised with no catch), you can configure the destination to be something else, like SQS, SNS or Event Bridge.
Note that destinations are meant for asynchronous calls, unlike Step Functions. In Step Functions, the flow is halted until the lambda function finished. With Destinations, the second lambda function is called asynchronously.
Moreover, the destination feature will not work if you called the initial lambda function synchronously (e.g. via the Test button).
Comments
Yes, you can do this. The perfect tool for that is AWS Step Functions which would allow you to chain your functions.
1 Comment
The first Lambda function can invoke another Lambda function before it exits. It can simply use a normal invoke() API call to the AWS Lambda service.
You would probably want to make this an asynchronous call, so that the first function does not wait for the second function to finish.