AWS Lambda powers millions of serverless applications, but poor configuration can lead to high costs and slow performance. After analyzing 1M+ daily Lambda invocations, here are the best practices that actually matter.
1. Right-Size Your Memory Allocation
The Mistake: Using default 128 MB memory for all functions.
The Fix: Memory directly affects CPU power AND cost. More memory = faster execution = potentially lower cost.
Memory Impact:
| Memory | vCPU Power | Cost per ms | 100ms execution | 1000ms execution |
|---|---|---|---|---|
| 128 MB | 0.08 vCPU | $0.0000002083 | $0.000021 | $0.000208 |
| 512 MB | 0.33 vCPU | $0.0000008333 | $0.000083 | $0.000833 |
| 1024 MB | 0.58 vCPU | $0.0000016667 | $0.000167 | $0.001667 |
| 3008 MB | 1.79 vCPU | $0.0000049896 | $0.000499 | $0.004990 |
Real Example:
Sweet Spot: 1024 MB for most CPU-intensive tasks. Test and measure!
2. Use Lambda Layers for Dependencies
The Problem: 50 MB deployment packages that take 10+ seconds to cold start.
The Solution: Extract common dependencies to Lambda Layers.
Before (Slow):
my-function.zip (48 MB)
├── function.py
├── boto3/
├── requests/
├── pandas/
└── numpy/
After (Fast):
my-function.zip (2 MB)
└── function.py
my-layer.zip (46 MB) - Cached by Lambda
├── boto3/
├── requests/
├── pandas/
└── numpy/
Implementation:
Result: Cold start drops from 10s → 2s.
3. Enable Provisioned Concurrency for Critical Functions
When to Use:
- API endpoints with less than 100ms latency requirements
- Functions invoked more than 1000 times/hour
- User-facing workflows
Cost Comparison:
On-Demand Lambda:
- 1M invocations/month
- 200ms avg duration
- 512 MB memory
- Cost: $4.17/month
Provisioned Concurrency (5 instances):
- Same workload
- 0ms cold start
- Cost: $21.90/month
- Worth it? YES for user-facing APIs
Configuration:
4. Optimize Cold Start Times
Strategy 1: Minimize Package Size
Strategy 2: Lazy Load Heavy Libraries
Strategy 3: Keep Functions Warm
5. Use Environment Variables Wisely
Anti-Pattern:
Best Practice:
6. Implement Proper Error Handling
7. Use Dead Letter Queues (DLQ)
Why: Don't lose failed events. Send them to SQS/SNS for later analysis.
CloudWatch Alarm for DLQ:
8. Set Appropriate Timeouts and Retries
Default Timeout: 3 seconds (too low for most!)
Guidelines:
- API integrations: 30-60 seconds
- Data processing: 5-15 minutes
- Fire-and-forget: 1-3 seconds
9. Monitor with CloudWatch and X-Ray
10. Use VPC Only When Necessary
VPC Lambda:
- ✅ Access RDS, ElastiCache, private resources
- ❌ Slower cold starts (2-10 seconds)
- ❌ Requires NAT Gateway for internet ($32/month)
No VPC Lambda:
- ✅ Fast cold starts (under 1 second)
- ✅ Direct internet access
- ✅ Free egress to AWS services
Best Practice: Use VPC endpoints instead of full VPC.
Cost Optimization Summary
Before Optimization:
- 128 MB memory
- No layers
- No monitoring
- VPC with NAT Gateway
- Cost: $180/month for 10M invocations
After Optimization:
- 1024 MB memory (faster execution)
- Lambda layers (smaller packages)
- CloudWatch monitoring
- VPC endpoints (no NAT)
- Cost: $54/month for same workload
Savings: 70% reduction!
Exam Tips
For SAA-C03:
- Lambda scales automatically (no EC2 management)
- Use S3 events to trigger Lambda
- Combine with Step Functions for workflows
- Remember: 15-minute maximum execution time
For DVA-C02:
- Know Lambda pricing model (requests + duration)
- Understand cold start optimization
- Use Lambda Powertools for production
- Master error handling and retries
For SAP-C02:
- Design for fault tolerance with DLQ
- Implement observability (X-Ray, CloudWatch)
- Use Provisioned Concurrency for critical paths
- Optimize costs with memory tuning
Relevant Certifications
AWS Services Covered
Continue Reading
DynamoDB vs RDS: The Decision Tree for AWS Architects
Stop guessing which database to use. Learn the exact criteria for choosing between DynamoDB and RDS based on 100+ production deployments.
IAM Policies: The Complete Guide to AWS Security
Master IAM policies with real-world examples, security best practices, and the exact patterns used by AWS Solutions Architects. Pass SAA-C03 IAM questions with confidence.
S3 Lifecycle Policies Deep Dive: Save 90% on Storage Costs
Master S3 Lifecycle policies to automatically transition and expire objects, reducing your AWS bill while maintaining compliance and performance.