How does AWS Lambda work behind the scenes?
AWS Lambda functions run in isolation via a container sandbox. After a function is created or updated, and then first executed, and prior to calling of the function handler, a new container will be spun up which will manage the allocated memory (defined at initial configuration), and also hold an instance of the code to be executed. There are 3 ways that a Lambda function can complete:
- Timeout – the function is still running after the specified duration (set in configuration) has lapsed. As of this writing, the maximum timeout can be set to 5 minutes.
- Code Determined – the code has completed and calls context.done().
- Default – once all code has finished running. If context.done() is not called a message will be logged “Process exited before completing request”.
- Forced – if your program crashes it will also exit.
When calling context.done, the first argument should be the equivalent of null, otherwise an error is assumed and will be logged to AWS Cloudwatch. The second parameter is optional and will be output to to the console, which can be helpful for testing purposes.
Calling the same lambda function multiple times may create a new container or reuse an existing one. Containers are reused for performance reasons as it will reuse any initialization steps that were needed. In short, you will never know when your container will be reused and only that it will not be reused if the lambda function changes. Never rely on the tmp folder because of this, assume it will never persist between lambda executions. It is possible that long running processes that are not complete when your Lambda function ends, may pause and resume again upon the next execution of the lambda function, however this will only happen if the container is reused, which again can not rely on.
AWS Lambda Limits
- Resource Limites
- Temporary storage “/tmp” 512 MB
- Number of file descriptors 1,024
- Combined total number of processes and threads 1,024
- Execution timeout 300 seconds (5 minutes)
- Payload size for request body (RequestResponse) 6 MB
- Payload size for body payload (Event) 128 K
- Payload size for response body (RequestResponse) 6 MB
- Max memory available 1536 megabytes (aka 1.5 gigabytes)
- CPU speed is determined by amount of ram chosen. The more ram, the faster CPU speed.
- Deployment Limits
- Maximum size of uploaded jar or zip file for an AWS Lambda function 50 MB
- Maximum size of package after decompression 250 MB
- Account Per Region Limits
- Maximum size for all uploaded deployment packages per region 75 GB
- Concurrent executions of an AWS Lambda function 100