AWS Serverless Cost Optimization: Lambda, API Gateway, and DynamoDB
Review memory allocation, API choices, caching, DynamoDB capacity, and billing alerts to find AWS serverless cost reductions worth testing.
Cost figures and durations below are illustrative planning estimates, not current quotes. Check provider pricing, project scope, and local hiring costs before using them in a budget.
AWS serverless costs depend on request volume, execution time, memory, storage, and data transfer. Use billing data and workload measurements to identify which settings are worth changing.
Lambda Cost Optimization Best Practices
Right-Size Memory Allocation
Lambda's pricing model ties CPU to memory: more memory means more CPU allocation, which often means shorter execution time. The cost formula is duration × memory, which means the cheapest configuration is not always the lowest memory setting.
Use AWS Lambda Power Tuning (an open-source Step Functions state machine) to run your function at multiple memory configurations and identify the minimum-cost point. Many functions are cheapest at 512MB or 1GB, not at the 128MB default.
Reduce Package Size to Shrink Init Time
Initialization time is billed. A function that takes 800ms to cold-start before handling its first invocation is paying for that 800ms. Lean packages — under 5MB — cold-start meaningfully faster than 80MB packages.
Use a bundler. Tree-shake aggressively. Audit and remove unused dependencies every quarter.
Hoist Expensive Initialization Outside the Handler
Database connections, SDK clients, and configuration fetches that happen inside the handler run on every invocation. Moving them to module scope means they run once per container lifecycle and are reused across warm invocations.
At high invocation rates, this compounds significantly. A database connection that takes 40ms to establish, called 10 million times per month, adds 400,000 seconds of billed duration — roughly $6.50 at standard Lambda pricing. Entirely avoidable.
Use Graviton (ARM) Architecture
Lambda functions on Graviton2 cost approximately 20% less per GB-second than x86 equivalents. For most Node.js and Python functions, the migration is a single architecture flag change with no code modification. Run a load test on both architectures before switching.
Apply Compute Savings Plans
AWS Compute Savings Plans apply to Lambda usage and deliver up to 17% discount on a one-year commitment, or more on three-year. Unlike Reserved Instances, Savings Plans are flexible across function configurations, regions, and runtimes. Compare the commitment with your expected eligible usage before choosing a Savings Plan.
API Gateway Cost Optimization
Migrate REST APIs to HTTP APIs
REST API Gateway costs roughly $3.50 per million requests. HTTP API Gateway costs $1.00 per million requests — a 70% reduction. HTTP APIs support most common use cases: Lambda integrations, JWT authorization, CORS, custom domains, and stage variables.
The main capability gaps are usage plans, API keys (for rate limiting by key), and some advanced request/response transformations. If you do not need those features, migrating existing REST APIs to HTTP APIs is one of the highest-ROI changes you can make to an AWS serverless bill.
Enable Caching for Stable Endpoints
API Gateway REST API caching uses a dedicated cache instance for a configurable TTL. For endpoints whose output does not change on every request — product listings, configuration data, reference lookups — caching eliminates Lambda invocations entirely.
A cache hit costs nothing in Lambda compute. Cache instances have an hourly charge based on capacity, and API Gateway request charges still apply. Compare the current regional cache price with the backend work avoided.
Set Throttling Limits
Unlimited Lambda invocations driven by a misbehaving client or a traffic spike can produce a bill with no corresponding revenue. API Gateway throttling (burst and rate limits at the stage or method level) caps exposure. This is not just a cost control measure — it is also a reliability practice.
DynamoDB Cost Optimization Best Practices
Switch Predictable Workloads to Provisioned Capacity
DynamoDB on-demand bills read and write requests, while provisioned capacity bills reserved throughput. Compare current regional prices and expected utilization; provisioned capacity can be economical when demand is predictable and capacity is well used.
Use On-Demand for: new tables with unknown traffic patterns, batch jobs that run irregularly, and development/staging environments.
Use Provisioned with auto-scaling for: any table that has consistent baseline traffic.
Enable DynamoDB Standard-IA for Cold Data
DynamoDB Standard-Infrequent Access storage class reduces storage costs by roughly 60% for tables that are not accessed frequently. Standard-IA has higher read and write prices than Standard, so compare total storage and request costs for the table.
Avoid Scans
Table scans consume read capacity units proportional to the table size, not the result set size. A scan on a 10GB table to find 50 records consumes the same RCUs as reading the entire 10GB. Scans on large tables can spike costs significantly.
Design access patterns first, then design your table. Global Secondary Indexes add write cost but eliminate scan cost — usually a favorable trade.
Cross-Cutting Practices
Tag Everything
Cost allocation tags let you slice your AWS bill by team, product, environment, and feature. Without tags, you are optimizing in the dark. Apply tags at the resource level for Lambda functions, API Gateways, and DynamoDB tables, then use Cost Explorer tag-based grouping to see where money is actually going.
Set Budget Alerts at Multiple Thresholds
AWS Budgets can notify you when actual or forecasted spend crosses a threshold. Set alerts at 50%, 80%, and 100% of your monthly target for each service. This catches runaway costs before the bill closes, when you still have options.
Review Costs Monthly, Not Quarterly
Cloud costs drift. A change deployed on day 3 of the month shows up in the bill 28 days later if you only check at month end. Weekly Cost Explorer reviews can surface changes before month end. Monthly deep dives are where you make structural changes.