AWS WAF and AWS Shield
Protect Harbourline's public banking APIs with AWS WAF managed rules, rate limiting and safe logging, and understand what Shield Standard gives you free and when Shield Advanced is worth USD 3,000 a month.
Assumes: .NET 10 LTS · AWS SDK for .NET v4 · AWS CLI v2 · CDK v2 (Amazon.CDK.Lib 2.277.0) · Terraform AWS provider v6 · Region us-east-1. Verified against official sources on 2026-11-17.
Learning objectives
By the end of this lesson you can:
- Explain web ACLs (now also called protection packs in the new WAF console), rules, rule groups, rule priority, actions and web ACL capacity units (WCUs).
- Choose AWS managed rule groups for a banking API, including when the paid Bot Control, Account Takeover Prevention (ATP) and Account Creation Fraud Prevention (ACFP) groups are justified.
- Write a rate-based rule that protects a login endpoint, with a custom JSON
429response. - Roll out rules safely in Count mode, read the logs, and redact sensitive fields before they are stored.
- Compare Shield Standard and Shield Advanced, and explain the commitment, the Shield Response Team and cost protection.
- Deploy and tear down a web ACL on an API Gateway stage with CDK (C#) or Terraform within one session.
Prerequisites
- Segmentation, private endpoints and egress filtering
- Module 4: APIs and integration — API Gateway stages, the Account Information API
- Tools: AWS CLI v2 (
learn-sandboxprofile), .NET 10 SDK, optionally the CDK CLI or Terraform 1.14
Why this matters in banking
Banks are among the most attacked organisations on the internet. Three attack families matter most for Harbourline's public APIs:
- Credential stuffing — attackers replay username/password pairs leaked from other websites against the mobile-banking login. Even a 0.1% success rate on a million attempts is a thousand compromised customers, each a potential fraud case and a regulatory notification.
- Application attacks — injection, path traversal, deserialisation exploits and scanners probing for
/.envor admin pages. - DDoS — volumetric floods at the network layer, or request floods at the application layer that exhaust your .NET instances and database connections. For a bank, an outage of online payments is an operational resilience event that regulators expect you to plan for and report on.
AWS WAF and AWS Shield do not replace secure code (you still validate input and use parameterised queries), but they stop a large volume of junk before it reaches your code, give you a fast lever during an incident, and produce the logs your SOC needs.
The idea in plain language
AWS WAF is a web application firewall: a filter in front of an HTTP endpoint that looks at each request — IP address, headers, path, query string, body — and decides to allow, block, count, or challenge it (CAPTCHA or a silent browser Challenge). You put rules in a web ACL and attach it to CloudFront, an Application Load Balancer, an API Gateway REST API stage, a Cognito user pool, AppSync, or Verified Access.
AWS Shield is DDoS protection. Shield Standard is on for every AWS customer at no charge and absorbs common network- and transport-layer floods (SYN floods, UDP reflection). Shield Advanced is a paid subscription that adds tailored detection, automatic application-layer mitigation (using WAF), access to the Shield Response Team (SRT), and financial protection against scaling costs caused by an attack.
Analogy: WAF is the security guard at the branch door who checks every visitor against a list of known fraudsters, turns away anyone carrying a crowbar, and stops one person from walking in and out fifty times a minute. Shield Standard is the building's structure that stays up when a crowd pushes against the walls. Shield Advanced is a contract with a specialist crowd-control firm that has a 24/7 phone line and reimburses you for the extra staff you had to hire during the riot.
What problem the AWS services solve
| Concept | What it is | Notes and limits (checked 2026-11-17) |
|---|---|---|
| Web ACL / protection pack | Ordered list of rules plus a default action | 100 per account per Region by default; a CloudFront web ACL must be created in us-east-1 with scope CLOUDFRONT |
| Rule | A statement (match condition) plus an action | Evaluated in priority order (lowest number first); a terminating action stops evaluation |
| Rule group | Reusable set of rules: your own, AWS managed, or AWS Marketplace | Managed groups are versioned and updated by AWS or the seller |
| WCU | Capacity cost of rules | Max 5,000 WCUs per web ACL; above 1,500 WCUs you pay extra per request |
| Rate-based rule | Counts requests per aggregation key (IP, header, …) over a window | Minimum limit 10; windows of 60, 120, 300 (default) or 600 seconds; max 10 per web ACL |
| Labels | Tags that rules add to a request so later rules can act on them | E.g. block only requests that ATP labelled credential_compromised |
| Logging | Full request logs to CloudWatch Logs, S3 or Firehose | Destination names must start with aws-waf-logs-; redaction of selected fields supported |
| Body inspection | How much of the request body WAF reads | ALB: fixed 8 KB. API Gateway, CloudFront, Cognito, Verified Access: 16 KB default, up to 64 KB (extra cost) |
Limitations to be honest about: WAF only sees HTTP(S) requests to supported AWS resources; it does not protect an EC2 instance with a public IP or an NLB. It cannot understand your business logic (for example "this customer is moving money to a new payee for the first time"). Managed rules can produce false positives on legitimate banking payloads — such as ISO 20022 XML or long JWTs — which is why you start in Count mode.
Shield Standard vs Shield Advanced
| Shield Standard | Shield Advanced | |
|---|---|---|
| Price | Included at no extra cost | USD 3,000 per month with a 1-year commitment, plus data-transfer-out usage fees; one fee covers all accounts in an Organizations consolidated-billing family |
| Layer 3/4 protection | Automatic for all customers | Enhanced, with resource-specific detection baselines |
| Layer 7 protection | Bring your own WAF rules | Automatic application-layer DDoS mitigation (adds a 150-WCU rule group), plus the Anti-DDoS managed rule group |
| Protected resources | All | You choose: CloudFront, Route 53 hosted zones, Global Accelerator, ALB, Classic Load Balancer, Elastic IPs |
| WAF charges | Normal | Standard WAF fees covered for protected resources (web ACL, rules, requests up to 1,500 WCUs and default body size; up to 50 billion requests per month). Bot Control, Fraud Control, CAPTCHA and WCUs above 1,500 are still charged |
| Shield Response Team | No | Yes — requires a Business or Enterprise Support plan |
| Cost protection | No | Service credits for scaling charges caused by a DDoS attack on protected resources (claim process applies) |
| Health-based detection | No | Uses Route 53 health checks to reduce false positives and speed detection |
| Visibility | Basic | Attack diagnostics, CloudWatch metrics, global threat dashboard |
AWS Shield network security director is a separate capability that discovers your network resources, checks security groups, NACLs and WAF configurations against best practices, and raises prioritised findings. It launched in preview in June 2025, gained multi-account analysis in December 2025, and sends findings to AWS Security Hub since March 2026. It is not included in a Shield Advanced subscription. ⚠ VERIFY its current availability status and pricing before you rely on it.
Microsoft / Azure comparison
| You know (Microsoft/Azure) | AWS equivalent | What's different |
|---|---|---|
| Azure Web Application Firewall on Application Gateway or Front Door | AWS WAF on ALB, API Gateway or CloudFront | One WAF service attaches to many resource types; rules use WCUs, not SKUs |
| OWASP CRS managed rule set (DRS) | AWSManagedRulesCommonRuleSet + KnownBadInputs + SQLiRuleSet | AWS groups are AWS-authored, not literal OWASP CRS |
| Azure WAF rate-limit custom rule | Rate-based rule | Windows of 1–10 minutes, aggregation on IP, forwarded IP, headers, cookies, labels and more |
| Azure WAF Bot Manager rule set | Bot Control (Common / Targeted) | Targeted level uses browser interrogation and tokens; extra fees |
| Azure DDoS Network Protection | Shield Advanced | Similar commercial model (fixed monthly fee, response team, cost protection) |
| Azure DDoS infrastructure protection (free) | Shield Standard | Automatic, free |
| Azure Firewall Manager WAF policies | AWS Firewall Manager WAF/Shield policies | Org-wide enforcement, priced per policy per Region |
| ASP.NET Core rate limiting middleware | (complementary) | Keep app-level limits per customer; WAF limits per IP or header at the edge |
Architecture
Most banks attach a web ACL at the edge (CloudFront) to stop traffic as close to the attacker as possible, and optionally a second, simpler one at the regional layer to protect against requests that bypass CloudFront. In the lab you use only a regional web ACL on an API Gateway stage, because it is cheaper and quicker.
Rule evaluation inside a web ACL
Order matters: cheap, high-confidence rules (reputation lists, rate limits) go first; expensive inspection later. Allow rules for trusted sources go first only if you are sure they cannot be spoofed — never allow-list on a header the client controls.
Step-by-step implementation
Inventory your endpoints and their risk
List every public path and what it does. For the Accounts API: POST /v1/auth/login (credential stuffing target), GET /v1/accounts/{id} (enumeration target), POST /v1/payments (fraud target, requires Idempotency-Key). Note the expected request rates per customer and per IP — mobile carriers put thousands of customers behind one IP, so per-IP limits must be generous.
Start with baseline managed rule groups
Add AWSManagedRulesAmazonIpReputationList (25 WCUs), AWSManagedRulesCommonRuleSet (700 WCUs), AWSManagedRulesKnownBadInputsRuleSet (200 WCUs) and AWSManagedRulesSQLiRuleSet. That is under 1,500 WCUs, so no capacity surcharge. Set the managed groups' override action to Count for the first deployment.
Add a rate-based rule for login
Scope it with a scope-down statement that matches only /v1/auth/login. Use a 5-minute window and a limit agreed with the fraud team. Return a custom 429 with a JSON body in the ProblemDetails (RFC 9457) shape your clients already understand.
Turn on logging with redaction — before production traffic
Send logs to a CloudWatch Logs group named aws-waf-logs-hl-m05-accounts-api with retention set (for example 30 days in the lab; your records policy in production). Redact the authorization header and the query string. WAF logs contain full request metadata; storing bearer tokens or customer identifiers in a log group widely readable by engineers is itself a data-protection incident.
Run in Count mode and tune
Send normal traffic (your integration tests, a synthetic monitor) for a period agreed with the business. Query the logs for rules that matched legitimate requests. Common false positives in banking: SizeRestrictions_BODY on large statement downloads posted as JSON, CrossSiteScripting_BODY on free-text payment references containing <, and NoUserAgent_HEADER on a partner's batch client. Override only the specific rule to Count (rule action override), not the whole group.
Switch to Block and alarm
Change the override action to None (meaning "use the rule group's own actions"). Create CloudWatch alarms on BlockedRequests for each rule's metric so a sudden spike pages the on-call engineer.
Decide on paid protections with a cost model
ATP on the login endpoint charges a subscription plus a per-request fee that is far higher than base WAF — use a scope-down so it only inspects the login path. Bot Control Common is inexpensive; Targeted costs more and needs the WAF client SDK or challenge actions. Shield Advanced is justified when you have a significant revenue or regulatory exposure to DDoS, multiple internet-facing resources, and want SRT support.
Govern centrally
In a multi-account bank, use AWS Firewall Manager from a security tooling account to require a baseline web ACL on every ALB, API Gateway stage and CloudFront distribution, and to auto-remediate drift. Application teams can add their own rules in a second rule group slot.
AWS Console instructions
- Console → WAF & Shield. The new console experience calls web ACLs protection packs; the classic view still says Web ACLs. Both refer to the same resource.
- Choose Create protection pack (web ACL) → resource type Regional resources → Region US East (N. Virginia) → Add associated resources → pick the API Gateway REST API stage
hl-m05-accounts-api / prod. - Add rules → Add managed rule groups → AWS managed rule groups. Enable Amazon IP reputation list, Core rule set, Known bad inputs, SQL database. For each, choose Edit → Override all rule actions to Count for the first rollout.
- Add rules → Add my own rules → Rule builder → Rate-based rule. Name
login-rate-limit, rate limit100, evaluation window5 minutes, aggregate on Source IP address, scope Only consider requests that match URI path starts with/v1/auth/login. Action Block → Enable custom response → code429→ body keyrate-limited. - Set rule priority as in the diagram above. Default action Allow.
- After creation: Logging and metrics → Enable logging → CloudWatch Logs log group
aws-waf-logs-hl-m05-accounts-api→ Redacted fields: Headerauthorization, Query string. - Shield: WAF & Shield → AWS Shield → Overview shows the subscription state. Do not click Subscribe to Shield Advanced in a learning account: it starts a one-year commitment.
AWS CLI
export AWS_PROFILE=learn-sandbox AWS_REGION=us-east-1
# 1. Show the WCU capacity a set of rules would need before you create the web ACL
aws wafv2 check-capacity --scope REGIONAL --rules file://rules.json
# 2. Create the web ACL (rules.json contains the rules shown in the IaC section)
aws wafv2 create-web-acl --name hl-m05-accounts-api-acl --scope REGIONAL \
--default-action Allow={} \
--visibility-config SampledRequestsEnabled=true,CloudWatchMetricsEnabled=true,MetricName=hl-m05-accounts-api-acl \
--custom-response-bodies '{"rate-limited":{"ContentType":"APPLICATION_JSON","Content":"{\"type\":\"https://harbourline.example.com/problems/rate-limited\",\"title\":\"Too many requests\",\"status\":429}"}}' \
--rules file://rules.json \
--tags Key=Project,Value=cea-lab Key=Lab,Value=m05-l15 Key=Environment,Value=learn Key=Owner,Value=alex
# 3. Attach it to an API Gateway REST API stage (ARN format for REST API stages)
aws wafv2 associate-web-acl \
--web-acl-arn arn:aws:wafv2:us-east-1:111122223333:regional/webacl/hl-m05-accounts-api-acl/EXAMPLE-ID \
--resource-arn arn:aws:apigateway:us-east-1::/restapis/a1b2c3d4e5/stages/prod
# 4. Create the log group (name MUST start with aws-waf-logs-) with retention
aws logs create-log-group --log-group-name aws-waf-logs-hl-m05-accounts-api
aws logs put-retention-policy --log-group-name aws-waf-logs-hl-m05-accounts-api --retention-in-days 30
# 5. Enable logging with redaction of the Authorization header and the query string
aws wafv2 put-logging-configuration --logging-configuration '{
"ResourceArn": "arn:aws:wafv2:us-east-1:111122223333:regional/webacl/hl-m05-accounts-api-acl/EXAMPLE-ID",
"LogDestinationConfigs": ["arn:aws:logs:us-east-1:111122223333:log-group:aws-waf-logs-hl-m05-accounts-api"],
"RedactedFields": [ { "SingleHeader": { "Name": "authorization" } }, { "QueryString": {} } ]
}'
# 6. Look at a sample of recent requests matched by the login rate rule (last 3 hours max)
aws wafv2 get-sampled-requests \
--web-acl-arn arn:aws:wafv2:us-east-1:111122223333:regional/webacl/hl-m05-accounts-api-acl/EXAMPLE-ID \
--rule-metric-name login-rate-limit --scope REGIONAL --max-items 50 \
--time-window StartTime=$(date -u -d '-1 hour' +%s),EndTime=$(date -u +%s)
# 7. Which IPs are currently being rate limited by the rule?
aws wafv2 get-rate-based-statement-managed-keys --scope REGIONAL \
--web-acl-name hl-m05-accounts-api-acl --web-acl-id EXAMPLE-ID --rule-name login-rate-limit
# 8. Check whether this account has Shield Advanced (read-only — does not subscribe)
aws shield get-subscription-stateNotes on each command:
check-capacitytells you the WCUs before you hit the 5,000 limit or the 1,500 pricing threshold.--custom-response-bodiesdefines reusable bodies that rules reference by key. JSON inside JSON needs escaping; in practice keep this in a file.- REST API stages use
arn:aws:apigateway:<region>::/restapis/<api-id>/stages/<stage>. HTTP APIs (API Gateway v2) cannot be associated with WAF directly — put CloudFront in front if you use them. - The
aws-waf-logs-prefix is mandatory for all three destination types. - The log group ARN must not end in
:*. Many tools (including CDK'sLogGroupArn) add:*; WAF rejects it. get-sampled-requestsis free and great for quick tuning, but only covers up to the last three hours and a sample.- Lists the aggregation keys (IPs) that are currently over the limit — useful during an incident.
- Shows
ACTIVEorINACTIVE; it changes nothing.
Infrastructure as Code
using Amazon.CDK;
using Amazon.CDK.AWS.Logs;
using Amazon.CDK.AWS.WAFv2;
using Constructs;
namespace Harbourline.Accounts.Infra;
public sealed class ApiWafStackProps : StackProps
{
/// <summary>ARN of the API Gateway REST API stage to protect.</summary>
public required string ApiStageArn { get; init; }
/// <summary>When true, managed groups only Count (first rollout).</summary>
public bool CountOnly { get; init; } = true;
}
public sealed class ApiWafStack : Stack
{
public ApiWafStack(Construct scope, string id, ApiWafStackProps props) : base(scope, id, props)
{
static CfnWebACL.VisibilityConfigProperty Metrics(string name) => new()
{
CloudWatchMetricsEnabled = true,
SampledRequestsEnabled = true,
MetricName = name,
};
// Managed groups: Count during rollout, then None (= use the group's own Block actions).
CfnWebACL.OverrideActionProperty Override() => props.CountOnly
? new CfnWebACL.OverrideActionProperty { Count = new Dictionary<string, object>() }
: new CfnWebACL.OverrideActionProperty { None = new Dictionary<string, object>() };
CfnWebACL.RuleProperty Managed(string name, int priority) => new()
{
Name = name,
Priority = priority,
OverrideAction = Override(),
Statement = new CfnWebACL.StatementProperty
{
ManagedRuleGroupStatement = new CfnWebACL.ManagedRuleGroupStatementProperty
{
VendorName = "AWS",
Name = name,
},
},
VisibilityConfig = Metrics(name),
};
var loginRateLimit = new CfnWebACL.RuleProperty
{
Name = "login-rate-limit",
Priority = 1,
Action = new CfnWebACL.RuleActionProperty
{
Block = new CfnWebACL.BlockActionProperty
{
CustomResponse = new CfnWebACL.CustomResponseProperty
{
ResponseCode = 429,
CustomResponseBodyKey = "rate-limited",
ResponseHeaders = new[]
{
new CfnWebACL.CustomHTTPHeaderProperty { Name = "Retry-After", Value = "300" },
},
},
},
},
Statement = new CfnWebACL.StatementProperty
{
RateBasedStatement = new CfnWebACL.RateBasedStatementProperty
{
Limit = 100, // requests per window per IP (minimum allowed is 10)
EvaluationWindowSec = 300, // 60, 120, 300 or 600
AggregateKeyType = "IP",
ScopeDownStatement = new CfnWebACL.StatementProperty
{
ByteMatchStatement = new CfnWebACL.ByteMatchStatementProperty
{
FieldToMatch = new CfnWebACL.FieldToMatchProperty { UriPath = new Dictionary<string, object>() },
PositionalConstraint = "STARTS_WITH",
SearchString = "/v1/auth/login",
TextTransformations = new[]
{
new CfnWebACL.TextTransformationProperty { Priority = 0, Type = "LOWERCASE" },
},
},
},
},
},
VisibilityConfig = Metrics("login-rate-limit"),
};
var webAcl = new CfnWebACL(this, "AccountsApiAcl", new CfnWebACLProps
{
Name = "hl-m05-accounts-api-acl",
Scope = "REGIONAL",
DefaultAction = new CfnWebACL.DefaultActionProperty { Allow = new CfnWebACL.AllowActionProperty() },
VisibilityConfig = Metrics("hl-m05-accounts-api-acl"),
CustomResponseBodies = new Dictionary<string, object>
{
["rate-limited"] = new CfnWebACL.CustomResponseBodyProperty
{
ContentType = "APPLICATION_JSON",
Content = """{"type":"https://harbourline.example.com/problems/rate-limited","title":"Too many requests","status":429}""",
},
},
Rules = new object[]
{
Managed("AWSManagedRulesAmazonIpReputationList", 0),
loginRateLimit,
Managed("AWSManagedRulesCommonRuleSet", 2),
Managed("AWSManagedRulesKnownBadInputsRuleSet", 3),
Managed("AWSManagedRulesSQLiRuleSet", 4),
},
});
_ = new CfnWebACLAssociation(this, "AccountsApiAclAssociation", new CfnWebACLAssociationProps
{
ResourceArn = props.ApiStageArn,
WebAclArn = webAcl.AttrArn,
});
// Log group name must start with aws-waf-logs-; retention is mandatory in Harbourline labs.
var logGroup = new LogGroup(this, "WafLogs", new LogGroupProps
{
LogGroupName = "aws-waf-logs-hl-m05-accounts-api",
Retention = RetentionDays.ONE_MONTH,
RemovalPolicy = RemovalPolicy.DESTROY, // ⚠ learning shortcut: RETAIN in production
});
_ = new CfnLoggingConfiguration(this, "WafLogging", new CfnLoggingConfigurationProps
{
ResourceArn = webAcl.AttrArn,
// WAF rejects the ":*" suffix that LogGroupArn includes, so build the ARN explicitly.
LogDestinationConfigs = [FormatArn(new ArnComponents
{
Service = "logs",
Resource = "log-group",
ResourceName = logGroup.LogGroupName,
ArnFormat = ArnFormat.COLON_RESOURCE_NAME,
})],
RedactedFields = new object[]
{
new CfnLoggingConfiguration.FieldToMatchProperty
{
SingleHeader = new Dictionary<string, object> { ["Name"] = "authorization" },
},
new CfnLoggingConfiguration.FieldToMatchProperty { QueryString = new Dictionary<string, object>() },
},
});
foreach (var (k, v) in new[] { ("Project", "cea-lab"), ("Lab", "m05-l15"), ("Environment", "learn"), ("Owner", "alex") })
{
Amazon.CDK.Tags.Of(this).Add(k, v);
}
}
}Complete code
Tuning WAF is mostly reading logs. This .NET 10 console app reads WAF log records (one JSON object per line, as written to CloudWatch Logs or S3) and produces a tuning report: which rules terminated requests, which rules only counted (candidates for switching to Block, or false positives), and the top client IPs. It runs offline on a sample file, so you can practise without an AWS account; point it at an exported log file later.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<RootNamespace>Harbourline.Security.WafLogReport</RootNamespace>
</PropertyGroup>
</Project>using System.Text.Json.Serialization;
namespace Harbourline.Security.WafLogReport;
// Only the fields we need from the AWS WAF log format.
public sealed record WafLogRecord(
[property: JsonPropertyName("timestamp")] long Timestamp,
[property: JsonPropertyName("action")] string Action,
[property: JsonPropertyName("terminatingRuleId")] string TerminatingRuleId,
[property: JsonPropertyName("httpRequest")] WafHttpRequest HttpRequest,
[property: JsonPropertyName("ruleGroupList")] List<WafRuleGroup>? RuleGroupList,
[property: JsonPropertyName("nonTerminatingMatchingRules")] List<WafMatchedRule>? NonTerminatingMatchingRules);
public sealed record WafHttpRequest(
[property: JsonPropertyName("clientIp")] string ClientIp,
[property: JsonPropertyName("country")] string Country,
[property: JsonPropertyName("uri")] string Uri,
[property: JsonPropertyName("httpMethod")] string HttpMethod);
public sealed record WafRuleGroup(
[property: JsonPropertyName("ruleGroupId")] string RuleGroupId,
[property: JsonPropertyName("nonTerminatingMatchingRules")] List<WafMatchedRule>? NonTerminatingMatchingRules);
public sealed record WafMatchedRule(
[property: JsonPropertyName("ruleId")] string RuleId,
[property: JsonPropertyName("action")] string Action);using System.Text.Json;
using Harbourline.Security.WafLogReport;
var path = args.Length > 0 ? args[0] : "sample-waf-logs.jsonl";
var records = new List<WafLogRecord>();
foreach (var line in File.ReadLines(path))
{
if (string.IsNullOrWhiteSpace(line)) continue;
try
{
var record = JsonSerializer.Deserialize<WafLogRecord>(line);
if (record is not null) records.Add(record);
}
catch (JsonException ex)
{
Console.Error.WriteLine($"Skipping malformed line: {ex.Message}");
}
}
Console.WriteLine($"Analysed {records.Count} requests from {path}\n");
// 1. Which rule ended evaluation, per action?
Console.WriteLine("Terminating rules:");
foreach (var g in records.GroupBy(r => (r.Action, r.TerminatingRuleId)).OrderByDescending(g => g.Count()))
{
Console.WriteLine($" {g.Key.Action,-8} {g.Key.TerminatingRuleId,-45} {g.Count(),6}");
}
// 2. Rules that matched in COUNT mode — inside managed groups or at web ACL level.
var counted = records
.SelectMany(r => (r.RuleGroupList ?? [])
.SelectMany(rg => (rg.NonTerminatingMatchingRules ?? []).Select(m => $"{ShortName(rg.RuleGroupId)}:{m.RuleId}"))
.Concat((r.NonTerminatingMatchingRules ?? []).Select(m => m.RuleId)))
.GroupBy(x => x)
.OrderByDescending(g => g.Count());
Console.WriteLine("\nCounted (not blocked) matches — review before switching to Block:");
foreach (var g in counted)
{
Console.WriteLine($" {g.Key,-60} {g.Count(),6}");
}
// 3. Top client IPs among blocked requests (IPs are personal data under UK GDPR — keep reports internal).
Console.WriteLine("\nTop blocked client IPs:");
foreach (var g in records.Where(r => r.Action == "BLOCK").GroupBy(r => r.HttpRequest.ClientIp)
.OrderByDescending(g => g.Count()).Take(5))
{
Console.WriteLine($" {g.Key,-16} {g.Count(),6} e.g. {g.First().HttpRequest.HttpMethod} {g.First().HttpRequest.Uri}");
}
return 0;
static string ShortName(string ruleGroupId) =>
ruleGroupId.Contains("#", StringComparison.Ordinal) ? ruleGroupId[(ruleGroupId.LastIndexOf('#') + 1)..] : ruleGroupId;{"timestamp":1795392000000,"action":"BLOCK","terminatingRuleId":"login-rate-limit","httpRequest":{"clientIp":"198.51.100.23","country":"GB","uri":"/v1/auth/login","httpMethod":"POST"},"ruleGroupList":[],"nonTerminatingMatchingRules":[]}
{"timestamp":1795392000500,"action":"BLOCK","terminatingRuleId":"login-rate-limit","httpRequest":{"clientIp":"198.51.100.23","country":"GB","uri":"/v1/auth/login","httpMethod":"POST"},"ruleGroupList":[],"nonTerminatingMatchingRules":[]}
{"timestamp":1795392001000,"action":"ALLOW","terminatingRuleId":"Default_Action","httpRequest":{"clientIp":"203.0.113.50","country":"GB","uri":"/v1/payments","httpMethod":"POST"},"ruleGroupList":[{"ruleGroupId":"AWS#AWSManagedRulesCommonRuleSet","nonTerminatingMatchingRules":[{"ruleId":"CrossSiteScripting_BODY","action":"COUNT"}]}],"nonTerminatingMatchingRules":[]}
{"timestamp":1795392002000,"action":"BLOCK","terminatingRuleId":"AWSManagedRulesAmazonIpReputationList","httpRequest":{"clientIp":"192.0.2.99","country":"NL","uri":"/.env","httpMethod":"GET"},"ruleGroupList":[],"nonTerminatingMatchingRules":[]}
{"timestamp":1795392003000,"action":"ALLOW","terminatingRuleId":"Default_Action","httpRequest":{"clientIp":"203.0.113.51","country":"GB","uri":"/v1/accounts/ACC-0000012345","httpMethod":"GET"},"ruleGroupList":[],"nonTerminatingMatchingRules":[]}Run it with dotnet run -- sample-waf-logs.jsonl. Expected output (abridged):
Analysed 5 requests from sample-waf-logs.jsonl
Terminating rules:
BLOCK login-rate-limit 2
ALLOW Default_Action 2
BLOCK AWSManagedRulesAmazonIpReputationList 1
Counted (not blocked) matches — review before switching to Block:
AWSManagedRulesCommonRuleSet:CrossSiteScripting_BODY 1
Top blocked client IPs:
198.51.100.23 2 e.g. POST /v1/auth/login
192.0.2.99 1 e.g. GET /.envThe CrossSiteScripting_BODY count on POST /v1/payments is the interesting line: a customer typed something like Rent <flat 2> in the payment reference. Switching the Core rule set to Block without a targeted override would reject a legitimate payment.
Code explained line by line
WafLogRecordand friends are C# records withJsonPropertyNameattributes mapping AWS's camelCase field names. Only fields you need are declared;System.Text.Jsonignores the rest, which keeps the code resilient to new fields AWS adds.List<WafRuleGroup>?— nullable because some log records omit the property; the code uses?? []before iterating.File.ReadLines(path)streams the file line by line, so a multi-gigabyte S3 export does not need to fit in memory.- The
try/catch (JsonException)skips malformed lines instead of aborting — log exports sometimes contain truncated final lines. GroupBy(r => (r.Action, r.TerminatingRuleId))uses a tuple key to count how many requests each rule ended, per action.Default_Actionmeans no rule terminated the request.- The
countedquery flattens two places where Count matches appear: inside managed rule groups (ruleGroupList[].nonTerminatingMatchingRules) and at web ACL level (nonTerminatingMatchingRules). ShortNameturns a rule-group ID such asAWS#AWSManagedRulesCommonRuleSetinto its readable name.- The top-IP report only looks at blocked requests. The comment is deliberate: IP addresses can be personal data under UK GDPR, so treat the report like any other log extract.
Testing the rate limit locally? You can write a small HttpClient loop that sends 150 POST /v1/auth/login requests to your deployed lab stage and prints status codes. Expect the 429s to start only after a delay: WAF checks rates frequently but not instantly, and AWS documents that enforcement is approximate, so a few extra requests may get through. Never run load against an endpoint you do not own, and never against production without change approval — your load test is indistinguishable from an attack.
Security considerations
⚠ Learning shortcut — not for production
The lab uses a single regional web ACL, a 30-day log retention in the same account, and RemovalPolicy.DESTROY. In production: an edge web ACL on CloudFront plus a regional one, logs delivered to S3 in the log-archive account with retention set by your records-management policy, a Firewall Manager baseline policy, alarms on blocked-request spikes, and change control on every rule edit.
- Never allow-list on spoofable data.
X-Forwarded-For,User-Agentor a custom "internal caller" header can be set by anyone. If you aggregate rate limits on a forwarded IP header, configure it only when a trusted proxy (CloudFront) sets it. - Protect the origin from bypass. If CloudFront carries the WAF, make sure the API Gateway or ALB cannot be called directly — for example with a secret origin header verified by a regional web ACL, or with a VPC origin for ALBs.
- Redact before storing. Authorization headers, session cookies, and query strings with customer identifiers must be redacted in logging configuration. Redaction affects logs only; it does not affect sampled requests or Security Lake collection, which have their own data-protection settings.
- WAF is not authentication. Rate limiting login does not replace MFA, account lockout and risk-based authentication in your identity provider.
- Fraud Control needs privacy review. ATP inspects submitted usernames and passwords (compared against a stolen-credentials database). Your DPO should review this processing; document it in the record of processing activities.
- Shield Advanced contract. Subscribing commits the organisation for a year. Treat it as a procurement decision with finance sign-off, not a console click.
Banking use case
Harbourline's mobile-banking login receives about 40 requests per second at peak. In March, the fraud team sees a credential-stuffing campaign: 2 million login attempts in 6 hours from 30,000 residential proxy IPs, each IP sending only a handful of requests — too few to trip a per-IP rate limit.
The response, agreed by the fraud, security and digital teams:
| Control | Why it helps | Cost consideration |
|---|---|---|
Per-IP rate limit on /v1/auth/login (100 per 5 min) | Stops naive attackers and misbehaving scripts | Base WAF only |
| ATP scoped to the login path | Detects requests with known-compromised credentials and volumetric patterns per session, adding labels you can block on | Subscription plus a per-request fee; volume-based tiers make a scope-down essential |
| Bot Control (Targeted) with the WAF mobile SDK | Token-based client verification distinguishes the real app from scripts | Higher per-request fee; requires app release |
| Step-up MFA in the identity provider for risky logins | Defeats the attack even if credentials are valid | Identity platform cost |
Alert to the fraud team on credential_compromised labels | Customers whose credentials leaked elsewhere get a forced password reset | Operational process |
The architecture review notes that none of these controls makes the login "PCI compliant" or "PSD2 SCA compliant" by itself; Harbourline's compliance team maps each control to its obligations.
Common mistakes
- Deploying managed rules straight to Block → legitimate payments rejected on day one → deploy in Count, review logs, override individual rules, then Block.
- Using
LogGroupArnfrom CDK for WAF logging →:*suffix rejected → build the ARN without it. - Log group name without the
aws-waf-logs-prefix → logging configuration fails → rename. - Creating a CloudFront web ACL outside us-east-1 → it does not appear for association → CloudFront scope lives in us-east-1.
- Per-IP limits set too low → mobile users behind carrier NAT are blocked → base limits on observed p99 per IP, and use header- or session-based aggregation where appropriate.
- Expecting WAF to protect an HTTP API (API Gateway v2) directly → not supported → front it with CloudFront.
- Subscribing to Shield Advanced "to try it" → one-year commitment → evaluate on paper first.
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
WAFInvalidParameterException on logging config | Destination name lacks aws-waf-logs- or ARN ends with :* | Rename the destination; strip the suffix |
WAFLimitsExceededException creating the web ACL | Over 5,000 WCUs, or rate-based rule count over 10 | Run check-capacity; remove or scope rules |
Legitimate requests get 403 with no obvious rule | A managed group rule (e.g. SizeRestrictions_BODY) terminated | Read terminatingRuleId and ruleGroupList in logs; add a rule action override to Count for that rule |
| Rate limit never triggers in testing | Below the limit for the window, or traffic aggregated on a different key, or scope-down path mismatch (case) | Check get-rate-based-statement-managed-keys; add a LOWERCASE transformation |
429 arrives later than expected | Rate checks are periodic and approximate | Expected; design limits with margin |
| Web ACL not listed when associating a resource | Wrong scope or Region | Regional resources need a REGIONAL web ACL in the same Region |
| Customers report CAPTCHA loops in the app | CAPTCHA action on API calls from a native app without the WAF SDK | Use Challenge/CAPTCHA only on browser flows, or integrate the mobile SDK |
Cost and cleanup
| Item (checked 2026-11-17) | Price | Lab exposure |
|---|---|---|
| Web ACL | USD 5.00 per month, prorated hourly | 2 hours ≈ USD 0.01 |
| Rules (each managed group counts as one rule) | USD 1.00 per rule per month, prorated | 5 rules × 2 h ≈ USD 0.01 |
| Requests | USD 0.60 per million | Negligible |
| WCUs above 1,500 | Extra USD 0.20 per million requests per 500 WCUs | Lab stays under 1,500 |
| Bot Control Common / Targeted | USD 10 per month subscription each; per-request fees after free allowance (10M / 1M requests) | Not used in lab |
| Fraud Control ATP / ACFP | USD 10 per month subscription each; tiered per-request fees starting around USD 1 per 1,000 requests after 10,000 free | Not used in lab |
| CloudWatch Logs ingestion | Standard Logs pricing | Cents |
| Shield Advanced | USD 3,000 per month, 1-year commitment | Design only |
Free alternative: skip deployment; run the WafLogReport program on the sample file and write the rules in CDK or Terraform, validated with cdk synth or terraform validate.
# CDK
cdk destroy --profile learn-sandbox
# Terraform
terraform destroy
# CLI-created resources
aws wafv2 delete-logging-configuration --resource-arn <web-acl-arn>
aws wafv2 disassociate-web-acl --resource-arn arn:aws:apigateway:us-east-1::/restapis/a1b2c3d4e5/stages/prod
LOCK=$(aws wafv2 get-web-acl --name hl-m05-accounts-api-acl --scope REGIONAL --id <id> --query LockToken --output text)
aws wafv2 delete-web-acl --name hl-m05-accounts-api-acl --scope REGIONAL --id <id> --lock-token "$LOCK"
aws logs delete-log-group --log-group-name aws-waf-logs-hl-m05-accounts-api
# Confirm nothing tagged for the lab remains, and that Shield Advanced is still INACTIVE
aws resourcegroupstaggingapi get-resources --tag-filters Key=Project,Values=cea-lab --query "ResourceTagMappingList[].ResourceARN"
aws shield get-subscription-stateExercises
- 🟢 Guided: Deploy the CDK stack in Count mode against your Module 4 lab API stage. Send 20 normal requests and 5 requests with
?id=1%27%20OR%201=1in the query string. Export the log group and runWafLogReporton it. Which rule counted the SQL-injection probe? - 🟡 Challenge: Add a second rate-based rule that aggregates on a custom key: the
x-harbourline-customer-idheader combined with IP, limit 30 per minute onPOST /v1/payments. Explain in two sentences why this is safe only if the header is set by your authorizer, not by the client. - 🔴 Live problem: Monday 08:05. The service desk says "online banking is slow and some customers see an error page". CloudWatch shows API Gateway 5xx rising, WAF
AllowedRequests20× normal, and requests spread across 40,000 IPs toGET /v1/accounts/{id}with random IDs. You do not have Shield Advanced. Decide what you would do in the first 30 minutes and what you would recommend afterwards.
Assignment
Create a "WAF rule register" for the Accounts API in your portfolio: one row per rule (priority, statement summary, action, owner, false-positive history, date last reviewed), the CDK stack, and a short decision record explaining whether Harbourline should buy Shield Advanced, with a cost comparison against the expected impact of a four-hour outage of online banking (use fictional figures and state your assumptions).
Solutions
Export logs with:
aws logs filter-log-events --log-group-name aws-waf-logs-hl-m05-accounts-api \
--query "events[].message" --output text | tr '\t' '\n' > waf.jsonl
dotnet run -- waf.jsonlYou should see AWSManagedRulesSQLiRuleSet:SQLi_QUERYARGUMENTS (or a similar SQLi rule ID) in the Counted section five times. Why: in Count mode, the managed group's matches appear as non-terminating matches inside ruleGroupList, and the request is allowed by the default action. That is exactly the evidence you need before switching the group to Block.
Statement = new CfnWebACL.StatementProperty
{
RateBasedStatement = new CfnWebACL.RateBasedStatementProperty
{
Limit = 30,
EvaluationWindowSec = 60,
AggregateKeyType = "CUSTOM_KEYS",
CustomKeys = new object[]
{
new CfnWebACL.RateBasedStatementCustomKeyProperty
{
Header = new CfnWebACL.RateLimitHeaderProperty
{
Name = "x-harbourline-customer-id",
TextTransformations = new[] { new CfnWebACL.TextTransformationProperty { Priority = 0, Type = "NONE" } },
},
},
new CfnWebACL.RateBasedStatementCustomKeyProperty { Ip = new Dictionary<string, object>() },
},
ScopeDownStatement = /* ByteMatch on URI path "/v1/payments" AND method POST */ null,
},
},Each custom key adds 30 WCUs. Why it needs care: WAF runs before your authorizer on API Gateway, so a client can put any value in the header and rotate it to dodge the limit. Use it only behind a layer that sets the header from a validated token (for example CloudFront Functions validating a signed cookie), or use the IP-only limit plus application-level per-customer limits in ASP.NET Core's rate-limiting middleware.
First 30 minutes:
- Declare an incident and open a bridge; this is a customer-facing outage.
- Confirm it is attack traffic, not a release: random IDs across many IPs, no matching deployment.
- Use WAF logs (or sampled requests) to find a distinguishing feature: user agent, missing headers that the real mobile app always sends, countries, JA3/JA4 TLS fingerprints if behind CloudFront ⚠ VERIFY field availability for your resource type.
- Add a rule in Count for that feature, check it matches the attack and not real users within minutes, then switch to Block. Add the
AWSManagedRulesAnonymousIpListgroup if the IPs are hosting providers or proxies. - Add a rate-based rule on
/v1/accounts/aggregated per IP with a limit above normal customer behaviour. - Protect the backend: lower API Gateway stage throttling, increase Lambda reserved concurrency limits only if the database can take it.
Afterwards: front the API with CloudFront (edge absorption, Shield Standard at the edge), enable Bot Control, add an enumeration check in code (customers can only fetch their own accounts — this attack should have produced 403s from the API, not database lookups), run a cost/benefit for Shield Advanced with SRT, and write the post-incident review.
Knowledge check
Answer: A rule's action applies to your own rules. A managed rule group uses an override action: Count makes every rule in the group count instead of block; None lets the group use its own actions. You can also override individual rules inside a group with rule action overrides.
Answer: Minimum limit 10 requests; windows of 60, 120, 300 (default) or 600 seconds.
Answer: WAF requires the log group ARN without the trailing :*, which CDK's LogGroupArn includes. Also, the log group name must begin with aws-waf-logs-.
Answer: CloudFront distributions, Route 53 hosted zones, Global Accelerator accelerators, Application and Classic Load Balancers, and Elastic IP addresses. SRT engagement requires a Business or Enterprise Support plan in addition to the Shield Advanced subscription.
Answer: It works (the hard maximum is 5,000), but every request incurs an additional charge for the WCUs above 1,500, in 500-WCU increments.
Interview questions
- "How would you roll out WAF on an existing banking API without breaking customers?" — Inventory endpoints; deploy baseline managed groups in Count with logging and redaction; run representative traffic; review counted matches; override specific false-positive rules; switch to Block in stages with alarms; document every rule and owner.
- "Is WAF enough to stop credential stuffing?" — No. It reduces volume (rate limits, ATP, bot control), but you also need MFA, risk-based authentication, breached-password checks, monitoring of login success rates, and customer notification processes.
- "When would you recommend Shield Advanced?" — When downtime of internet-facing services has material financial or regulatory impact, there are multiple protected resources, the organisation wants 24/7 DDoS experts (SRT), cost protection and centrally managed WAF via Firewall Manager — and leadership accepts a one-year USD 3,000/month commitment.
- "Where should WAF sit: CloudFront or ALB?" — Prefer the edge for absorption and global visibility, with origin-bypass protection; keep a regional ACL where traffic may reach the origin directly, or for internal ALBs exposed via other paths.
Certification practice
A bank's login endpoint behind CloudFront receives a credential-stuffing attack from thousands of IP addresses, each sending a few requests per hour. The bank wants to block requests that use credentials known to be leaked, with minimal application changes. What should a security engineer do?
- A. Add a rate-based rule with a limit of 10 requests per 5 minutes per IP
- B. Add the AWS WAF Fraud Control account takeover prevention (ATP) managed rule group, scoped to the login path, and configure the login request fields
- C. Add the Amazon IP reputation list managed rule group only
- D. Subscribe to Shield Advanced and request SRT assistance
Answer: B. ATP checks submitted credentials against a stolen-credential database and tracks login behaviour, which is exactly this attack. A is too coarse for low-and-slow distributed traffic and would hurt users behind shared IPs. C helps only against known-bad IPs, not residential proxies. D targets DDoS, not application-level account takeover.
An auditor finds that AWS WAF logs in CloudWatch Logs contain customers' bearer tokens in the Authorization header. What change prevents this while keeping the rest of the request data for investigations?
- A. Disable WAF logging and rely on sampled requests
- B. Add the
Authorizationheader to the web ACL logging configuration's redacted fields - C. Encrypt the log group with a customer managed KMS key
- D. Add a WAF rule that strips the
Authorizationheader from requests
Answer: B. Redacted fields replace the value in logs. A loses investigative evidence. C protects data at rest but tokens are still readable by anyone with log access. D — WAF does not remove request headers, and doing so would break authentication.
A company needs to protect a public Application Load Balancer from SQL injection and cross-site scripting attacks and wants to rate-limit abusive clients. Which service should the architect use?
- A. AWS Shield Standard
- B. AWS WAF with managed rule groups and a rate-based rule, associated with the ALB
- C. Network ACLs on the ALB subnets
- D. AWS Network Firewall in front of the ALB
Answer: B. WAF inspects HTTP requests for injection patterns and supports rate-based rules. A protects against network/transport-layer DDoS, not SQLi. C filters by IP and port only. D can inspect traffic but is aimed at VPC-level network filtering and does not provide managed web-application rule groups or per-client HTTP rate limiting in the same way.
A bank has 120 AWS accounts. Security policy requires that every internet-facing Application Load Balancer and API Gateway stage has a baseline set of WAF rules, including in accounts created in future. What is the most scalable solution?
- A. A Lambda function in each account that creates a web ACL nightly
- B. AWS Firewall Manager WAF policies applied from a delegated administrator account to the relevant OUs, with automatic remediation
- C. An SCP that denies
elasticloadbalancing:CreateLoadBalancerunless a web ACL exists - D. AWS Config managed rules that report non-compliant ALBs
Answer: B. Firewall Manager centrally applies and remediates WAF policies across accounts and new resources. A is fragile custom code. C — SCPs cannot evaluate whether a web ACL is associated. D detects but does not enforce.
Completion checklist
- I can explain web ACLs, rule priority, managed groups and WCUs in two sentences
- I deployed a web ACL in Count mode, read the logs and understood each match
- I wrote a rate-based rule with a custom ProblemDetails
429 - I configured redaction of the
Authorizationheader and query string - I can compare Shield Standard and Advanced, including cost and commitment
- I destroyed the web ACL and log group and confirmed Shield Advanced is still inactive
Key takeaways
- WAF filters HTTP requests to supported AWS resources; Shield Standard protects every customer at layers 3 and 4 at no cost.
- Roll out managed rules in Count, tune with logs, then Block — banking payloads cause real false positives.
- Rate-based rules need a scope-down, sensible limits (minimum 10, windows 1–10 minutes) and an understanding that enforcement is approximate.
- Logs are evidence and also personal data: name them
aws-waf-logs-…, redact secrets, and set retention. - Shield Advanced (USD 3,000/month, 1-year commitment) is a business decision about DDoS risk appetite, not a technical toggle. AWS protects its edge; Harbourline owns its rules, tuning and incident process.
Further reading
- AWS WAF quotas
- Using rate-based rule statements in AWS WAF
- Rate-based rule high-level settings
- AWS Managed Rules rule groups list
- Account takeover prevention (ATP) rule group
- Logging AWS WAF web ACL traffic
- AWS Shield Advanced overview
- Shield Response Team support
- AWS WAF pricing · AWS Shield pricing
- AWS Shield network security director (preview announcement)
Independent learning resource — not affiliated with, endorsed or sponsored by Amazon Web Services. AWS and related marks are trademarks of Amazon.com, Inc. or its affiliates. All banking data in examples is fictional. Sample architectures are for learning and are not automatically compliant with GDPR, PCI DSS or any banking regulation.
Segmentation, private endpoints and egress filtering
Layer security groups, NACLs, VPC endpoint policies, DNS Firewall and AWS Network Firewall so a banking workload can reach only the services and partners it genuinely needs.
Zero-trust principles and AWS Verified Access (overview)
Replace 'inside the network means trusted' with per-request decisions based on identity, device posture and context, and see how AWS Verified Access gives Harbourline staff VPN-less access to internal banking tools.