AWS WAF in front of a CloudFront download service and an ALB-fronted Kubernetes app: managed rules, geo-blocking, rate limits, custom rules, a migration off WAF Classic, and what the Splunk dashboards actually showed.

A web application firewall is a bouncer. It stands at the door, looks at every request, and decides who gets in. The trouble with bouncers is that a bad one either lets everyone through or throws out your best customers, and you only find out which when someone complains.

I've set up AWS WAF in front of two quite different things, moved an old WAF Classic setup over to WAFv2, and wired the logs into Splunk so we could see what the bouncer was actually doing. Over that time it blocked more than 1,000 requests from regions we didn't serve and caught around 50 DDoS attempts before they reached the app. This post is how that was put together, what AWS gives you out of the box, and where we had to write our own rules.

No employer names, account IDs or hostnames here. The architecture and the lessons are the useful part anyway.

The two doors

Door one: file downloads from S3 through CloudFront. Users download files from an S3 bucket, served through a CloudFront distribution so they're fast wherever the user is. AWS WAF sits on the CloudFront distribution.

Door two: an Application Load Balancer in front of Kubernetes. The ALB forwards traffic into an Istio ingress gateway, which routes it to services in the cluster. AWS WAF sits on the ALB.

Same product, two slightly different setups. Here's what tends to trip people up.

Scope and region. A web ACL for CloudFront has to be created with CLOUDFRONT scope, and that means in us-east-1, wherever your actual workload lives. A web ACL for an ALB is REGIONAL and must be in the same region as the load balancer. You can't share one web ACL between the two. If your Terraform has a single AWS provider pointed at eu-west-2, the CloudFront one will fail in a way that makes you question your career, so give it its own provider alias.

provider "aws" { alias = "us_east_1" region = "us-east-1" } resource "aws_wafv2_web_acl" "downloads" { provider = aws.us_east_1 name = "downloads-cf" scope = "CLOUDFRONT" # default_action, rules, visibility_config ... } resource "aws_wafv2_web_acl" "app" { name = "app-alb" scope = "REGIONAL" # default_action, rules, visibility_config ... } 

Lock the side door. WAF on CloudFront only helps if CloudFront is the only way in. If the S3 bucket is still reachable directly, an attacker just skips the bouncer and walks round the back. Use Origin Access Control so the bucket only accepts requests from your distribution, and keep S3 Block Public Access on.

For the ALB, the WAF sees the request before the cluster does. That's the point. Anything WAF blocks never reaches Istio, never uses a pod's CPU, and never shows up in your app logs pretending to be a user. Istio still does its own job (routing, mTLS between services), but it shouldn't have to deal with someone hammering the front door.

Start with AWS managed rules

Most of our protection came from AWS managed rule groups. They're maintained by AWS, they're updated as new attack patterns show up, and they cover the boring stuff you'd otherwise have to write yourself. The usual starting set:

  • AWSManagedRulesCommonRuleSet for the general OWASP-style attacks
  • AWSManagedRulesKnownBadInputsRuleSet for request patterns linked to known exploits
  • AWSManagedRulesAmazonIpReputationList for IPs AWS's own threat intelligence has flagged
  • AWSManagedRulesSQLiRuleSet where there's anything database-shaped behind the app

You don't need all of them everywhere. A CloudFront distribution that only serves file downloads has no database behind it, so the SQL injection set is mostly dead weight there. The app behind the load balancer is a different story. Pick per door, not per account.

The one habit worth stealing: turn new rules on in Count mode first. Count mode logs what a rule would have blocked without blocking it. Leave it for a few days, look at what it caught, then switch it to Block. Managed rules are written for everyone's traffic, not yours, and the Common rule set in particular can be strict about things like large request bodies. Better to find that out from a log than from a user whose upload suddenly stopped working.

rule { name = "aws-common" priority = 10 override_action { count {} # switch to none {} once you've checked the logs } statement { managed_rule_group_statement { vendor_name = "AWS" name = "AWSManagedRulesCommonRuleSet" } } visibility_config { cloudwatch_metrics_enabled = true metric_name = "aws-common" sampled_requests_enabled = true } } 

Geo-blocking: the easiest win

If you don't serve a country, there's no reason to accept traffic from it. A geo match rule does exactly that, by country code.

rule { name = "allowed-countries-only" priority = 1 action { block {} } statement { not_statement { statement { geo_match_statement { country_codes = ["GB", "NG"] # example list } } } } visibility_config { cloudwatch_metrics_enabled = true metric_name = "geo-block" sampled_requests_enabled = true } } 

This one rule blocked more than 1,000 requests from regions we didn't allow. None of those were customers. A lot of them were the kind of automated scanning every public endpoint gets within minutes of going live, and it's nice not to pay for them.

Geo-blocking isn't a security boundary on its own, since anyone can rent a server in an allowed country. Think of it as cutting the noise so the rules after it, and the people reading the logs, only deal with traffic that might be real.

Rate-based rules for DDoS

A rate-based rule counts requests from each IP over a time window and blocks any IP that goes over the limit you set. It's the simplest defence against a flood of requests, whether that's a real DDoS attempt or one very enthusiastic script.

A multi-lane road at night packed bumper to bumper with cars
Photo by Minku Kang on Unsplash

rule { name = "rate-limit-per-ip" priority = 5 action { block {} } statement { rate_based_statement { limit = 2000 # requests per window per IP, tune to your traffic aggregate_key_type = "IP" } } visibility_config { cloudwatch_metrics_enabled = true metric_name = "rate-limit" sampled_requests_enabled = true } } 

The limit is the whole game. Set it too low and your own office behind one NAT IP gets blocked on a busy morning. Set it too high and it never fires. Start from what your real traffic looks like per IP, then go comfortably above it.

The two doors also want different limits. Someone downloading files makes a handful of large requests. Someone using a web app makes lots of small ones as the page loads. One number for both is either too loose for downloads or too tight for the app.

These rules caught around 50 DDoS attempts. We knew because each one showed up in Splunk as a burst of rate-based blocks from a set of IPs, and the app behind it carried on as normal.

Getting WAF logs into Splunk

A WAF you can't see into is just a guess. We sent the WAF logs to Splunk, which is where the security team already lived, and built searches and alerts on top.

A laptop showing an analytics dashboard with charts and graphs
Photo by Luke Chesser on Unsplash

The path we used was the simple one: WAF logs into an S3 bucket, and the Splunk Add-on for AWS pulling them from there. WAF can write straight to S3, as long as the bucket name starts with aws-waf-logs-. On the Splunk side, the add-on's SQS-based S3 input is the one to use: S3 sends an event notification to an SQS queue for each new log file, and Splunk reads the queue, so it picks up new files without re-scanning the whole bucket.

resource "aws_s3_bucket" "waf_logs" { bucket = "aws-waf-logs-example-app" # must start with aws-waf-logs- } resource "aws_wafv2_web_acl_logging_configuration" "app" { resource_arn = aws_wafv2_web_acl.app.arn log_destination_configs = [aws_s3_bucket.waf_logs.arn] } 

It's not real-time. Files land in S3 every few minutes, so there's a short delay before Splunk sees them. For spotting floods and trends that was fine. If you need alerts within seconds, Kinesis Data Firehose straight into Splunk's HTTP Event Collector is the faster option.

Each log record tells you which rule ended the request, what it did, and where the request came from, which makes searches like this possible:

index=aws_waf action=BLOCK terminatingRuleType=RATE_BASED | timechart span=5m count by terminatingRuleId 
index=aws_waf action=BLOCK terminatingRuleId="allowed-countries-only" | stats count by httpRequest.country | sort - count 

The first shows a flood starting and stopping. The second shows where the geo-blocked traffic was coming from. Put both on a dashboard and you can answer "are we under attack right now?" in about ten seconds, which beats the alternative of refreshing the app and hoping.

Where managed rules weren't enough

Managed rules cover common attacks. They don't know your application. We needed custom rules for cases AWS's rule groups didn't cover, and the logs in Splunk were usually what told us where.

The kinds of gaps that need a custom rule look like this:

  • A path that needs tighter limits than the rest. A login or file-download endpoint can get its own rate-based rule, scoped with a match on the URI, so it's limited harder without touching the rest of the site.
  • A pattern you keep seeing in the logs. A user agent, header or query string that only ever shows up in blocked or suspicious traffic can be blocked by name.
  • Exceptions. A partner or monitoring service that trips a managed rule can be allowed through by IP set, placed at a higher priority than the rule it trips, instead of turning the managed rule off for everyone.

Here's the first one, a tighter limit on a single path:

rule { name = "rate-limit-downloads" priority = 4 action { block {} } statement { rate_based_statement { limit = 300 aggregate_key_type = "IP" scope_down_statement { byte_match_statement { search_string = "/downloads/" positional_constraint = "STARTS_WITH" field_to_match { uri_path {} } text_transformation { priority = 0 type = "LOWERCASE" } } } } } visibility_config { cloudwatch_metrics_enabled = true metric_name = "rate-limit-downloads" sampled_requests_enabled = true } } 

The pattern I'd recommend: put custom rules in their own rule group with its own priority range, give every rule a clear metric_name, and keep a one-line comment on why each exists. Six months later "block-xyz-header" means nothing to anyone, including you.

Moving off WAF Classic

The older setup was on WAF Classic, the original version of AWS WAF. WAFv2 is a different API with different resources, a much better rule language, managed rule groups, labels, and more flexible rate-based rules. AWS has been retiring Classic, so the move had to happen at some point anyway.

The approach that works:

  1. Inventory first. List every Classic web ACL, its rules, and what it's associated with (CloudFront distributions, load balancers).
  2. Rebuild in WAFv2, in code. AWS has a migration option that generates a CloudFormation template from a Classic web ACL, which is a useful starting point, but treat its output as a draft. Rewriting the rules properly in Terraform is also the chance to swap hand-rolled Classic rules for managed rule groups.
  3. Run the new web ACL in Count mode alongside. Compare what it would block against what Classic is blocking.
  4. Switch the association. Point the CloudFront distribution or ALB at the new web ACL, then turn on logging for it. Logging and associations are exactly the bits that are easy to forget during a migration.
  5. Delete Classic only after a quiet week.

The thing to watch most closely is the logs. If the new web ACL's logging isn't set up before you switch, Splunk goes quiet, and a quiet dashboard looks exactly like a good day. Check that new log files are arriving in S3 and showing up in Splunk on the same day you switch, not when someone asks for a report.

What I'd tell someone starting today

  • Put WAF on every public entry point, and make sure it's the only entry point. A WAF on CloudFront with an open S3 bucket behind it is a locked front door with the back door propped open.
  • Start with managed rules in Count mode, read the logs, then block.
  • Geo-block anything you don't serve. It's the cheapest noise reduction you'll ever get.
  • Set rate limits from real traffic, not from a number that felt right.
  • Send logs somewhere people already look. For us that was Splunk. A dashboard nobody opens is the same as no dashboard.
  • Write custom rules for your own app, and write down why each one exists.

The bouncer analogy holds up all the way through. The good ones aren't the ones who throw the most people out. They're the ones who know exactly who's supposed to be inside.

Originally published on Medium.