AWS WAF

Difficulty: Intermediate

Question

What is AWS WAF, how does it work, and where would you attach it to protect a web application?

Answer

AWS WAF is a managed Layer 7 web application firewall that inspects HTTP(S) requests before they reach your application and allows, blocks, counts or challenges them based on rules you define. It protects against common web exploits such as SQL injection, cross-site scripting (XSS), bad bots and request floods, and you attach it to CloudFront, an Application Load Balancer, API Gateway and a few other front doors rather than to EC2 instances directly.

How a web ACL evaluates a request

Everything in WAF lives inside a web ACL (access control list). A web ACL holds an ordered list of rules and a default action.

  1. A request arrives at the protected resource (for example a CloudFront distribution).
  2. WAF evaluates rules in priority order, lowest number first.
  3. Each rule inspects parts of the request: IP address, country, headers, query string, URI path, body, cookies, or JA3/JA4 TLS fingerprints.
  4. The first rule with a terminating action (Allow, Block, CAPTCHA, Challenge) decides the outcome. Count is non-terminating, so evaluation continues.
  5. If no rule matches, the web ACL's default action applies (usually Allow).

Rules are measured in WCUs (web ACL capacity units). Simple IP matches are cheap, regex and body inspection cost more, and each web ACL has a capacity ceiling, so you cannot stack unlimited complex rules.

Rule types you will actually use

  • AWS Managed Rules: curated rule groups such as AWSManagedRulesCommonRuleSet (OWASP-style protections), AWSManagedRulesSQLiRuleSet, AWSManagedRulesKnownBadInputsRuleSet and AWSManagedRulesAmazonIpReputationList. These are the fastest way to get baseline protection.
  • Rate-based rules: block an IP (or a custom key like a header or API key) that exceeds N requests in an evaluation window. Ideal for brute force on /login or OTP endpoints.
  • Geo match and IP sets: allow only India for an internal admin panel, or block known abusive ranges.
  • Bot Control and Fraud Control: paid managed groups for scraper detection, account takeover prevention (ATP) and fake account creation (ACFP).
  • Custom rules: string or regex matches, size constraints (block bodies over a limit), label matching to combine rules.

Where WAF can and cannot be attached

ResourceSupportedNotes
CloudFrontYesWeb ACL must be created in the global (us-east-1) scope
Application Load BalancerYesRegional web ACL
API Gateway REST APIYesHTTP APIs are not supported directly
AppSync, Cognito user pools, App Runner, Verified Access, AmplifyYesRegional
Network Load Balancer, EC2 instanceNoWAF needs HTTP, NLB is Layer 4

Common mistakes and gotchas

  • Deploying new rules straight into Block. Put new or managed rules in Count mode first, watch the logs for false positives (a checkout form tripping the SQLi rule is a classic), then switch to block.
  • Body inspection limits. WAF only inspects the first part of the request body (16 KB by default for CloudFront and ALB-type resources, configurable higher for some at extra cost). Payloads hidden beyond that limit are not inspected unless you set the oversize handling to block.
  • Thinking WAF replaces secure code. WAF is a filter, not a fix. Parameterised queries and output encoding are still mandatory.
  • Forgetting logging. Enable WAF logs to CloudWatch Logs, S3 or Firehose, otherwise you cannot explain why a customer got a 403.
  • WAF Classic is legacy; all new work uses AWS WAF (sometimes called WAFv2 in the API and CLI, aws wafv2).

How it fits a real architecture

A typical setup for an Indian e-commerce site during a sale: CloudFront in front of an ALB, one web ACL on CloudFront with the Common and SQLi managed groups, an IP reputation list, a rate-based rule of a few hundred requests per five minutes per IP on /api/otp, and a geo rule restricting /admin to India. AWS Shield Standard handles network-layer floods underneath, and WAF handles application-layer abuse. Pricing is per web ACL, per rule and per million requests, with extra charges for Bot Control and Fraud Control.

How AWS WAF filters a request. WAF sits at the edge or load balancer and decides before your code ever runs.
  1. Client request: Browser, bot or attacker sends HTTP(S) traffic.
  2. CloudFront or ALB: The protected resource hands the request to WAF.
  3. Web ACL rules: Rules run in priority order: managed, rate-based, custom.
  4. Action: Allow, Block (403), Count, CAPTCHA or Challenge.
  5. Origin application: Only allowed requests reach your servers.

Code examples

Rate-based rule protecting an OTP endpoint (web ACL rule JSON)

{
  "Name": "LimitOtpPerIp",
  "Priority": 1,
  "Action": { "Block": {} },
  "Statement": {
    "RateBasedStatement": {
      "Limit": 100,
      "EvaluationWindowSec": 300,
      "AggregateKeyType": "IP",
      "ScopeDownStatement": {
        "ByteMatchStatement": {
          "FieldToMatch": { "UriPath": {} },
          "PositionalConstraint": "STARTS_WITH",
          "SearchString": "/api/otp",
          "TextTransformations": [{ "Priority": 0, "Type": "LOWERCASE" }]
        }
      }
    }
  },
  "VisibilityConfig": {
    "SampledRequestsEnabled": true,
    "CloudWatchMetricsEnabled": true,
    "MetricName": "LimitOtpPerIp"
  }
}

Any single IP sending more than 100 requests to paths starting with /api/otp within five minutes is blocked until its rate drops. The scope-down statement keeps the limit from affecting normal browsing.

Associate a regional web ACL with an ALB

# List regional web ACLs (use --scope CLOUDFRONT --region us-east-1 for CloudFront)
aws wafv2 list-web-acls --scope REGIONAL --region ap-south-1

# Attach the web ACL to an Application Load Balancer
aws wafv2 associate-web-acl \
  --web-acl-arn arn:aws:wafv2:ap-south-1:123456789012:regional/webacl/shop-acl/abcd1234 \
  --resource-arn arn:aws:elasticloadbalancing:ap-south-1:123456789012:loadbalancer/app/shop-alb/50dc6c495c0c9188 \
  --region ap-south-1

Regional resources such as ALB and API Gateway use REGIONAL scope. CloudFront web ACLs are attached through the distribution config instead of associate-web-acl.

Key points

  • WAF works at Layer 7 and only protects HTTP(S) front doors: CloudFront, ALB, API Gateway REST, AppSync, Cognito and a few others
  • A web ACL evaluates rules in priority order; the first terminating action wins, otherwise the default action applies
  • Start managed and custom rules in Count mode, check logs for false positives, then switch to Block
  • Rate-based rules are the go-to defence against login, OTP and scraping abuse
  • CloudFront web ACLs must be created with CLOUDFRONT scope in us-east-1

Common follow-up questions

  • How is AWS WAF different from AWS Shield and from security groups?
  • How would you stop credential stuffing on a login API using WAF?
  • Why can you not attach WAF to a Network Load Balancer?
  • How do you troubleshoot a legitimate user being blocked by WAF?
  • What does AWS Firewall Manager add on top of WAF in a multi-account setup?

Interview tips

  • Name the building blocks precisely: web ACL, rules, rule groups, priorities, default action, WCUs
  • Mention the Count-first rollout; it shows you have operated WAF in production rather than just read about it
  • Contrast WAF (Layer 7, application attacks) with Shield (DDoS) and security groups (Layer 3/4 allow lists) in one sentence

Concepts covered

Web ACL, Managed Rule Groups, Rate-based Rules, Layer 7 Firewall, SQL Injection, XSS

More AWS & Cloud Fundamentals questions