Search code examples
.netamazon-web-servicesaws-lambdadotnet-httpclient

How to use HttpClient in AWS Lambda Correctly


I have a .net lambda function on AWS which is executed every 1 minute.

In the function, I have a public static readonly HttpClient client = new HttpClient(); initialised at the top of my code before any functions, and all the subsequent API requests use this client (there are two API’s I call, A and B).

When I run my Lambda function it works perfectly for about an hour, but then API A warms me that I’m Limited to 500 requests per minute which leads me to believe that somehow I’m making more than 500 requests when I run my Lambda function after an hour of executing it without a problem.

I’ve checked and seen that the each time I run the lambda function after the first time, the HttpClient client is already initialised from the first instance of Lambda, which leads me to believe that every subsequent Lambda function after the first instance is a warm start.

So my question is: what is the correct way of implementing HttpClient to run every 1 minute on AWS Lambda without somehow making hundreds of requests?


Solution

  • Apparently I was using everything correctly.

    We have another service which also uses the same API and sends lots of API requests between specific times (It is inefficient, I know), and this was what was causing the error I mentioned in the original post.

    Solution for now is to run my programme at the times when the other software is not calling the API.