Serverless Cost Optimization: Lambda and DynamoDB
Lambda power tuning and capacity optimization.
Serverless Isn't Free: Optimizing Lambda and DynamoDB Costs
Serverless architectures eliminate server management, but costs can still spiral if you're not careful. Lambda's per-invocation pricing and DynamoDB's capacity units require different optimization strategies than traditional EC2 workloads.
Lambda Cost Optimization
1. Right-Size Memory (and CPU)
Lambda allocates CPU proportionally to memory. A 128 MB function gets half the CPU of a 256 MB function. Sometimes increasing memory makes functions run faster and cheaper overall.
Use AWS Lambda Power Tuning (an open-source Step Functions workflow) to find the optimal memory setting for each function.
2. Reduce Invocation Count
- Batch SQS messages — increase
BatchSizeto process 10-100 records per invocation instead of 1 - Use EventBridge batching and filtering to reduce unnecessary triggers
- Cache responses with API Gateway caching to avoid invoking Lambda for repeated requests
3. Optimize Execution Duration
- Use connection pooling (reuse database connections across invocations)
- Minimize cold starts with Provisioned Concurrency for latency-sensitive functions
- Use Lambda Layers to share common dependencies and reduce package size
DynamoDB Cost Optimization
On-Demand vs Provisioned Capacity
| Mode | Best For | Cost |
|---|---|---|
| On-Demand | Unpredictable, spiky traffic | $1.25 per million write requests |
| Provisioned | Steady, predictable traffic | ~$0.00065 per WCU/hour |
| Provisioned + Auto Scaling | Predictable with occasional spikes | Cheapest for high-volume |
Key DynamoDB Savings
- Switch to Provisioned + Auto Scaling for tables with predictable patterns
- Enable DynamoDB TTL to automatically delete expired items (free)
- Use projected attributes in GSIs to reduce storage and read costs
- Enable DynamoDB Accelerator (DAX) for read-heavy workloads to reduce consumed RCUs
Eazy SaaS Tip: For Lambda, run Power Tuning on your top 10 functions by invocation count. For DynamoDB, switch steady-state tables from On-Demand to Provisioned + Auto Scaling — it typically saves 50-70%.