What's the best way to handle lambda to lambda com...
# help
a
What's the best way to handle lambda to lambda communication. I know it's an antipattern to have a lambda directly invoke another lambda. I read somewhere that using a queue or messaging service would be an option. Anyone have any suggestions? Is there an SST example for this? Basically in the project I'm working on we are using apollo federation and one of the entities is getting too large for 1 lambda, and we need to split it into multiple lambdas. Any help would be appreciated.
r
You have a few options such as publishing an SNS topic and having some lambdas subscribe to that topic (Publish-Subscribe model). Or you could push a message into an SQS queue, again with lambda subscribers. SNS - subscribers are pushed the data, SQS the subscribers pull the queued messages Good article here
a
My only concern is that this is 1 way communication, what if I need 2 way communication
kind of like a request and a response
r
Others may have a different opinion but I'd say lambda isn't really designed for that. Typically you raise events that can be handled by the appropriate lambda handlers. State can be passed via those events and/or persisted in a database(s) as necessary.
a
would it make sense to migrate from the
ApolloApi
stack to the
AppSync
stack since with AppSync it's easier to separate queries into their own lambdas
s
IMO there’s no problem with a Lambda calling another Lambda. just keep in mind, if Lambda A calls Lambda B and it’s waiting for a response, you’re effectively paying extra for having both Lambdas running.. so it’s definitely not ideal. if it’s a tiny bit of data, probably not an issue otherwise, I would set up an SQS queue. Lambda A pushes into the queue, and Lambda B is set up to poll from the queue periodically and process batches of SQS messages. this is especially good for a lot of rapid data.. like you are creating thousands of bits of data for Lambda B to process. once that data is processed, there are lots of ways to report that to whichever service you need to
f
When you having 1 Lambda sync invoking another Lambda, in some cases it might make sense to put all that logic into the same Lambda.
c
Is this not an ideal use case for step functions? I.e break a big function into smaller functions and then use step functions to orchestrate it. I haven't used step functions before but this is my understanding.
s
@Chad (cysense) we’d probably need to know more about what you’re trying to do. but Step Functions are great for running a bunch of functions in parallel or series, having some functions wait for another process to complete, etc
t
My suggestion is 1. Look at stepfunctions, they're a bit confusing but it's usually what you want in situations where you need lambda to lambda 1. Frank's suggestion that this might also hint that you have shared functionality that you need and should just call that code directly instead of calling another function 1. Think about whether you can rework this not to need a request + reply and if you can model it as events. In which case you can use eventbridge / sqs
^ slack has a bug which numbers all my lists as 1,1,1
a
Thank you all for the suggestions