All posts
Security

AWS WAF or Cloudflare vs Filtering Bad Traffic in Your Code: What a Bot Flood Costs on Serverless

By

November 2, 2026

7 min read

One post gets shared, the bots arrive, and every request runs your code: API Gateway charges for it, Lambda bills the time, and the whole Region shares 1,000 concurrent executions. Worked numbers for a six-hour flood with no firewall, with filtering in code, and with AWS WAF or Cloudflare in front, and why the third is about availability more than money.

AWS WAF
Cloudflare
Bots
Serverless
Cost Optimization

Bad traffic rarely arrives slowly. A post reaches a big forum, a scraper finds your API, an AI crawler decides to re-read the whole site, or someone points a load tool at a search endpoint. Traffic goes from tens of requests a second to thousands in minutes, and on serverless every one of them is a billed invocation of your code.

The question is not whether to reject that traffic, but where: inside your function, after it has already been paid for, or in front of it.

The worked example

A dynamic route behind API Gateway (HTTP API) and Lambda, 512 MB, 300 ms per request including a database call. A flood of 2,000 requests a second for six hours: 43.2 million requests. US East prices: HTTP API $1.00 per million, Lambda $0.20 per million requests plus $0.0000166667 per GB-second, CloudWatch Logs $0.50 per GB ingested.

No filtering

Rejected in code in 5 ms

Blocked at the edge (WAF or Cloudflare)

API Gateway

$43.20

$43.20

$0 (never reaches it)

Lambda requests

$8.64

$8.64

$0

Lambda duration

$108.00

$1.80

$0

Logs at ~1 KB a request

$21.60

$21.60 if you log rejections

$0 (WAF samples instead)

Firewall

—

—

AWS WAF on metered pricing about $26 at $0.60 per million; $0 of plan allowance on a CloudFront flat-rate plan, where WAF-blocked requests don't count; Cloudflare unmetered

Database

43.2 million queries

None, if you reject before the query

None

Concurrency used

~600 of the Region's 1,000

~10

0

Six-hour total, before the database

~$181

~$75

$0–$26 plus plan

Over a weekend the no-filtering column is about $1,450 before the database bill, which is often the larger line: an auto-scaling database scales up to serve the flood, and a provisioned one simply slows down for everyone.

Want a second opinion on this for your system?

Book a free 30-minute call. You'll leave knowing what we'd do first and roughly what it costs, whether or not you hire us.

The part that isn't money: concurrency

Lambda gives an account 1,000 concurrent executions per Region by default, shared by every function, and a requests-per-second limit of ten times that. Concurrency is requests per second times duration: 2,000 a second at 300 ms is 600. Double the flood and it is 1,200, past the limit, and Lambda starts throttling every function in the Region, including checkout, login and the webhook handler for your payment provider. The bot doesn't need to break anything; it only has to use the pool.

  • Reserved concurrency on public-facing functions caps how much of the pool they can take, so a flood throttles that route and not your webhooks.

  • Filtering in code helps here: a 5 ms rejection uses a sixtieth of the concurrency of a 300 ms request.

  • Blocking at the edge helps most: the request never invokes anything.

Why filtering in code isn't enough on its own

  • You still pay per request at the front door (API Gateway, load balancer LCUs) and per invocation.

  • Your code sees one request at a time. Rate limits need shared state, which means a cache or table on every request, which is load in its own right.

  • Modern bots look like browsers: residential IPs, real user agents, headless Chrome. Edge services see traffic across many sites and maintain reputation lists and bot signatures; your function sees one request.

  • Code is the right place for business rules: per-account quotas, per-user AI spend caps, abuse of one feature. Keep those there.

AWS WAF or Cloudflare

AWS WAF (on CloudFront, ALB or API Gateway)

Cloudflare

Price

$5 per web ACL, $1 per rule, $0.60 per million requests; Bot Control $10 a month plus per-request charges after its free allowance. Included in CloudFront flat-rate plans ($0, $15, $200, $1,000 a month)

Free plan with unmetered DDoS protection; Pro from $20 a month billed annually

Rate limiting

Rate-based rules per IP or other keys, windows from 1 to 10 minutes, thresholds from 10 requests

Free plan: one rule, counted by IP over 10 seconds; more rules and keys on paid plans

Bots

Bot Control: verified crawlers allowed, unverified bots and HTTP libraries blocked

Bot management by plan; Super Bot Fight Mode on paid plans

Fits when

Origin is on AWS; one bill and one IAM model; rules also protect ALB and API Gateway

You want a free edge with DDoS included, or you aren't on AWS

Watch for

Metered requests still cost something unless on a flat-rate plan

Origin traffic that misses the cache pays AWS egress; Free plan rate limits are coarse

A sensible baseline

  1. Put every public route behind CloudFront or Cloudflare. Nothing should hit API Gateway or a function URL directly.

  2. A rate-based rule on every expensive route: sign-up, login, search, AI endpoints, forms. Answer 429.

  3. Managed bot rules that let verified search and AI crawlers through, since they are how people find you.

  4. Reserved concurrency on public functions so a flood can't starve webhooks and internal jobs.

  5. Per-user quotas in code for anything that costs money per call (AI tokens, SMS, email).

  6. A billing alarm and a dashboard of blocked versus allowed requests, so the next flood is a graph, not a surprise.

hoolco Monthly Review

Once a month: what changed in tech that month and what it means for you, from cloud and AI to security and the tools you build with. Every item links to its source. No sales pitch, unsubscribe in one click.

As of November 2, 2026. Provided as is, for general information, and accurate to the best of our knowledge on the date shown. Not legal, compliance, tax, financial or other professional advice. Prices, limits and vendor services change; check current vendor documentation before relying on anything here. Terms of use.