2.1.1.2. Lambda Execution Environment & Runtime
2.1.1.2. Lambda Execution Environment & Runtime
First Principle: Lambda's execution environment provides a secure, isolated, and managed runtime for your code, abstracting server operating systems and simplifying dependency management.
When Lambda invokes your function, it creates an isolated execution environment with your chosen runtime (Python, Node.js, Java, etc.), your code, and any dependencies. Understanding this environment is critical for optimizing performance.
Memory drives everything. You configure memory (128 MB to 10 GB), and CPU scales proportionally. A function with 1,769 MB gets one full vCPU. This means CPU-bound functions run faster with more memory ā even if they don't need the extra RAM. This is one of the most common optimization questions on the exam.
Each environment gets /tmp storage (512 MB by default, up to 10 GB) for temporary file processing. This space is ephemeral ā it persists across warm invocations of the same environment but vanishes when the environment is recycled.
Lambda Layers let you package shared libraries separately from your function code. Instead of bundling a 50 MB ML library into every function, you deploy it once as a Layer and attach it to multiple functions. Container images (up to 10 GB) give you full control over the runtime environment when Layers aren't sufficient.
The execution context ā the environment that persists between warm invocations ā is where optimization lives. Database connections, SDK clients, and configuration loaded outside your handler function are reused across invocations, avoiding repeated initialization.
Scenario: You're developing a Python Lambda function that needs to process a large image file (several MBs), requiring temporary local storage during processing. You also want to include a common Python library without bundling it directly in your function's deployment package.
ā ļø Exam Trap: Increasing Lambda memory also increases CPU proportionally. A function that's CPU-bound (not memory-bound) will run faster with more memory ā even if it doesn't need the extra RAM. This is a common optimization question.
