Get startedGet started for free

Understanding Lambda's execution model

1. Understanding Lambda's execution model

Welcome to this course on serverless applications with AWS Lambda. This course was developed by Claudio, a senior DevOps Engineer with many years of building and maintaining applications within AWS. I'm Shahzad, a content developer here at DataCamp, and I'll be guiding you through this course! In this video, you'll build a mental model: event in, handler runs in an execution environment, outcome out, and what a cold start means.

2. Automatic execution

Lambda runs your code whenever an event happens. For example, a user uploads an image to S3 and Lambda automatically resizes it. You could swap the trigger: different triggers, same pattern.

3. Idle until triggered

When there's no event, nothing runs. When an event arrives, your handler runs and then stops. Think of it like a motion-sensor light: off until something triggers it.

4. What is AWS Lambda?

So what is AWS Lambda? It's an event-driven compute service that scales automatically with demand. You only pay for the compute time your code uses, making it ideal for automation, APIs, and background jobs.

5. How Lambda executes your code

Regardless of the trigger, the execution flow is always the same. An event source triggers Lambda, which passes the event payload to your handler, and your handler returns an outcome.

6. Scaling with demand

When more events arrive, Lambda spins up additional execution environments to handle them in parallel. Each new environment can introduce a cold start, which we'll cover shortly.

7. The handler function

Every Lambda function has a handler, and this is where your code lives. Lambda calls it with two arguments: `event`, the input payload as JSON, and `context`, which provides runtime metadata. Your handler does its work and returns a result.

8. The event object

The event object is just JSON, but its shape depends on the trigger. An S3 event gives you the bucket name and object key, while an API event includes the HTTP method, path, and body. Different triggers mean different JSON, but the job is always the same: extract what you need.

9. The context object

The context object gives you runtime metadata. For example `get_remaining_time_in_millis` tells you how much time is left before timeout, and `aws_request_id` gives you a unique ID for log tracing.

10. Stateless execution

Each Lambda invocation is independent. Global variables like this counter may persist if the environment is reused, but that's never guaranteed. Store state in S3 or a database instead.

11. Cold starts

A cold start happens when Lambda creates a new execution environment. It loads your code and dependencies, runs initialization, and only then executes your handler. That setup adds latency.

12. Warm starts

A warm start is the opposite. Lambda reuses an existing environment, skips initialization, and your handler runs almost immediately.

13. Init, invoke, freeze

The init phase runs once per environment. After that, only the handler runs each invocation. Between invocations, Lambda may freeze the environment and reuse it when the next event arrives.

14. Optimizing for cold starts

This is why you should place imports and SDK clients outside the handler. That code runs once when the environment is created and gets reused on every warm start.

15. Let's practice!

Let's now jump into some exercises!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.