How Lambda Functions Work: Init, Invoke, Shutdown

When an event triggers your function, Lambda does not just run your code.⁣ ⁣ Firstly, it creates a secure, isolated execution environment which serves as your function's temporary home. ⁣ ⁣ Lambda uses your configurations (memory, timeout, etc.) to optimise this setup, and this whole process is neatly split into three phases: Init, Invoke, and Shutdown.⁣ ⁣ 🔹Phase 1: Initialisation(INIT)⁣ ⁣ This is where the journey begin (expect the dreaded "cold start" for the first request). ⁣ ⁣ Lambda creates or "unfreezes" a secure execution environment, downloads your function's code, and any layers it needs.⁣ ⁣ → Extension Init: Any companion extensions like monitoring or security agents get started first. They run their setup tasks, ensuring the full ecosystem is ready.⁣ ⁣ → Runtime Init: The runtime (e.g., Node.js, Python, Java) is bootstrapped. It is the engine that will eventually execute your code.⁣ ⁣ → Function Init: Finally, your function's static code is ran. Anything outside the main handler method is executed. ⁣ ⁣ This is where the heavy lifting like initializing SDK clients or creating database connections once and saving crucial milliseconds on subsequent calls is done.⁣ ⁣ This meticulous initialisation ensures everything is perfectly set up before your core logic starts.⁣ ⁣ 🔹Phase 2: Invocation(Invoke)⁣ ⁣ → Function Call: Lambda invokes your function's handler method with the event data.⁣ ⁣ → Execution: Your code runs its intended purpose which can be processing data, calling APIs, or whatever business logic it holds.⁣ ⁣ → The Wait: Once your function completes, Lambda doesn't immediately scrap the environment. ⁣ ⁣ Instead, it freezes the environment, keeping it "warm" and ready. ⁣ ⁣ If another request for the same function arrives shortly, it skips the entire Init phase and jumps right back into Invoke, providing near-instantaneous response times and better performance.⁣ ⁣ 🔹Phase 3: Shutdown⁣ ⁣ When the execution environment sits idle, receiving no further requests for a period (the exact duration is up to AWS), the shutdown phase is initiated.⁣ ⁣ → Runtime Shutdown: Lambda gracefully shuts down the function runtime.⁣ ⁣ → Extension Shutdown: It alerts any running extensions and sends them a final shutdown event, giving them a brief window (typically up to two seconds) to stop cleanly, wrap up logs, or remove any remaining data before the environment is permanently terminated.⁣ ⁣ 📌 Understanding this three-phased Execution lifecycle is key to writing high-performance, cost-efficient Lambda functions. ❓Is it just me who is always looking for what happens behind the scenes of every cloud service that I use or there is someone else that is interested in the little very little details?

  • diagram

The most valuable knowledge here is understanding the Init Phase. To drastically reduce "cold start" latency, make sure to move all your heavy-duty setup like importing large libraries, creating database connections, and initializing AWS SDK clients outside your main handler function. This ensures that expensive work runs only once when the environment is created, not on every subsequent request.

Like
Reply

To view or add a comment, sign in

Explore content categories