Serverless Cost Estimation Template
Template for estimating serverless costs per workload: invocation-based pricing, memory-duration calculation, data transfer, API Gateway, Step Functions, and hidden costs. Includes cost optimization strategies and monthly budget projections.
Note: This guide follows English-language naming conventions and terminology standards common in international development teams. Examples use English identifiers and comments to maximize compatibility across codebases and tooling.
Overview
This template helps teams estimate serverless costs per workload before deployment and track actual costs against projections. Serverless pricing is based on invocations, execution duration, memory allocation, and data transfer. Underestimating any of these factors leads to budget overruns.
1. Pricing Model Reference
1.1 AWS Lambda Pricing (us-east-1)
Component | Free tier | Price after free tier
───────────────────────┼─────────────────┼──────────────────────────
Requests | 1M/month free | $0.20 per 1M requests
Compute (GB-second) | 400K/month free | $0.0000166667 per GB-second
Provisioned concurrency| None | $0.0000049700 per GB-second
Data transfer (in) | Always free | $0.00 per GB
Data transfer (out) | 100GB/month free| $0.09 per GB
1.2 Azure Functions Pricing
Component | Free tier | Price after free tier
───────────────────────┼─────────────────┼──────────────────────────
Executions | 1M/month free | $0.20 per 1M executions
GB-seconds | 400K/month free | $0.000016 per GB-second
Premium plan (vCPU) | None | $0.192/hour per vCPU
Premium plan (memory) | None | $0.0139/hour per GB
1.3 GCP Cloud Functions Pricing
Component | Free tier | Price after free tier
───────────────────────┼─────────────────┼──────────────────────────
Invocations | 2M/month free | $0.40 per 1M invocations
GB-seconds | 400K/month free | $0.0000025 per GHz-second
| | + $0.0000025 per GB-second
Data transfer (out) | 100GB/month free| $0.12 per GB
2. Cost Calculation Formula
2.1 Lambda Compute Cost
Monthly compute cost = (invocations × duration_seconds × memory_GB) × price_per_GB_second
Example:
Invocations: 10M/month
Duration: 200ms average (0.2 seconds)
Memory: 512MB (0.5 GB)
GB-seconds = 10,000,000 × 0.2 × 0.5 = 1,000,000 GB-seconds
Cost = 1,000,000 × $0.0000166667 = $16.67/month
2.2 Lambda Request Cost
Monthly request cost = (invocations - free_tier) × price_per_million / 1,000,000
Example:
Invocations: 10M/month
Free tier: 1M
Billable = 10,000,000 - 1,000,000 = 9,000,000
Cost = 9 × $0.20 = $1.80/month
2.3 Total Lambda Cost
def estimate_lambda_cost(
invocations: int,
avg_duration_ms: float,
memory_mb: int,
provisioned_concurrency_gb_seconds: int = 0,
data_transfer_out_gb: float = 0,
) -> dict:
"""Estimate monthly AWS Lambda cost."""
# Compute cost
duration_s = avg_duration_ms / 1000
memory_gb = memory_mb / 1024
gb_seconds = invocations * duration_s * memory_gb
free_tier_gb_seconds = 400_000
billable_gb_seconds = max(gb_seconds - free_tier_gb_seconds, 0)
compute_cost = billable_gb_seconds * 0.0000166667
# Request cost
free_tier_requests = 1_000_000
billable_requests = max(invocations - free_tier_requests, 0)
request_cost = (billable_requests / 1_000_000) * 0.20
# Provisioned concurrency cost
pc_cost = provisioned_concurrency_gb_seconds * 0.0000049700
# Data transfer cost
free_tier_data = 100
billable_data = max(data_transfer_out_gb - free_tier_data, 0)
data_cost = billable_data * 0.09
total = compute_cost + request_cost + pc_cost + data_cost
return {
"compute_cost": round(compute_cost, 2),
"request_cost": round(request_cost, 2),
"provisioned_concurrency_cost": round(pc_cost, 2),
"data_transfer_cost": round(data_cost, 2),
"total_monthly_cost": round(total, 2),
"gb_seconds": int(gb_seconds),
}
# Example usage
cost = estimate_lambda_cost(
invocations=10_000_000,
avg_duration_ms=200,
memory_mb=512,
)
print(f"Total monthly cost: ${cost['total_monthly_cost']}")
3. Workload Estimation Template
3.1 Per-Function Estimation
Function: processOrder
──────────────────────────────────────────────────
Metric | Value | Source
───────────────────────┼────────────────┼──────────────────
Expected invocations | 10M/month | Traffic forecast
Average duration | 200ms | Load testing
Memory allocation | 512MB | Power Tuning
p99 duration | 500ms | Load testing
Data transfer out | 5GB/month | Payload size × invocations
Provisioned concurrency| 10 concurrent | Latency requirement
Estimated monthly cost:
Compute: $16.67
Requests: $1.80
PC: $21.77 (10 × 0.5GB × 30 days × 24h × 3600s × $0.00000497)
Data: $0.00 (under 100GB free tier)
Total: $40.24/month
3.2 Multi-Function Estimation
workloads = [
{"name": "processOrder", "invocations": 10_000_000, "duration_ms": 200, "memory_mb": 512},
{"name": "sendEmail", "invocations": 5_000_000, "duration_ms": 100, "memory_mb": 256},
{"name": "generateReport", "invocations": 100_000, "duration_ms": 5000, "memory_mb": 2048},
{"name": "webhookHandler", "invocations": 50_000_000, "duration_ms": 50, "memory_mb": 128},
]
total_cost = 0
for w in workloads:
cost = estimate_lambda_cost(w["invocations"], w["duration_ms"], w["memory_mb"])
print(f"{w['name']}: ${cost['total_monthly_cost']}/month")
total_cost += cost["total_monthly_cost"]
print(f"\nTotal monthly cost: ${total_cost:.2f}")
4. Hidden Costs
4.1 API Gateway Costs
Component | Free tier | Price
───────────────────────┼─────────────────┼──────────────────────
Requests (HTTP API) | 300M/month | $1.00 per 1M
Requests (REST API) | 1M/month (12mo) | $3.50 per 1M
Data transfer out | 100GB/month | $0.09 per GB
WebSocket connections | 1M/month (12mo) | $0.80 per 1M
4.2 Step Functions Costs
Type | Free tier | Price per transition
───────────────────────┼─────────────────┼──────────────────────
Standard workflow | 4K/month (12mo) | $0.025 per 1,000
Express workflow | 4K/month (12mo) | $1.00 per 1M invocations
+ $0.03 per 1K GB-seconds
4.3 Common Hidden Costs
Hidden cost | Impact | Mitigation
───────────────────────────┼───────────────┼──────────────────────────
CloudWatch Logs ingestion | $0.50 per GB | Set log retention, use sampling
CloudWatch Logs storage | $0.03 per GB | Set TTL on log groups
X-Ray tracing | $0.50 per 1M | Sample traces (not 100%)
SQS requests | $0.40 per 1M | Batch messages
S3 PUT/GET requests | $0.005 per 1K | Batch S3 operations
Secrets Manager | $0.40 per sec | Use SSM Parameter Store (free)
DynamoDB read/write units | Per-request | Use on-demand capacity
NAT Gateway | $0.045 per GB | Use VPC endpoints where possible
5. Cost Optimization Strategies
5.1 Memory Tuning
Memory | CPU share | Cost per 1M invocations (200ms)
──────────┼──────────────┼──────────────────────────────────
128MB | 1/10 vCPU | $0.21
256MB | 1/5 vCPU | $0.43
512MB | 2/5 vCPU | $0.85
1024MB | 1 vCPU | $1.71
2048MB | 2 vCPU | $3.41
Optimal: Choose the memory where cost × duration is minimized.
Higher memory = faster execution = fewer GB-seconds.
5.2 Optimization Checklist
- Profile memory with AWS Lambda Power Tuning
- Reduce cold starts (provisioned concurrency only for critical paths)
- Batch SQS messages (reduce invocation count)
- Use EventBridge filtering (reduce unnecessary invocations)
- Set log retention to 7-30 days (not infinite)
- Use SSM Parameter Store instead of Secrets Manager for non-secret config
- Use VPC endpoints to avoid NAT Gateway charges
- Strip dev dependencies from deployment package
- Use Lambda Layers for shared dependencies
- Monitor AWS Cost Explorer weekly
FAQ
How accurate are serverless cost estimates?
Estimates are accurate within 10-20% when based on real load testing data. The biggest source of error is underestimating invocation count — production traffic often exceeds forecasts by 2-3x during peak events. Always model worst-case scenarios (3x expected traffic) and set billing alarms at 50%, 75%, and 100% of budget. Review actual costs against estimates monthly and adjust projections based on real data.
When does serverless become more expensive than EC2?
Serverless is cheaper than EC2 for workloads with low or bursty traffic (utilization < 40%). At sustained high traffic (> 70% utilization), EC2 or Fargate becomes cheaper because you pay for capacity rather than per-invocation. The break-even point depends on memory, duration, and invocation count. As a rule of thumb: if your function runs continuously (every second), serverless costs 2-3x more than an equivalent EC2 instance. Use the cost calculator to find your specific break-even point.
How do I reduce CloudWatch Logs costs?
Set log retention to 7-30 days instead of “never expire.” Use log levels (INFO in production, DEBUG only in dev). Remove verbose logging from hot paths. Use sampling for high-volume logs. Consider shipping logs to S3 with Glacier archival for long-term storage at $0.004/GB instead of CloudWatch’s $0.03/GB. A single function generating 1KB of logs per invocation at 10M invocations/month costs $5/month in ingestion alone.
What is the cost impact of provisioned concurrency?
Provisioned concurrency charges you for reserved capacity even when not in use. At 10 concurrent executions with 512MB memory, that’s 10 × 0.5GB × 30 days × 24h × 3600s × $0.00000497 = $21.77/month. Compare this to on-demand cost for the same traffic. Only use provisioned concurrency for functions with strict latency SLAs where cold starts are unacceptable. For dev/staging, always use on-demand.
How do I set up billing alerts for serverless workloads?
Use AWS Budgets to set cost alerts at 50%, 75%, and 100% of your monthly budget. Create per-function cost allocation tags and use Cost Explorer to filter by tag. Set up anomaly detection in AWS Cost Anomaly Detection for unexpected spikes. For granular monitoring, use CloudWatch custom metrics to emit estimated cost per function invocation and alarm on cost anomalies.
How do I estimate costs for Step Functions workflows?
Step Functions charges per state transition. Standard workflows cost $0.025 per 1,000 transitions. Express workflows cost $1.00 per 1M invocations plus $0.03 per 1K GB-seconds. For a workflow with 5 steps running 100K times/month, standard costs $12.50/month. Express costs $0.10/month for invocations plus compute charges. Use standard for long-running workflows (> 5 minutes) and express for short, high-volume workflows.
See Also
Related Resources
Serverless Function Deployment Checklist
Pre-deploy and post-deploy checklist for serverless functions (AWS Lambda, Azure Functions, GCP Cloud Functions): IAM roles, environment variables, memory sizing, timeout config, logging, alarms, and rollback procedures.
DocServerless Cold Start Runbook
Runbook for diagnosing and mitigating serverless cold starts: causes, measurement, optimization strategies (provisioned concurrency, warmers, initialization tuning), and monitoring with code examples for AWS Lambda, Azure, and GCP.
DocServerless Security Checklist
Security hardening checklist for serverless functions: IAM least privilege, secret management, input validation, dependency scanning, network isolation, logging, and compliance with code examples for AWS Lambda, Azure, and GCP.