Serverless Cost Optimization · AWS Guide
Serverless Cost Optimization: Lambda, Fargate, API Gateway & Step Functions
Serverless is efficient by default - but it’s not automatically cheap. Over-allocated Lambda memory, REST APIs instead of HTTP APIs, Standard Step Functions at high volume, and on-demand Fargate without Spot all add up. Here’s how to optimize each service.
Optimization by Service
AWS Lambda
Memory right-sizing + Graviton + batching
- Right-size memory with Lambda Power Tuning tool
- Migrate to arm64 (Graviton2) - 20% cheaper, same performance
- Enable SQS batching: BatchSize 100–1000, reduce invocations 99%
- Purchase Compute Savings Plans for baseline Lambda usage
- Set function timeout to P99 + 30% buffer, not maximum
AWS Fargate
Spot + Graviton + right-sized task definitions
- Enable Fargate Spot for non-critical tasks (up to 70% discount)
- Switch to Graviton (arm64) Fargate tasks: add runtimePlatform in task definition
- Right-size CPU and memory in task definitions - Fargate charges for allocated, not used
- Scale down ECS services to 0 tasks for dev/staging environments at night
- Use ECS Service Auto Scaling to match task count to traffic
Amazon API Gateway
Cache + HTTP API vs. REST API + request reduction
- Switch from REST API to HTTP API where REST features aren't needed - HTTP API is 71% cheaper
- Enable API Gateway caching for expensive backend calls ($0.02/hr for 0.5GB cache)
- Use WebSockets API instead of polling - reduces invocations for real-time features
- Remove unused API stages and APIs - they still incur baseline costs
- Use custom domains and CloudFront in front of API Gateway for caching at edge
AWS Step Functions
Express workflows vs. Standard + state reduction
- Use Express Workflows for high-volume, short-duration executions (1000× cheaper than Standard)
- Standard Workflows: $0.025 per 1,000 state transitions. Express: $0.00001 per state transition
- Reduce state transitions by combining sequential tasks into fewer states
- For event-driven patterns with high volume: evaluate EventBridge Pipes vs. Step Functions
- Use Step Functions Map with maxConcurrency to batch parallel work efficiently
Frequently Asked Questions
Is serverless always cheaper than running EC2 or containers?
Not always. Serverless is cost-efficient for sporadic, bursty, or low-volume workloads. At high, consistent invocation rates, EC2 or Fargate with Savings Plans often becomes cheaper than on-demand Lambda. The break-even depends on your specific function duration, memory, and invocation patterns - use Lambda Power Tuning to find the optimum.
What is the difference between Fargate and Lambda for containers?
Lambda: runs code in response to events, maximum 15-minute execution, up to 10GB memory, pay per GB-second. Fargate: runs Docker containers continuously (or scaled to 0), pay per vCPU/memory per hour. Lambda is cheaper for sporadic events; Fargate is better for long-running processes, web servers, and workloads that need more than 10GB memory.
When should I use HTTP API vs. REST API in API Gateway?
Use HTTP API (71% cheaper, $1.00 per million requests) when you need: JWT authorizers, Lambda proxy integration, simple routing. Use REST API ($3.50 per million requests) only when you need: request/response transformation, built-in API key management, per-method throttling, or WAF integration at the API layer.
How do I reduce Step Functions costs?
The main lever is Express vs. Standard Workflows. Standard costs $0.025 per 1,000 state transitions - for high-volume orchestration (100M transitions/month), that's $2,500/month. Express costs $0.00001 per state transition - the same 100M transitions cost $1,000/month with Express, plus duration-based compute. Express is 1000× cheaper per transition for workflows under 5 minutes.
Does Fargate Spot work for production workloads?
Yes, with proper configuration. Fargate Spot gives up to 70% discount and provides a 2-minute termination notice. For production services: run a mix of Fargate on-demand (baseline capacity) and Fargate Spot (burst capacity) using ECS capacity provider strategy. Stateless tasks with multiple replicas handle Spot interruptions gracefully.