Reputation: 676
I have a AWS lambda function written in c#. This function is responsible for calling 5-6 API calls (Post requests).
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
Upvotes: 5
Views: 4320
Reputation: 19728
For your use case, using AWS Step functions will provide a fully managed solution. The steps are as follows.
There are few benefits with Step functions over custom Lambda flow implementation.
Upvotes: 6
Reputation: 456457
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.
Upvotes: 6
Reputation: 5197
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.
Upvotes: 1