With AWS container reuse, I want to understand if the reuse happens when any call is pending within that container or after the call has finished. I need to declare some resources outside of the handler function and either I use synchronization and doc, but before I do that I want to understand what AWS promise/contract is.
1 Answers
The lambda does not need synchronisation, everything you declare outside the handler will be reused for the next invocation that hits the same execution environment. But there will not be two invocations in the same container at the same time. Only after the first invocations finishes a second one may hit the same execution environment.
The key term to look for is the already mentioned "execution environment" / "execution context". You may find https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html and https://docs.aws.amazon.com/lambda/latest/dg/running-lambda-code.html helpful.
After a Lambda function is executed, AWS Lambda maintains the execution context for some time in anticipation of another Lambda function invocation. In effect, the service freezes the execution context after a Lambda function completes, and thaws the context for reuse, if AWS Lambda chooses to reuse the context when the Lambda function is invoked again
The "After" does not make it 100% absolute, but clear enough and I can confirm that this is the case.

- 55,258
- 23
- 97
- 137
-
1thanks, is there any AWS documentation that I can confirm for **there will not be two invocations in the same container at the same time** and then I will accept answer, for now it is up voted – Raj Jan 13 '20 at 18:50
-
1@Raj added some context, is that enough? – luk2302 Jan 13 '20 at 18:53
-
2@Raj see also https://stackoverflow.com/a/45782990/1695906 including the comments. – Michael - sqlbot Jan 14 '20 at 01:06
-
@michael-sqlbot Thanks, it helps a lot. I wish AWS can be clear enough on documentation. I hope AWS does not intend to change this. – Raj Jan 14 '20 at 02:28
-
1@Raj it has been this way since Lambda launched, about 5 years ago, and there's no reason to believe this would change, as it's an intrinsic part of the design. Remember, for each invocation, you are paying for a certain dedicated amount of memory and CPU cycles to run your code for the required number of milliseconds. If each invocation didn't have exclusive use of that same amount of resources, the entire pricing model would not make sense. – Michael - sqlbot Jan 14 '20 at 02:59
-
@michael-sqlbot perfect, understood. – Raj Jan 14 '20 at 03:19