Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

occasionally connection timeout from lambda function

Tags:

aws-lambda

I'm not sure why, but from time to time - once in 20 lambda calls, I receive an error:

Connection timed out after 120000ms

the calls are done from ECS container, and all (caller and lambda) are written in node.js.

what should I check?

enter image description here

like image 702
Lior Goldemberg Avatar asked May 22 '26 22:05

Lior Goldemberg


1 Answers

I know this is an old post, but I'll write how I solved a situation with the same error message in a lambda that I was working on. I hope this helps someone with a similar issue.

In my case, I also have a web app inside an EC2 which calls a lambda through lambda.invoke() (npm aws-sdk). Both EC2 and lambda runs on Node.JS. Even though the error is logged inside the EC2, the message is thrown by the lambda itself to the caller (EC2).

My lambda makes ~3,000 requests to an API, what takes ~5 minutes (300,000 ms) to get all responses back. It seems that the lambda Node.JS runtime is keeping a socket alive during the lambda execution, which is higher than 120,000 ms (2 minutes). As the lambda code keeps running for more than this threshold, the runtime throws the error, and the lambda return a callback with it.

According to aws js sdk, the AWS object has one parameter for the http timeout:

httpOptions (map) — A set of options to pass to the low-level HTTP request. Currently supported options are:

  • timeout [Integer] — Sets the socket to timeout after timeout milliseconds of inactivity on the socket. Defaults to two minutes (120000).

After I changed this configuration to 360,000 ms (6 minutes), the lambda executes successfully. So you can just set this parameter to a higher value, according to your needs:

AWS.config.update({httpOptions: {timeout: 360000}});
like image 154
ofri cofri Avatar answered May 25 '26 13:05

ofri cofri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!