AWS Lambda
Run code without servers. Pay only when it runs.
What is Lambda?
Run code without managing servers. Upload a function, it executes when triggered. Scales from 0 to thousands automatically. Pay per millisecond of execution.
Think of it like hiring someone for a single task
You don't pay for idle time. Code runs only when triggered. Scales from zero to thousands automatically.
Key Features
Zero Servers
No EC2 to manage. AWS handles everything.
Auto Scaling
Handles 1 to 10,000 requests. No config needed.
Pay Per Use
Billed per millisecond. Free tier: 1M requests/month.
Multiple Languages
Python, Node.js, Java, Go, .NET, Ruby, or containers.
Event Triggers
API Gateway, S3, SQS, DynamoDB, CloudWatch, 200+ sources.
Provisioned Concurrency
Eliminates cold starts. Costs more.
When to Use
- API backends
- Event processing (S3, SQS triggers)
- Scheduled tasks (cron jobs)
- Real-time file processing
- Webhooks and callbacks
- Microservices
When Not to Use
- Tasks over 15 minutes → Step Functions/ECS
- Need persistent connections → EC2/ECS
- Heavy GPU workloads → EC2
- Large deployment packages → ECS
- Consistent high traffic → EC2/Fargate
- Need full OS access → EC2
Prerequisites
- An AWS account (free tier: 1M requests/month)
- AWS CLI installed
- Basic Python, Node.js, or Java knowledge
AWS Console Steps
Open Lambda Console
Navigate to Lambda in the AWS Console and click 'Create function'
Choose Blueprint
Select 'Author from scratch' and choose a runtime (Python, Node.js, etc.)
Configure Function
Name your function and create/select an execution role
Write Code
Edit the function code in the inline editor or upload a ZIP file
Test
Create a test event and invoke your function
Add Trigger
Connect to API Gateway, S3, or other event sources
AWS CLI Quickstart
Create Lambda function with AWS CLI
Create and invoke a Lambda function using the AWS CLI
# Create a deployment package
zip function.zip index.py
# Create the Lambda function
aws lambda create-function \
--function-name my-function \
--runtime python3.12 \
--handler index.handler \
--zip-file fileb://function.zip \
--role arn:aws:iam::123456789012:role/lambda-role
# Invoke the function
# ...This creates a Python Lambda function and invokes it with a test payload.
First Project Ideas
- Create a REST API with API Gateway
- Process S3 uploads (resize images, extract metadata)
- Send notifications via SNS/SES
- Scheduled cleanup tasks with EventBridge
- Webhook handler for external services
Pro Tips8
Cold starts: minimize package size
performanceCold starts happen on first invocation or scaling. Use smaller packages and Provisioned Concurrency for latency-sensitive APIs.
Keep packages small. Use Provisioned Concurrency for APIsDon't include dev dependencies in production packagesMore memory = more CPU
performanceCPU scales with memory. At 1,769 MB you get 1 vCPU. Faster execution can cost less.
Use Lambda Power Tuning to find optimal memoryDon't default to 128 MB - often slower AND costlierInitialize connections outside handler
performanceCode outside handler runs once per cold start. Reused across warm invocations.
Put DB connections and SDK clients at module levelDon't create connections inside the handlerEnvironment variables: 4KB limit
securityTotal env vars cannot exceed 4 KB. Use Secrets Manager for secrets.
Use Parameter Store for large configsDon't store secrets directly in env varsVPC Lambda needs NAT for internet
securityLambda in VPC has no internet by default. Use NAT Gateway or VPC endpoints.
Use VPC endpoints for AWS services to avoid NAT costsDon't put Lambda in VPC unless accessing VPC resourcesLayers share code across functions
generalUp to 5 layers per function. 250 MB total unzipped including function code.
Use layers for large, stable dependenciesDon't use layers for frequently changing codeAsync invocations retry automatically
reliabilityAsync retries twice on failure. Make handlers idempotent.
Use DynamoDB conditional writes for idempotencyDon't assume events run exactly onceDefault timeout is 3 seconds
reliabilityMax is 15 minutes. API Gateway has its own 29-second limit.
Set timeout based on P99 execution timeDon't set timeout to max 15 min 'just in case'Key Facts8
Max timeout: 15 minutes
Default is 3 seconds. Use Step Functions for longer tasks.
limitMax memory: 10 GB
Range is 128 MB to 10 GB. CPU scales with memory.
limitMax zip size: 50 MB (direct upload)
Use S3 for larger packages. Must be under 250 MB unzipped.
limitMax unzipped size: 250 MB
Includes function code and all layers combined.
limitDefault concurrency: 1,000 per region
Shared across ALL functions. Request increase via AWS Support.
default/tmp storage: up to 10 GB
Default is 512 MB. Ephemeral - cleared on cold start.
limitSync payload: 6 MB max
Request and response both limited to 6 MB.
limitAsync payload: 256 KB max
Store large data in S3 and pass the reference.
limit