Skip to content

Lambda — Serverless Functions

AWS Lambda is a serverless compute service that runs your code in response to events. You upload your code, configure a trigger, and Lambda handles everything else — provisioning servers, scaling, and patching.

Analogy: Lambda is like a vending machine. You put your code in (like stocking the machine), and when someone presses the right buttons (events), the machine runs your code and gives a result. You don’t care what’s inside the machine — it just works.


sequenceDiagram
participant S as Event Source<br/>(S3, API Gateway, etc.)
participant L as AWS Lambda
participant LService as Lambda Service
participant Code as Your Function Code
participant D as Downstream<br/>(DB, S3, API)
S->>L: Event fires<br/>(file uploaded, HTTP request)
LService->>LService: Find function by trigger config
LService->>LService: Spin up execution environment
Note over L: Cold start (if not already warm)
LService->>Code: Pass event object
Code->>Code: Execute your code
Code->>D: Access resources
D-->>Code: Result
Code-->>LService: Return response
LService-->>S: Response forwarded
Note over L: Environment stays warm for ~15 mins

1. Write the function code (Python example):

import json
import boto3
def lambda_handler(event, context):
"""Handle S3 upload event"""
# Get bucket and key from event
bucket = event['Records'][0]['s3']['bucket']['name']
key = event['Records'][0]['s3']['object']['key']
# Process the file
print(f"New file uploaded: {bucket}/{key}")
# Do something with the file
# e.g., create thumbnail, analyze data, etc.
return {
'statusCode': 200,
'body': json.dumps(f'Processed {key}')
}

2. Deploy via CLI:

Terminal window
# Package code
zip function.zip lambda_function.py
# Create Lambda function
aws lambda create-function \
--function-name process-uploads \
--runtime python3.12 \
--role arn:aws:iam::123456789:role/lambda-execution-role \
--handler lambda_function.lambda_handler \
--zip-file fileb://function.zip
# Add trigger (e.g., S3 bucket)
aws lambda create-event-source-mapping \
--function-name process-uploads \
--event-source arn:aws:s3:::my-bucket \
--events s3:ObjectCreated:*

flowchart TB
Lambda["⚡ AWS Lambda<br/>Your Function"]
subgraph Compute["Compute Triggers"]
API_GW["API Gateway<br/>HTTP / REST / WebSocket"]
ALB["Application Load Balancer<br/>HTTP forwarding"]
end
subgraph Storage["Storage Triggers"]
S3["S3<br/>Upload / Delete events"]
DynamoDB["DynamoDB Streams<br/>Table changes"]
end
subgraph Messaging["Messaging Triggers"]
SQS["SQS<br/>Queue messages"]
SNS["SNS<br/>Push notifications"]
end
subgraph Schedule["Schedule Triggers"]
CW_Events["CloudWatch Events<br/>Cron / Rate expressions"]
EventBridge["EventBridge<br/>Event bus / SaaS events"]
end
subgraph Auth["Auth Triggers"]
Cognito["Cognito<br/>Sign-up / Sign-in / Migrate"]
end
API_GW --> Lambda
ALB --> Lambda
S3 --> Lambda
DynamoDB --> Lambda
SQS --> Lambda
SNS --> Lambda
CW_Events --> Lambda
EventBridge --> Lambda
Cognito --> Lambda
style Lambda fill:#7c3aed,color:#fff
style Compute fill:#3b82f6,color:#fff
style Storage fill:#059669,color:#fff
style Messaging fill:#f59e0b,color:#fff
style Schedule fill:#ef4444,color:#fff
style Auth fill:#6366f1,color:#fff

sequenceDiagram
participant User as User / Trigger
participant Lambda as Lambda Service
participant Env as Execution Environment
participant Code as Your Code
Note over Lambda: Function has not been invoked recently
User->>Lambda: 1st Invocation (Cold Start)
Lambda->>Lambda: Download code from S3
Lambda->>Env: Spin up new container
Note over Env: ~100ms–1000ms overhead
Env->>Code: Initialize runtime (Node, Python, etc.)
Note over Code: + extra time for<br/>dependency imports
Code->>Code: Run handler
Code-->>User: Response
Note over Lambda: Function stays warm for ~15 minutes
User->>Lambda: 2nd Invocation (Warm Start)
Lambda->>Env: Reuse existing container
Env->>Code: Run handler immediately
Code-->>User: Response (fast ⚡)
AspectCold StartWarm Start
Latency~100ms–5s (varies by runtime)~1ms–10ms
Why slowDownload code + spin up env + init runtimeReuses existing environment
MitigationProvisioned Concurrency keeps N environments warmN/A
Java/.NETSlowest cold starts—
Python/NodeFastest cold starts (~100ms)—

TriggerFires When…
S3Object created/deleted in bucket
API GatewayHTTP request received
DynamoDB StreamsData changed in table
SQSMessage arrives in queue
CloudWatch EventsScheduled time / cron job
SNSNotification published
Alexa Skills KitUser speaks to Alexa

flowchart TB
Question["What kind of workload?"] --> Long{"Runs for<br/>longer than 15 min?"}
Long -->|"Yes (long-running server)"| EC2["Use EC2<br/>Full control, any OS"]
Long -->|"No (short-lived tasks)"| Event{"Event-driven?"}
Event -->|"Yes (file upload, HTTP, cron)"| Lambda["Use Lambda<br/>Serverless, auto-scale"]
Event -->|"No (steady web app)"| EC2
style Question fill:#f59e0b,color:#fff
style EC2 fill:#3b82f6,color:#fff
style Lambda fill:#7c3aed,color:#fff
AspectEC2Lambda
Duration limitNo limit15 minutes max
ScalingManual / auto-scaling groupInstant, automatic
Cold startNone~100ms-1s on first invocation
PricingPay per hourPay per 1ms of execution
StatePersistent (local storage)Stateless (use S3/DynamoDB)
OS controlFull (SSH, install anything)None (just upload code)
Best forWeb servers, databasesQuick tasks, APIs, data processing

PracticeWhy
Keep functions smallOne function = one job (single responsibility)
Use environment variablesFor config, not hard-coded values
Set memory wiselyMore memory = more CPU too
Handle cold startsUse provisioned concurrency for latency-sensitive apps
Use /tmp for temp filesMax 512 MB, persists while function is warm
Log everythingCloudWatch logs = your debugging friend

  • Lambda = run code without managing servers — just upload and go
  • Triggered by events: S3 uploads, API calls, scheduled cron jobs, and more
  • Auto-scales instantly — from zero to thousands of concurrent executions
  • 15-minute timeout — not for long-running apps (use EC2 instead)
  • Pay per execution — cheaper than EC2 for sporadic, stateless workloads