AWS Lambda is cheap per request, which is exactly why its cost is easy to ignore until the bill arrives. The good news: most serverless cost problems come from a short list of causes, and most fixes are small configuration or design changes. This guide walks through where the money goes and what to change, in roughly the order we look at it in a cost optimization engagement.
How Lambda pricing works
Lambda bills two things for each function:
- Requests — a fixed price per million invocations.
- Duration — billed in GB-seconds: the memory you configure multiplied by how long each invocation runs, rounded up to the millisecond.
A few details matter more than they look:
- Memory also buys CPU. Lambda allocates CPU in proportion to memory; at 1,769 MB a function gets the equivalent of one full vCPU. A CPU-bound function often finishes so much faster with more memory that it costs the same or less.
- Architecture changes the rate. Functions on arm64 (AWS Graviton) are billed at a lower price per GB-second than x86_64 — roughly 20% lower — and often perform as well or better for Node.js and Python.
- Initialization is billed. The init phase of a cold start counts toward billed duration, so heavy start-up code is a cost, not just a latency, problem.
- The function is rarely the whole bill. API Gateway, CloudWatch Logs, NAT Gateway, data transfer, DynamoDB, and Step Functions usually sit next to Lambda on the invoice — and are often bigger.
1. Right-size memory with measurements
Most functions run with whatever memory the template defaulted to. The right setting depends on whether the work is CPU-bound, I/O-bound, or memory-bound, and the only reliable way to find it is to measure.
AWS Lambda Power Tuning is an open-source Step Functions workflow that invokes a function at several memory sizes and charts cost and duration. Typical results:
- I/O-bound functions (waiting on DynamoDB, HTTP calls) barely speed up with more memory — the cheapest setting that keeps latency acceptable wins, often 256–512 MB.
- CPU-bound functions (parsing, compression, image work, heavy JSON) can get dramatically faster with more memory, so a larger setting may cost the same and cut latency.
- Functions near their memory limit may spend time in garbage collection; a little more memory can reduce duration.
AWS Compute Optimizer also produces Lambda memory recommendations from real invocation history, which is a good starting point across many functions.
2. Move to arm64 (Graviton)
For Node.js, Python, Java, .NET, and Go functions without native x86-only dependencies, switching the architecture to arm64 is usually a one-line change in infrastructure code. Test it — native modules (image libraries, some database drivers) need arm64 builds — and compare duration before and after. Combined with the lower GB-second price, it is one of the best effort-to-savings ratios available.
3. Invoke less
The cheapest invocation is the one that doesn't happen.
- Batch your event sources. For SQS, Kinesis, and DynamoDB Streams, increase the batch size and use a batching window so one invocation handles many records. Use partial batch responses so a single bad record doesn't force the whole batch to retry.
- Filter at the source. Event source mapping filters and EventBridge rule patterns discard events your function would ignore anyway — before you pay to invoke it.
- Replace polling with events. A scheduled function that checks for changes every minute runs over 40,000 times a month even when nothing changed. S3 notifications, DynamoDB Streams, or EventBridge events trigger work only when there is work.
- Use direct service integrations. Step Functions can call DynamoDB, SQS, SNS, EventBridge, and many other APIs directly; API Gateway can integrate with some services without a function in between. A Lambda function that only forwards a request is pure overhead.
4. Choose cheaper front doors
- HTTP APIs instead of REST APIs. Amazon API Gateway HTTP APIs cost significantly less per request than REST APIs and have lower latency. If you don't need REST-only features such as usage plans, request validation, or API caching, they are the better default. See HTTP API vs REST API.
- Function URLs for simple endpoints. Webhooks and internal endpoints can use Lambda function URLs, which have no per-request charge beyond Lambda itself.
- Cache what doesn't change. CloudFront in front of read-heavy endpoints can serve repeated responses without invoking anything.
5. Control logging costs
CloudWatch Logs ingestion is frequently the largest line item in a serverless account. Common fixes:
- Set retention on every log group. Log groups created implicitly by Lambda never expire by default. Define them in infrastructure code with a retention period.
- Log at the right level. Use structured JSON logs and set the level per environment —
DEBUGin production for every request is expensive. Lambda's advanced logging controls let you filter by level without code changes. - Don't log payloads by default. Large request and response bodies multiply ingestion volume and can leak personal data.
- Consider the Infrequent Access log class for logs you keep for audit but rarely query.
6. Look at the neighbors
- NAT Gateway. Functions in a VPC that call AWS services through a NAT Gateway pay per GB processed. VPC endpoints (gateway endpoints for S3 and DynamoDB are free) remove most of that traffic. Better still, don't attach functions to a VPC unless they need private resources.
- DynamoDB capacity mode. On-demand is ideal for spiky or unknown traffic; steady, predictable traffic is often cheaper on provisioned capacity with auto scaling. Review unused GSIs — each one duplicates writes.
- Step Functions workflow type. Standard workflows bill per state transition and suit long-running processes; high-volume, short workflows are usually much cheaper as Express workflows.
- Provisioned concurrency. It removes cold starts but bills for every hour it is configured. Make sure it is only on latency-critical paths, sized from real data, and scaled down outside peak hours.
7. Keep the savings
Cost regressions creep back. Put guardrails in place:
- AWS Budgets with alerts per account and per workload tag.
- Cost Anomaly Detection for sudden spikes.
- Cost review as part of architecture decisions — every new design should state its expected monthly cost.
A short checklist
- Tag every resource with a workload and environment.
- Run Power Tuning on your highest-cost functions.
- Switch compatible functions to arm64.
- Batch and filter event sources.
- Replace REST APIs with HTTP APIs where features allow.
- Set log retention and levels everywhere.
- Add VPC endpoints or remove unnecessary VPC attachments.
- Review DynamoDB capacity mode and indexes.
- Set budgets and anomaly alerts.
If you want an expert pass over your account, our serverless cost optimization service measures, prioritizes, and implements these changes as reviewable Change Plans.