简体   繁体   中英

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

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.

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.

If you just want a fire and forget, then don't use await. Just use an HttpClient method (get, put, etc.) to call the API, and you're done. Those methods return a Task<HttpResponseMessage> which you don't care about, so it's fine for your Lambda to exit at that point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM