Lambda call another Lambda

We have a micro services of Lambda

What is the best way to call another lambda synchronous or asynchronous ?

I find two option:

  • Call lambda invoke from boto (it load boto libarry and maybe it take more time)
  • make request to Private API Gateway (support only Synchronous )

How do you call another lambda in your company ?

Async - queue,
Sync - SDK

Firstly dont link lambda to lambda directly it leaves you to problems on release of new code.

If you dont need the response in the caller use and sqs queue as a broker. I.e. lambda → sqs → lambda

If you do need the response api gateway is a good option as it lets you decouple the two.

AFAIK you don’t need an explicit sqs queue between the lambdas…lambdas essentially have one built in for async invokes already…I’m pretty sure you can even associate a dlq to the lambda directly.

I use step functions to orchestrate several lambas and an API.

Either SQS, step functions or using the destination feature of lambda :slight_smile:

Step functions or event triggers from S3 or event bridge. A small number are scheduled using Eventbridge.

You can but be aware of the limitations. Lambda’s internal queue isn’t like-for-like with SQS. For example, the max message age is 6 hours instead of 14 days, aged out messages will go directly to the on-failure destination.

https://docs.aws.amazon.com/lambda/latest/dg/invocation-async.html

From the AWS docs and stackoverflow threads I’ve read they all suggest decoupling with SQS/step functions/SNS for increased scalability. I also saw that there may be problems where the first lambda waits for the second lambda’s completion before terminating, accruing 2x cost.

I separate mine with SQS so I’m wondering if you have experienced any problems with directly invoking?

Have you looked into State Machines?

we try to avoid calling out to other lambdas.

one rule to live by is that waiting in lambda is bad practice.

if you pay per compute second then it’s optimal to reduce unnecessary idle time. this includes calling out to another lambda.

tell us more about your process so we can help you remove the requirement to call another lambda

This is the correct answer.

I’ve personally used: API Gateways, boto3 invocation, and step functions. They all have different applications depending on what you need to do.

In general, asynchronous is better than synchronous since nesting lambda calls just means that you’re spending for one lambda to idle while it waits for another; plus it’s really easy to time out and fail that way.

Yes you can but doing this from the sqs side is easier and wont lose messages if you have to roll back a release or change a lambda while traffic is in flight. If you do it from sqs the builtin dlq redrive works well and is easy to use.

If you have a lambda, you should not synchronously call another lambda. You end up paying double the cost during the time you’re waiting for a response. It gets worse for each layer of invocation.

Try to design your lambdas in such a way that they can do all the work they need to do and then ship off their results for something else to process.

Don’t make a distributed call stack with lambda…that’s gonna cause you a lot of pain and money.

My understanding is you should avoid lambdas calling lambdas directly for several reasons.

  1. Cost - you pay for the amount of time a lambda runs. If lambda 1 has to wait for lambda 2 you are basically paying double.

  2. Efficiency - I have been told it’s better to use Lambda to SNS or SQS then use those as a trigger to the next Lambda.

I recently read an interesting article on how to build asynchronous API calls in AWS.

https://builtin.com/software-engineering-perspectives/asynchronous-api-dynamodb-streams

This article made me think I could set up an API gateway with a POST and GET.

Post - write to db and return an id, then trigger a lamb that writes to SNS to trigger my next Lambda and chain my lambdas like that. Each lambda updates the DB.

Get - checks the DB to see if all my lambdas are done running to return completed data to the client.

But what if you need the response of lambda b inside lambda a?

In many cases we just have the lambda call the other lambda. It’s the easiest way to do it.

You should only try to be more fancy about it if there’s something else going on such as

  • your expected number of invocations is so high that the cost of lambdaA waiting on the result of lambdaB is significantly more than the additional AWS + developer cost.
  • your latency requirements for lambdaA are very tight.
  • something else

You use some sort of persistent layer such as DB or cache or orchestrator, like a step function.

+1 step functions were designed for this use case, amongst others.