AWS Lambda chaining best practice

2020-03-26 04:11发布

问题:

I'm looking a solution for my problem and maybe someone could give me some ideas. I have a API Gateway plugged to a aws lambda A. I have to handle cases like this:

  1. Lambda A should call lambda B and if there are any results, return to the API Gateway.
  2. Lambda A should call lambda B and if no results, it will call lambda C, and then return whatever the results are to the APi Gateway.

So, my problem is how to chain these lambdas, because I don't want to have a huge lambda. At first, I thought about using Step Functions except that this works in a asynchronous mode, so no good for my case. I know I can do a lambda to call the step function and wait for the result, but I don't like this solution.

Any ideas for a nice solution ?

Thanks.

C.C.

回答1:

You can use invoke lambda with "async await" here is docs:

https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Lambda.html#invoke-property

For asynchronous call: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Lambda.html#invokeAsync-property



回答2:

You can use AWS Step Function to manage coordination between AWS lambda functions.



回答3:

Really B & C are apis that A is calling

Put lambda B & C behind another api gateway, using IAM auth. Give A rights to call that Api via your IAM Role.

  • Now Your external clients call A via whatever auth scheme.
  • A is free to call B or C via api (url as parameter perhaps)
  • A can return results to caller

For a more granular approach, you can have B/C as their own api gateways, meaning your have decoupled them fully