Difficulty: Intermediate
What is AWS WAF, how does it work, and where would you attach it to protect a web application?
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.
Everything in WAF lives inside a web ACL (access control list). A web ACL holds an ordered list of rules and a default action.
Allow, Block, CAPTCHA, Challenge) decides the outcome. Count is non-terminating, so evaluation continues.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.
AWSManagedRulesCommonRuleSet (OWASP-style protections), AWSManagedRulesSQLiRuleSet, AWSManagedRulesKnownBadInputsRuleSet and AWSManagedRulesAmazonIpReputationList. These are the fastest way to get baseline protection./login or OTP endpoints.| Resource | Supported | Notes |
|---|---|---|
| CloudFront | Yes | Web ACL must be created in the global (us-east-1) scope |
| Application Load Balancer | Yes | Regional web ACL |
| API Gateway REST API | Yes | HTTP APIs are not supported directly |
| AppSync, Cognito user pools, App Runner, Verified Access, Amplify | Yes | Regional |
| Network Load Balancer, EC2 instance | No | WAF needs HTTP, NLB is Layer 4 |
Count mode first, watch the logs for false positives (a checkout form tripping the SQLi rule is a classic), then switch to block.aws wafv2).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.
{
"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.
# 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.
Web ACL, Managed Rule Groups, Rate-based Rules, Layer 7 Firewall, SQL Injection, XSS