Serverless Cost Invocation Estimator
Calculate AWS Lambda and serverless costs including API Gateway and data transfer. Enter values for instant results with step-by-step formulas.
Formula
Total Cost = Lambda Compute + Invocations + API Gateway + Data Transfer + Storage
Serverless cost aggregates multiple pricing components. Lambda Compute = (GB-seconds - 400K free) × $0.0000166667. GB-seconds = Invocations × (Duration/1000) × (Memory/1024). Lambda Invocations = (Invocations - 1M free) × $0.0000002. API Gateway = Requests × rate ($3.50/M REST, $1/M HTTP). Data Transfer = (GB - 1 free) × $0.09. Storage = GB × $0.023 (S3). Example: 10M invocations, 200ms, 512MB, 100GB transfer, 5M API calls. Compute: 10M × 0.2 × 0.5 = 1M GB-sec, billable 600K, cost $10. Invocations: 9M billable, cost $1.80. API: 5M × $3.50/M = $17.50. Transfer: 99GB × $0.09 = $8.91. Total: $38.21. Formula works because it captures all pricing dimensions. Common mistake: Only calculating Lambda, missing API Gateway (often largest component). Optimization: HTTP API saves $12.50/month in this example. Memory optimization may reduce compute cost 20-40% by finding sweet spot where faster execution offsets higher per-second rate. Data transfer compression can cut $4-5/month. Holistic cost modeling prevents serverless sticker shock—model all components before committing to architecture.
Frequently Asked Questions
How is AWS Lambda priced?
Lambda pricing has two components: (1) Invocations: $0.20 per 1 million requests (first 1M free/month). (2) Duration: $0.0000166667 per GB-second (first 400K GB-seconds free). GB-second = (Memory in GB) × (Duration in seconds). Example: 1M invocations, 512MB memory, 200ms avg duration. Invocations: Free (within 1M). Duration: 1M × 0.2s × 0.5GB = 100K GB-seconds. Cost: 100K × $0.0000166667 = $1.67. Plus API Gateway if used ($3.50/million REST, $1/million HTTP). Total depends heavily on duration and memory—optimize these for cost savings.
When is serverless cheaper than EC2?
Serverless wins for: Variable, spiky traffic (scale to zero when idle). Low utilization (<20% of time active). Unpredictable demand. Event-driven workloads. EC2 wins for: Steady, high-volume traffic (>10M requests/day consistently). Long-running processes (>15 min). Specific hardware needs (GPU, high memory). Breakeven example: t3.medium ($30/month) handles ~10M requests/month at 100% utilization. Lambda at 10M requests, 200ms, 512MB: ~$35/month. Similar cost, but Lambda scales automatically; EC2 needs auto-scaling setup. At 50M requests, EC2 clearly cheaper ($30 vs $150+ Lambda).
What are the hidden costs of serverless?
Beyond Lambda: (1) API Gateway: $3.50/million (REST) or $1/million (HTTP). Often exceeds Lambda compute cost. (2) Data transfer: $0.09/GB out to internet. (3) CloudWatch Logs: $0.50/GB ingested + $0.03/GB stored. Verbose logging adds up. (4) NAT Gateway: $0.045/hour + $0.045/GB if Lambda in VPC accesses internet. (5) DynamoDB/RDS connections: DynamoDB pay-per-request or provisioned; RDS Proxy for connection pooling. (6) Step Functions: $0.025/1000 state transitions. Mitigation: Use HTTP APIs (not REST), compress logs, cache in CloudFront, avoid VPC unless necessary.
How do I optimize Lambda duration?
Duration directly impacts cost. Strategies: (1) Increase memory (paradoxically cheaper—more memory = more CPU = faster execution). Test 128MB vs 512MB vs 1024MB; sometimes 512MB runs 3x faster at 2x cost = net savings. (2) Reduce cold starts: Use provisioned concurrency for critical paths, keep package small (<50MB), use layers wisely. (3) Optimize code: Initialize SDK clients outside handler, use connection pooling, cache configuration. (4) Async when possible: Don't wait for DynamoDB if not needed. (5) Language choice: Go/Rust have faster cold starts than Java/Python. Target: <200ms for most API handlers.
What is the optimal Lambda memory size?
Not always minimum. Memory determines CPU allocation proportionally. 1,769MB = 1 full vCPU. More memory = faster execution = potentially lower cost. Testing approach: Run same function at 128MB, 256MB, 512MB, 1024MB, 2048MB. Measure duration and calculate total GB-seconds. Often 512-1024MB is optimal (fast enough, not overpaying). Example: Function at 128MB takes 800ms = 0.1 GB-seconds. At 512MB takes 150ms = 0.075 GB-seconds. 512MB is 25% cheaper despite 4x memory. Tools: AWS Lambda Power Tuning (open source) automates this analysis.
Should I use REST API or HTTP API for API Gateway?
HTTP API is 70% cheaper and faster for most use cases. REST API: $3.50/million. Features: Request/response transformation, usage plans, API keys, caching, AWS WAF integration. HTTP API: $1.00/million. Features: OIDC/OAuth 2.0, CORS, auto-deploy, faster (lower latency). Use REST if: Need request transformation, API caching, usage plans with throttling, WAF protection. Use HTTP for: Simple proxy to Lambda, JWT authorization, cost-sensitive. Alternative: Lambda Function URLs (free, no API Gateway) for internal/service-to-service calls.
How does serverless scale during traffic spikes?
Lambda scales automatically: Concurrent executions increase as requests arrive. Default: 1,000 concurrent per region (can request increase). Provisioned concurrency: Pre-warmed instances (no cold start) at $0.015/GB-hour. Use for latency-sensitive endpoints. Burst limit: 3,000 (or 500-1,000 in some regions) initial burst, then 500/minute increase. Throttling: If limits exceeded, requests get 429 errors. Plan: Request limit increase before expected spike, use provisioned concurrency for critical paths, implement retry logic in clients. Compared to EC2: Auto-scaling takes 3-5 minutes; Lambda scales in milliseconds.
What are Lambda cold starts and how to reduce them?
Cold start: First invocation after idle period. Lambda loads code, initializes runtime. Duration: 100ms-2s depending on runtime, package size, VPC. Causes: No recent invocations (Lambda recycles containers after ~10-15 min idle), scaling up (new containers). Mitigation: (1) Keep package small (<50MB). (2) Use lighter runtimes (Node.js, Python, Go over Java, .NET). (3) Minimize dependencies. (4) Avoid VPC unless necessary (adds ~1s cold start). (5) Provisioned concurrency (eliminates cold starts, costs ~$0.015/GB-hour). (6) Keep functions warm (scheduled ping—not recommended, fragile). Most cold starts <500ms; only matters for synchronous user-facing APIs.
How do I calculate serverless cost per API call?
Total cost per call = (Lambda compute + Lambda invocation + API Gateway + Data transfer) / Invocations. Example: 1M API calls/month, 200ms duration, 512MB, 1KB response. Lambda invocation: (1M - 1M free) × $0.0000002 = $0 (within free tier). Lambda compute: 1M × 0.2s × 0.5GB = 100K GB-sec - 400K free = $0. API Gateway (HTTP): 1M × $0.000001 = $1. Data transfer: 1M × 1KB = 1GB × $0.09 = $0.09. Total: $1.09/month = $0.00000109/call. At 10M calls: Lambda $1.67, API $10, transfer $0.90 = $12.57 = $0.00000126/call. Economies of scale in invocation/compute; API Gateway scales linearly.
When should I consider containers (ECS/Fargate) instead of Lambda?
Consider containers if: (1) Execution >15 minutes (Lambda max). (2) Need persistent connections (WebSockets, long polling). (3) High, steady throughput (cheaper at scale). (4) Large package size (>250MB). (5) Specific runtime needs (custom binaries, OS packages). (6) Stateful processing. Fargate pricing: ~$0.04/vCPU-hour, ~$0.004/GB-hour. Example: Steady 50 req/sec needs ~2 vCPU. Fargate: ~$60/month. Lambda at 130M requests (50×60×60×24×30), 200ms, 512MB: ~$200/month. Fargate wins. Hybrid approach: Lambda for spiky, event-driven; Fargate for steady high-volume workloads.