Home > Net >  AWS Lambda instance runs for more than 15 minutes
AWS Lambda instance runs for more than 15 minutes

Time:02-23

I have a lambda that is running an API, so every instance of the lambda starts a flask server, which runs until the lambda is killed. When another request is received, a new instance of the lambda is created- starting the flask server again.

Looking at the logs from one of my lambdas, a log stream ran for more than 15 minutes (~20 minutes before a process failed). The timeout is set to 5 min 0 seconds.

From what I understand, one long stream is generated per instance, an instance is killed after the timeout has been reached, and no timeout can be set for more than 15 minutes.

How do I have logs that span for ~20 minutes in the same log stream? Is the timeout the amount of time a lambda can run without generating logs before being killed? Is there a way to limit the time a lambda instance runs before the instance is terminated?

Initial log timestamp: 2022-02-20T11:05:05.970-07:00
Final log timestamp: 2022-02-20T11:25:29.895-07:00
Lambda Timeout: 5min0sec

START, END, REPORT statements -- I have multiple of these per log stream:

2022-02-20T11:05:06.509-07:00
START RequestId: 6807306e-f05f-425b-bd75-985b42794eaa

2022-02-20T11:05:07.203-07:00
END RequestId: 6807306e-f05f-425b-bd75-985b42794eaa

2022-02-20T11:05:07.203-07:00
REPORT RequestId: 6807306e-f05f-425b-bd75-985b42794eaa  Duration: 691.58 ms Billed Duration: 2856 ms    Memory Size: 128 MB Max Memory Used: 119 MB Init Duration: 2163.68 ms   
XRAY TraceId: 1-621282cf-00745c0f030b1d810fad9710   SegmentId: 3effe66c64dc69d4 Sampled: true   
2022-02-20T11:05:07.767-07:00
START RequestId: 8c724d99-8640-4634-88b9-1efb774c54a9 Version: $LATEST

2022-02-20T11:05:07.845-07:00
END RequestId: 8c724d99-8640-4634-88b9-1efb774c54a9

2022-02-20T11:05:07.845-07:00
REPORT RequestId: 8c724d99-8640-4634-88b9-1efb774c54a9  Duration: 76.09 ms  Billed Duration: 77 ms  Memory Size: 128 MB Max Memory Used: 119 MB 
XRAY TraceId: 1-621282d3-55a51f9d69552d8905b959b7   SegmentId: 47dc3b533278679f Sampled: true   
2022-02-20T11:05:08.182-07:00
START RequestId: ee69e296-af07-49a2-8264-ed9758d03992 Version: $LATEST

2022-02-20T11:05:08.263-07:00
END RequestId: ee69e296-af07-49a2-8264-ed9758d03992

2022-02-20T11:05:08.263-07:00
REPORT RequestId: ee69e296-af07-49a2-8264-ed9758d03992  Duration: 78.26 ms  Billed Duration: 79 ms  Memory Size: 128 MB Max Memory Used: 119 MB 
XRAY TraceId: 1-621282d4-7c8f7ea75ead3f7c166a44a6   SegmentId: 2dcaed42621dbebd Sampled: true

... the final start/end that fails.

2022-02-20T11:25:29.749-07:00
START RequestId: 5521e958-0ffb-4de7-b028-953a21ac2ac9 Version: $LATEST

2022-02-20T11:25:29.895-07:00
END RequestId: 5521e958-0ffb-4de7-b028-953a21ac2ac9

2022-02-20T11:25:29.895-07:00
REPORT RequestId: 5521e958-0ffb-4de7-b028-953a21ac2ac9  Duration: 142.60 ms Billed Duration: 143 ms Memory Size: 128 MB Max Memory Used: 119 MB 
XRAY TraceId: 1-62128799-19bd91ab324cf0271e964434   SegmentId: 554bd90b4a8ba1c5 Sampled: true

CodePudding user response:

I think your misconception here is When another request is received, a new instance of the lambda is created- starting the flask server again. This is not necessarily true. According to the documentation:

The first time you invoke your function, AWS Lambda creates an instance of the function and runs its handler method to process the event. When the function returns a response, it stays active and waits to process additional events.

This makes perfect sense, especially for an http api. If a new lambda instance had to spin up every request, database connections, caching layers, etc would all have to be reestablished. This would not be scalable or performant. So AWS is doing you a favor by keeping the lambda instance up and running, even though you only pay for the time it's actually working.

As for the timeout question, the timeout is the max time that the lambda will complete a single job, in your case responding to an API request. Not how long a lambda will stay active.

CodePudding user response:

A single request will be terminated after 15 min, but the instance, where the request was worked on, stays for 4.5 hours.(last time I checked at least) and it will write every request in the same log stream.

If another request comes in, when the lambda instance is "alive" and not busy, the same instance will be reused, and will write in the same log stream, as the previous call.

Only if no lambda instance is free to work on request, a new instance will be started.

This is the reason, why the "warm up" calls for some bad designed lambdas with long start time work.

  • Related