Introduction
AWS Lambda's pay-per-execution model is attractive, but costs can quickly spiral out of control without proper optimization. This guide covers practical strategies to keep your serverless costs under control.

Cost optimization is crucial for maintaining profitable serverless applications
Understanding Lambda Pricing
Lambda pricing consists of two main components:
- Request charges: $0.20 per 1M requests
- Duration charges: Based on GB-seconds of compute time
- Additional costs: Data transfer, CloudWatch logs, etc.
Memory Configuration Optimization
Memory allocation directly impacts both performance and cost. Here's how to optimize:
# Use AWS Lambda Power Tuning tool
sam deploy --template-file power-tuning-template.yaml
# Or manually test different memory settings
aws lambda update-function-configuration \
--function-name my-function \
--memory-size 512
Key insights:
- Higher memory = faster execution but higher per-second cost
- Find the sweet spot where total cost is minimized
- CPU power scales linearly with memory allocation
Execution Time Optimization
Reduce execution time to directly reduce costs:
Connection Pooling
// ❌ Bad: New connection per invocation
exports.handler = async (event) => {
const client = new DatabaseClient();
await client.connect();
const result = await client.query('SELECT * FROM users');
await client.disconnect();
return result;
};
// ✅ Good: Reuse connections
const client = new DatabaseClient();
exports.handler = async (event) => {
if (!client.isConnected()) {
await client.connect();
}
const result = await client.query('SELECT * FROM users');
return result;
};
Async/Await Optimization
// ❌ Bad: Sequential execution
const user = await getUserById(id);
const orders = await getOrdersByUserId(id);
const preferences = await getPreferences(id);
// ✅ Good: Parallel execution
const [user, orders, preferences] = await Promise.all([
getUserById(id),
getOrdersByUserId(id),
getPreferences(id)
]);
Cold Start Mitigation
Reduce cold start penalties:
- Provisioned Concurrency: Keep functions warm (costs more but eliminates cold starts)
- Smaller deployment packages: Faster initialization
- Language choice: Node.js and Python have faster cold starts than Java/.NET
# Enable Provisioned Concurrency
aws lambda put-provisioned-concurrency-config \
--function-name my-function \
--qualifier $LATEST \
--provisioned-concurrency-count 10
Monitoring and Alerting
Set up cost monitoring to avoid surprises:
# CloudWatch alarm for Lambda costs
aws cloudwatch put-metric-alarm \
--alarm-name "Lambda-High-Cost" \
--alarm-description "Lambda costs exceed threshold" \
--metric-name EstimatedCharges \
--namespace AWS/Billing \
--statistic Maximum \
--period 86400 \
--threshold 100 \
--comparison-operator GreaterThanThreshold
Alternative Architectures
Consider alternatives for cost-sensitive workloads:
- ECS Fargate: Better for long-running processes
- EC2 Spot Instances: For batch processing
- Step Functions: Orchestrate multiple short Lambda functions
Cost Optimization Checklist
- ✅ Right-size memory allocation using power tuning
- ✅ Optimize code for faster execution
- ✅ Use connection pooling and caching
- ✅ Monitor costs with CloudWatch alarms
- ✅ Consider Provisioned Concurrency for high-traffic functions
- ✅ Clean up unused functions and versions
- ✅ Use appropriate timeout values
- ✅ Evaluate alternative architectures for specific use cases
Conclusion
Lambda cost optimization requires a holistic approach combining proper configuration, efficient code, and continuous monitoring. Start with memory optimization and execution time reduction for the biggest impact.
Remember: the cheapest Lambda function is often the one that doesn't run at all. Always question if serverless is the right choice for your specific use case.
Happy optimizing!
Jed