Understanding Lambda's execution model

Serverless Applications with AWS Lambda

Claudio Canales

Senior DevOps Engineer

Automatic execution

Event-driven AWS

  • Lambda runs code when an event happens.
  • Example: S3 upload -> resize image.
  • Different triggers, same pattern.
Serverless Applications with AWS Lambda

Idle until triggered

Idle -> event -> run -> stop

  • Idle: nothing runs.
  • Event: handler runs, then stops.

Motion-light analogy for Lambda

  • Like a motion light: idle until triggered.
  • Trigger flips "off" to "on".
Serverless Applications with AWS Lambda

What is AWS Lambda?

AWS Lambda at a glance

  • Event-driven compute service.
  • Scales automatically with demand.
  • Pay only for runtime used.
Serverless Applications with AWS Lambda

How Lambda executes your code

Lambda execution flow

  • An event source triggers Lambda.
  • Lambda passes an event payload to your handler.
  • Your handler returns an outcome.
Serverless Applications with AWS Lambda

Scaling with demand

Scaling creates more environments

  • More events: more parallel invocations.
  • Lambda spins up more environments.
  • Cold starts can happen per new environment.
Serverless Applications with AWS Lambda

The handler function

def lambda_handler(event, context):
    # Your code here
    return {
        'statusCode': 200,
        'body': 'Hello from Lambda!'
    }
  • event: input payload (JSON).
  • context: runtime metadata.
  • return: output (or success/fail).

Handler receives event and context

Serverless Applications with AWS Lambda

The event object

Object storage event payload

S3 event: bucket + object key.

# event["Records"][0]["s3"]
{
  "bucket": {"name": "my-bucket"},
  "object": {"key": "data.csv"}
}
  • Same job: extract what you need.

HTTP request event payload

API event: method, path, body.

# API request event
{
  "httpMethod": "POST",
  "path": "/users",
  "body": "{\"name\": \"John\"}"
}
  • Different triggers, different JSON.
Serverless Applications with AWS Lambda

The context object

def lambda_handler(event, context):
    time_left = (
        context.get_remaining_time_in_millis()
    )
    request_id = context.aws_request_id
  • get_remaining_time_in_millis Time left before timeout.
  • aws_request_id Request ID for log tracing.
Serverless Applications with AWS Lambda

Stateless execution

Warm start reuse vs reset

  • Each invocation is independent.
  • Reuse is possible, not guaranteed.
  • Store state outside Lambda.
counter = 0

def lambda_handler(event, context):
    global counter
    counter += 1  # may reset
    return counter
Serverless Applications with AWS Lambda

Cold starts

Cold start timeline

  • New execution environment.
  • Load code and dependencies.
  • Run initialization, then handler.
Serverless Applications with AWS Lambda

Warm starts

Warm start timeline

  • Reuse existing environment.
  • Skip most initialization.
  • Handler runs sooner.
Serverless Applications with AWS Lambda

Init, invoke, freeze

Execution environment lifecycle

  • Init runs once per environment.
  • Handler runs per invocation.
  • Between runs, env can freeze.
Serverless Applications with AWS Lambda

Optimizing for cold starts

# init (once per env)
import boto3
s3 = boto3.client('s3')
# handler (every invocation)
def lambda_handler(event, context):
    return {"statusCode": 200}

Init once vs handler per invocation

  • Place imports and SDK clients outside the handler.
Serverless Applications with AWS Lambda

Let's practice!

Serverless Applications with AWS Lambda

Preparing Video For Download...