AWS Use Cases | Decoding Your API Costs | Calculating “Maximum Cost Per API Call”
Introduction
In the dynamic world of cloud-native applications, understanding your AWS expenditure goes beyond just looking at the monthly bill. When your services are built on a multitude of APIs — from API Gateway routing requests to Lambda functions processing them, and DynamoDB or S3 storing data — pinpointing the cost of each individual API call becomes a crucial yet challenging task.
The Problem:
You’re running an application that interacts with various AWS services via their APIs. You know your overall AWS bill, but you’re blind to the specific financial impact of distinct API calls.
AWS provides high-level metrics like invocation counts, but it doesn’t give you a direct “$ per API call” metric. This makes it incredibly difficult to optimize your application’s architecture and code for cost efficiency, identify pricing outliers, and have granular unit economics.
Solution
We are going to assume some values and Calculate Each Service Expected cost using AWS Pricing Calculator. In addition we are going to validate our calculation using AWS reference documentation Matrix. So let’s Start All services one by one
- AWS Api Gateway
- AWS Cognito
- AWS Lambda
- AWS Cloudfront
- AWS S3 Bucket
- AWS Elastic Cache & VPC
- AWS Dynamo DB & Streams
- AWS Event Bridge Scheduler
Extras
- AWS SES
- AWS Api Gateway Caching
- AWS Cognito Social Media Login
- AWS WAF
- Cloudwatch Dashboard
Assumptions
These Assumptions are made from A gaming Application based on aws serverless architecture with leaderboard
Note : Reward Service not activated yet, so the calculations are omit from this
Note : These assumptions are considered for 30 days
- Total Active Players = 50000
- Total Game Play APIs = 20
- Single Player Play A Game With 20 Game Sessions per day
- Total API calls = 600,000,000 (600M)
- Total Lambda Invocation = 600M (Total APIs = Lambda Invocations)
- Average Lambda Invocation Duration = 1s
- Total RCUs = 870,000,000 (870M)
- Total WCUs = 450,000,000 (450M)
- Total Dynamo Storage = 15GB
- Total Dynamo Streams Request = 150000000
- Total Players Valkey Read & Writes (100 players in a Leaderboard) = 3000,000,000 (3000M)
- Total Valkey Storage = 100MB
- Total Lambda-GB-Second (used historical data) = 12,000,000 (12M)
- Total cloudfront calls(Game & Profile Image load) = 60,000,000 (60M)
Create a Estimation using Pricing calculator
Pricing calculator is providing by AWS to do the estimation. Follow the steps to create a cost estimation
- Billing and Cost Management > Pricing Calculator > Saved estimates>
2. create workload estimate — name as “cost-estimation”
3. add services to workload we created , I choose new services
Cost Estimation Break Down
Now I am going to break down each services and it’s estimated cost
1. API Gateway
- Pricing Model — Click Here
- We will consider
1.ApiGatewayRequest
2. DataTransfer-Out-Bytes
Note : There is no extra charge in API Gateway itself for using the Cognito authorizer. Reference
Note : There is a free 100 GB per month always for DataTransfer-Out-Bytes
Findings
- Total API calls = 600,000,000 (600M)
- First 333M Request = $ 4.25*333 = $1415.25
- Next 277M Request = $3.53*277 = $977.81
- Total size of data transfers = 2KB * 600 million = 1200 GB
- Amazon API Gateway data transfer charges = 1200 GB * $0.09 = $108
Calculation = First 333M Request + Next 277M Request = $2393.06 + $54 = $2497
Recommendations
- Use Http endpoint to reduce cost (for same request with 4KB $678.00)
- Use Caching to reduce the other backend invocation calls (Assume 80% use cache and 24hrs TTL & only 20% API need caching = 1GB | $39.42
- Also caching will reduce the cost of lambda,dynamo etc
- Minimize Data Transfer Out (DTO)
- Implement Throttling and Usage Plans
2. AWS Cognito
- Types : Essential , Lite, Plus
- Pricing Model — Click Here
- We will consider Cognito Essential
Note : For Social Media Login Integration no extra charges, it will consider same MAUs
Findings
- If we have below 10000 MAU monthly we will not get charged (Free Tier)
- Assume We have 50000 MAU monthly we will be cost around $600 (with free tier deduction)
- Assume We have 50000 MAU monthly we will be cost around $750 (without free tier)
Calculation : MAUs * Price per MAU = (50000) * 0.015 = $750
Recommendations
- Avoid Unnecessary MAU Triggers
: AdminGetUser
vs.ListUsers
: When querying user attributes, always preferListUsers
overAdminGetUser
if possible.AdminGetUser
marks a user as active for the month, contributing to your MAU count, whileListUsers
does not. - Token Refresh Strategy: Design your application to refresh tokens only when needed, and optimize the token expiration duration. While API Gateway validating a token doesn’t incur a new MAU, a token refresh does. Refresh tokens around 75% of their lifetime to maximize token duration while ensuring a smooth user experience. Don’t request new tokens if a valid one still exists.
- Delete Unused Accounts: If you have a significant number of inactive user accounts that are highly unlikely to return, consider implementing a policy to soft-delete or archive them after a long period of inactivity. While inactive users don’t directly cost, managing a very large pool can have other overheads. (Be careful with data retention policies and user privacy here).
3. AWS Lambda
- Pricing Model — Click Here
- We will consider Lambda function compute & Lambda function request
- Node research on Maximum time lambda call time & Memory allocation
Findings Need Update
- 1,000,000 Requests , 400,000 GB-Seconds free on the free tier (12 months)
- For our calculation perspective we will choose x86 architecture (To get the maximum)
- Total Lambda Invocation = 600M
- Invocation duration = 135 ms (Please get the highest invocation time lambda)
- For 128 MB price is in for ms $0.0000000021
- Cost GB-s = $0.0000166667
- 0.2 $ Per Million Request (we have 600M)
- GB-s = 600000000*0.125GB*1s = 82500000GBs
Calculation = Invocation Cost + Compute cost = (0.2 * 600) + (0.0000166667*75000000) = 120 + 1250 = $1470
Recommendations
- Switch to ARM Architecture (AWS Graviton2): AWS Graviton2 processors (ARM architecture) offer significantly better price-performance for Lambda functions, typically up to 34% better price performance compared to x86.
- Optimize Memory Allocation: Lambda allocates CPU power proportionally to memory. Sometimes, allocating more memory can actually reduce the total cost by making the function run faster, even though the per-GB-second rate is higher.
- Reduce Invocations: API Gateway Caching, Client-Side Caching
- Optimize Logging (CloudWatch Logs) : Reduce Log Verbosity: Log only essential information. High-volume Lambda invocations generate massive amounts of logs, and CloudWatch Logs can become a significant cost. Lambda Log Destinations: AWS recently introduced tiered pricing for Lambda logs in CloudWatch Logs (making it cheaper for high volumes) and also allowed sending logs directly to S3 or Kinesis Data Firehose (which can be cheaper for long-term storage or further processing).
Recommendations to Reduce Cost for Profile Image Handling
Optimize the Upload Process (Ingestion Costs: API Gateway, Lambda, S3 PUTs)
- Switch to Direct-to-S3 Uploads using Pre-Signed URLs:
- Why: This is the most cost-effective and scalable way to handle large file uploads to S3. It bypasses your API Gateway and Lambda for the actual image data transfer, reducing their invocation and data transfer costs for uploads.
- How:
- Your client requests a pre-signed URL from your
API Gateway -> Lambda
endpoint. This request is very small. - Your Lambda function generates a time-limited pre-signed PUT URL for an S3 object (e.g., in a “raw-uploads” bucket).
- The client then uploads the 5MB+ image directly to S3 using this pre-signed URL.
- Cost Savings:
- Eliminates Data Transfer In/Out through API Gateway for the bulky image.
- Eliminates Lambda invocation duration and GB-seconds for receiving the large image data.
- S3 PUT requests are very cheap ($0.005 per 1,000 requests for S3 Standard).
- Impact: If your upload volume is indeed very high, this will drastically reduce your API Gateway and Lambda costs related to the upload process itself.
4. AWS Cloudfront
- Pricing Model — Click Here
- We will be considering Total request & Data Transfer
Cloudfront Costs
Data Transfer Out (DTO):
- Free Tier: 1 TB/month.
- Beyond Free Tier: $0.120/GB (ap-southeast-1).
HTTP/HTTPS Requests:
- Free Tier: 10M requests/month.
- Beyond Free Tier: $0.0120 per 10,000 requests (HTTPS).
Findings
- Cloudfront calls for access game= 30,000,000 (30M)
- Cloudfront calls for load profile Image = 30,000,000 (30M)
- DTO for access game = 6GB
- DTO for load image (5KB) = 1500 GB
Calculation = Total Request Cost + Total DTO Cost = (0.012* 60000000)/10000 + (0.12*1500) = $252
1. Aggressive CloudFront Caching for Images:
Summary of Cost with 50KB Images and 95% Caching: Your estimated monthly CloudFront cost for 30,000,000 loads of 50KB images, assuming a 95% cache hit ratio and ap-southeast-1
pricing, would be approximately $180
5. AWS S3
- Pricing model — Click Here
Findings
- Game Access calls= 30,000,000 (30M)
- Profile Image Loading = 30,000,000 (30M)
- Monthly PUT Requests (Active users) = 50,000
- Total Storage (50KB) — $0.023/GB/month = 2.5 GB
- Beyond Free Tier: $0.005 per 1,000 put requests.
- Beyond Free Tier: $0.0004 per 1,000 get requests.
Calculation = Total Storage + Put Request + Get Request = 2.5*0.023 + (0.005*50000)/1000 + (0.0004*60000000)/1000 = $25
Recommendation
- Delete / archive unused object using S3 lifecycle policy
- After cloudfront caching the GET request will reduce
6. AWS Elastic Cache
- Price Model — Click Here
- We will consider Data stored & ElastiCache Processing Units (ECPUs)
- This service is not under free tier
Findings
- Total Hours in Month = 730 (AWS consideration)
- Assume we have 1 Leaderboard with 100 players
- Total Valkey ElastiCache Processing Units = 3000,000,000
- Minimum metered data storage: 100 MB per cache for ElastiCache Serverless for Valkey.
- Assume a record is 1kb from valkey
Calculation = Data stored + (ECPUs) = (0.1GB*730Hrs*$0.101) + (3000*0.0027) = 7.37 + 8.1 = $15.47
7. AWS Dynamo DB
- Pricing Model — Click Here
- We will consider RCU, WCU, Storage
Findings
- First 25 GB stored per month is free using the DynamoDB Standard table class. $0.285 per GB-month thereafter
- $0.71 per million write request units
- $0.1425 per million read request units
- Assume we consumed 15GB of Data = $0.285*15 = $4.275
- Assume we have 870M Read Capacity Units (average size 4kb with eventually consistence)= $0.1425*870 = $123.975
- Assume we have 450M Write Capacity Units = $0.71*450 = $319
Calculation = RCU cost + WCU cost + Storage Cost = $4.275 + $61.98 + $319 = $381.48
Dynamo DB Streams
Findings
- Stream request = 150,000,000 (150M)
Calculation = (150000000*0.0228)/100000 = $34.2
8. Event Bridge Scheduler
- Pricing Model — Click Here
- $1.25/million scheduled invocations per month, after 14 million free invocations per month
Extras
1. AWS SES
- Pricing Model — Click Here
- We will consider email sent
We are using SES along with cognito
- For my scenario i am only sending email, so i chose only that option, if you have extra feature you can get the calculation.
Findings
- Free tier customers receive up to 3,000 message charges free each month for the first 12 months after you start using SES.
- A customer could send 1,000 messages per month with Virtual Deliverability Manager enabled (2,000 message charges) and receive 1,000 emails (1,000 message charges) without exceeding the revised SES free tier limit of 3,000 message charges
- Assume if we sent 50000 emails monthly after free tier we will be cost around $5
Calculation : 50000 * 0.1$ per thousand email= 50000 * 0.1/1000 = $5
2. AWS WAF
- Pricing Model — Click Here
- WAF pricing is based on two main components:
- Web ACLs: The number of Web ACLs you create.
- Rules: The number of rules you associate with your Web ACLs.
- Requests: The number of web requests that AWS WAF inspects.
Assumptions for AWS WAF:
- Number of Web ACLs: 1 (You likely have one main Web ACL protecting your API Gateway).
- Number of Rules: 10 (A typical set of rules might include common OWASP top 10 protections, rate limiting, and custom rules).
- Requests Inspected: We’ll assume that all 600,000,000 API Gateway requests are inspected by WAF.
Findings:
- Web ACLs: $5.00 per Web ACL per month.
- Rules: $1.00 per rule per month.
- Requests: $0.60 per 1 million requests after the first 10 million (for regions in North America and Europe, other regions may vary slightly).
- Web ACL Cost: $5.00/Web ACL * 1 Web ACL = $5.00
- Rule Cost: $1.00/rule * 10 rules = $10.00
- Request Cost:
- Total Requests: 600,000,000
- First 10,000,000 requests are included (no charge).
- Remaining requests: 600,000,000–10,000,000 = 590,000,000
- Cost per million requests: $0.60
- Request Cost: (590,000,000 / 1,000,000) * $0.60 = 590 * $0.60 = $354.00
Calculation = Web ACL Cost + Rule Cost + Request Cost Total AWS WAF Cost = $5.00 + $10.00 + $354.00 = $369.00
Recommendations for AWS WAF:
- Optimize Rule Sets: Regularly review and optimize your WAF rules to ensure they are effective and not redundant. Removing unnecessary rules can slightly reduce costs.
- Use Rate-Based Rules: Implement rate-based rules to automatically block or count requests from IP addresses that exceed a specified threshold. This can help mitigate DDoS attacks and reduce the number of requests processed by downstream services, potentially saving costs on API Gateway and Lambda.
- Consider AWS Firewall Manager: For larger organizations with multiple accounts and applications, AWS Firewall Manager can help centrally manage WAF rules across accounts, potentially leading to more consistent security and cost optimization.
- Leverage AWS WAF Logging: Use AWS WAF logging (to S3 or CloudWatch Logs) to analyze WAF activity, identify common attack patterns, and refine your rules for better protection and efficiency. Be mindful of the cost of storing these logs.
Conclusion
- Biggest Cost Contributors: API Gateway, Lambda, DynamoDB, and Cognito.
- Potential Savings: With optimizations, total cost could drop to ~$3,500/month.
- Next Steps:
- Test ARM Lambda and HTTP API Gateway for immediate savings.
- Monitor DynamoDB RCU/WCU usage and adjust capacity.
- Set up Cost Explorer for real-time tracking.