Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda c# asynchronous API calls

I have a AWS lambda function written in c#. This function is responsible for calling 5-6 API calls (Post requests).

  1. All of these API calls are independent of each other.
  2. I do not care of the about the responses from any of these API calls.
  3. Each of the API call takes about 5 seconds to complete even though I do not care about the follow up response.

Question: I want my lambda function to execute and respond within a second. How can I asynchronously make my API calls such that lambda function can finish all of these within my time limit without waiting for the responses from the API calls? Ideally, I want to implement a fire and forget API call system that sends the final response back without any delay.

According to AWS lambda documentation, I have to use await operator with asynchronous calls in lambda to avoid the function being completed before the asynchronous calls are finished.

Am I missing something here? Or is there a way to accomplish this?

Thanks

like image 845
OrangeTree Avatar asked Dec 10 '22 10:12

OrangeTree


2 Answers

You can't run code "outside" of a serverless request. To try to do so will only bring pain - since your serverless host has no idea that your code is incomplete, it will feel free to terminate your hosting process.

The proper solution is to have two lambdas separated by a queue. The first (externally-facing) lambda takes the POST request, drops a message on the queue, and returns its response to the caller.

The second (internal-only) lambda monitors the queue and does the API calls.

like image 157
Stephen Cleary Avatar answered Dec 29 '22 03:12

Stephen Cleary


For your use case, using AWS Step functions will provide a fully managed solution. The steps are as follows.

  1. Define you AWS step function flow, based on whether you want trigger them parallel or one after another.
  2. Integrate the initial step with API Gateway POST method.
  3. After starting the Step function state machine, it will return a success state (Immediately without waiting for the end state)

There are few benefits with Step functions over custom Lambda flow implementation.

  • You can configure to retry each step, if individual steps return errors.
  • If any of the steps ends up with an error, you can trigger a callback.
  • You can visually identify and monitor which step had issues & etc.
like image 32
Ashan Avatar answered Dec 29 '22 01:12

Ashan