Aws-lambda-dotnet: Guidance for HttpClient life-cycle in Lambdas

Created on 4 Jun 2018  ·  4Comments  ·  Source: aws/aws-lambda-dotnet

Microsoft recommends, for most use-cases, that you create a singleton of HttpClient, configure it and share it for the life-cycle of the app, or Http request in some cases.

How do Lambda's work in regards to this? Each time my Lambda fires (regardless if cold/warm) is it the same process running each time or a new dotnet process? If it's a new dotnet process each time then I can safely just instantiate and dispose of the HttpClient because it's only used once through-out the life-cycle of the Lambda. If the same process is re-used (with the handler invoked multiple times) then I need to handle the life-cycle of my HttpClient differently.

Any guidance on how we should treat the usage of HttpClient in Lambda's would be appreciated.

guidance

Most helpful comment

Here is my talk from NDC Minnesota last month. If you skip ahead to about 2:40 into I go through the lifecycle of a Lambda environment which will hopefully clear things up for you.

https://vimeo.com/271358846

All 4 comments

Here is my talk from NDC Minnesota last month. If you skip ahead to about 2:40 into I go through the lifecycle of a Lambda environment which will hopefully clear things up for you.

https://vimeo.com/271358846

Answered my question, thanks!

This was so helpful! Although this is 2 years too late, Thanks! @normj

Thanks @normj

For anyone who comes across this searching for answers - create a singleton HttpClient, it should survive a few invocations of events until such time that you had a long period of inactivity and Lambda destroys the environment running your code.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rv-dtomaz picture rv-dtomaz  ·  6Comments

briancullinan picture briancullinan  ·  7Comments

matheusmaximo picture matheusmaximo  ·  7Comments

Xocix picture Xocix  ·  7Comments

JustinGrote picture JustinGrote  ·  5Comments