How-to guide

Keep serverless costs predictable on AWS

A practical guide to capping Lambda, API Gateway, DynamoDB and logging costs on AWS so a traffic spike or a retry loop doesn't become a surprise bill.

By the Cloudverg engineering team · · 6 min read

Why this matters

Serverless pricing is per request, per millisecond and per gigabyte moved. That's great when traffic is small and steady. It also means cost follows behavior, and behavior can change without anyone deploying anything. A retry storm, a bot hitting a public endpoint, or a function that suddenly takes three times longer because a downstream API got slow will all show up on your AWS bill within hours.

There's no server to look at, so teams often don't notice until the invoice arrives. The costs also hide in odd places. A Lambda function may be cheap on its own while the CloudWatch Logs it writes, the NAT gateway it talks through, and the DynamoDB writes it triggers cost several times more.

Predictable doesn't mean low. It means you know the shape of the bill, you've set limits where AWS lets you, and you get an alert while the problem is still small. This guide covers the controls that matter most for Lambda, API Gateway, DynamoDB, SQS and logging.

Before you start

You need access to Cost Explorer, AWS Budgets, and the Lambda and CloudWatch consoles. Read-only on billing is enough for the investigation. Making the changes needs permission to edit functions, API stages and alarms.

Pick one workload to start with, not the whole account. Choose the one with the most unpredictable traffic, usually a public API or an event-driven pipeline fed by queues or S3 uploads. Write down what it does, which functions and tables belong to it, and who owns it.

Check that your resources carry a tag for the application or team, and that the tag is activated as a cost allocation tag in the Billing console. Without that, Cost Explorer can only show you service totals, and you can't tell which function drove a jump. Also note whether your functions run inside a VPC. If they do, find out how they reach the internet, because NAT gateway charges are a common surprise for serverless teams.

How to do it

Work through the steps below in order. The first three give you visibility, the next three put limits in place, and the last ones remove waste that tends to creep back.

Don't try to tune everything at once. Make one change, watch a few days of data, then move on. Serverless costs react quickly to configuration changes, so you'll see the effect without waiting for a full billing cycle.

  1. Tag the workload and activate the tag. Apply an application or team tag to every function, table, queue and API in the workload. Activate it as a cost allocation tag in the Billing console so Cost Explorer can group spend by it.
  2. Map where the money goes. In Cost Explorer, filter to your tag and group by service, then by usage type. Note the top three line items. For serverless these are often Lambda duration, CloudWatch Logs ingestion and DynamoDB or data transfer.
  3. Create a budget with alerts. In AWS Budgets, create a monthly cost budget scoped to the tag. Add alerts at forecasted and actual thresholds, and send them to a shared channel or distribution list, not one person's inbox.
  4. Cap concurrency on functions that can run away. Set reserved concurrency on functions triggered by queues, streams or public endpoints. Pick a number from observed peak ConcurrentExecutions plus headroom, so a loop or flood hits a ceiling instead of your wallet.
  5. Throttle the front door. On API Gateway, set stage-level and per-route throttling, and add usage plans with quotas if you issue API keys. For public endpoints, put AWS WAF rate-based rules in front to cut off bots before they invoke anything.
  6. Make retries finite. Set maximum receive count on SQS queues with a dead-letter queue attached. For asynchronous Lambda invocations, lower the retry attempts and maximum event age, and configure an on-failure destination so bad events stop circulating.
  7. Right-size memory and timeout. Run the Lambda Power Tuning tool or compare Duration at a few memory settings under real load. Set the timeout close to the longest legitimate run, so a hung downstream call can't bill you for fifteen minutes.
  8. Set log retention and trim verbosity. Set a retention period on every CloudWatch log group, such as 14 or 30 days. Lower the log level in production and drop payload dumps. Move long-term logs to S3 if you need them.
  9. Review table and network choices. Check each DynamoDB table for on-demand versus provisioned fit now that you have usage history. Replace NAT gateway paths with VPC endpoints for S3 and DynamoDB where functions sit in a VPC.

How to check it worked

Give each change a few days, then compare. In Cost Explorer, group by service and then by your application tag, and look at daily granularity for the workload you changed. You're looking for a flatter line, not a lower one. Spikes should now be smaller and rarer, and any that remain should match a known event like a release or a marketing push.

Open the CloudWatch metrics for Lambda: Invocations, Duration, Errors, Throttles and ConcurrentExecutions. Cost per invocation is roughly duration times memory, so a drop in average duration after tuning memory should be visible directly. Throttles should only appear if you set a concurrency limit and traffic hit it, which means the limit did its job.

Test the guardrails on purpose. Send a burst of requests at a staging endpoint and confirm the API Gateway throttle returns 429 responses. Push a bad message through the queue and confirm it lands in the dead-letter queue after the set number of attempts instead of looping. Trigger a budget alert threshold in a test account if you can, so you know the email or chat message actually arrives and reaches someone who will act on it.

Mistakes to avoid

The most common one is raising Lambda memory to the maximum and forgetting about it. More memory means more CPU, so some functions finish faster and cost less, but many don't. Measure with real traffic, or use the AWS Lambda Power Tuning tool, and set the value the data supports.

Another is leaving log retention on its default of never expire. Every function writes to a CloudWatch log group, and those groups grow forever unless you set retention. Verbose debug logging in production makes it worse, since ingestion is charged per gigabyte. Log what you need to diagnose a failure, sample the rest, and set a retention period on every group.

Teams also set a reserved concurrency limit on one function and assume the account is protected. It isn't. All functions in a Region share the account concurrency quota, so one runaway function can starve the others unless each critical one has its own limit. And be careful the other way too: a limit set too low turns a cost problem into an outage.

Finally, don't ignore the services around Lambda. DynamoDB on-demand mode is simple and safe for unpredictable traffic, but a steady, high-volume table is often cheaper on provisioned capacity with auto scaling. Review that choice once traffic has settled into a pattern. And watch data transfer: functions that pull large objects across Regions or out through a NAT gateway can cost more than the compute.

Frequently asked questions

Does AWS let me set a hard spending cap on serverless?

No. AWS Budgets can alert you and, with budget actions, apply restrictive IAM or service control policies, but there's no switch that stops all spending at a dollar figure. The practical cap is the set of limits you configure yourself: reserved concurrency, API throttling, queue retry limits and dead-letter queues.

Is serverless always cheaper than running containers or servers?

No. It's usually cheaper for spiky or low-volume workloads because you pay nothing when idle. A steady, high-volume workload that keeps functions busy around the clock can cost less on containers or instances, especially with a commitment discount, so compare once your traffic pattern is clear.

What's the first thing to check when a Lambda bill jumps?

Look at the Invocations and Duration metrics for the day it started, then at CloudWatch Logs ingestion. A jump in invocations points to a trigger loop or traffic flood. A jump in duration points to a slow dependency or a code change. Check recent deployments and the error rate alongside both.

Monthly plan

Want help with this? Cloud cost audit

$4,500/month

2 weeks · Starts within 10 business days

A two-week, read-only review of your AWS, Azure, Google Cloud or Oracle bill, with every saving ranked by dollars per month.

Renews monthly. Cancel any time.
Prefer to pay once?

Pay $4,500 once for one month of the Cloud cost audit. It ends on its own, nothing renews.

What is included

Related guides

More cloud costs guides   Cloud cost audit

Smallest way to start

Get a senior architect on your problem this week

Ninety minutes a month on the cloud, AI or security question that is blocking you, a pre-read of your material and a written summary within two business days.

$450/month

Renews monthly. Cancel any time.
Prefer to pay once?

Pay $450 once for one month of the Expert architecture session. It ends on its own, nothing renews.