.png)
Introduction
It’s 2 a.m., and the dashboard shows a sudden spike in 403 responses. Is it an attack, a misfiring WAF rule or a broken deployment? The answer is in the logs, but AWS WAF, Amazon CloudFront and the Application Load Balancer (ALB) can generate millions of log lines a day. Reading them by hand slows the investigation and makes it easy to miss the pattern that matters.
Amazon Athena lets SOC analysts query those logs where they already sit in Amazon S3, using standard SQL and no servers to manage. This post walks through the queries and workflow we use to go from a suspicion to evidence, and from evidence to a safe mitigation.
How Athena fits into the log pipeline
WAF, CloudFront and ALB deliver logs to S3. The AWS Glue Data Catalog holds the table definitions, and Athena runs SQL on top of them.
Figure 1: From raw logs to SOC evidence
Two things are worth getting right from day one. First, create your tables with partition projection, using the DDL in the Athena documentation. Athena then recognises new partitions automatically, so there are no crawlers to schedule and no partitions to add by hand.
Second, know your delivery delays. WAF and ALB deliver logs to S3 roughly every five minutes. CloudFront usually delivers within an hour, but some entries can arrive up to 24 hours later. Athena is an investigation and threat-hunting tool; real-time alerting still belongs in CloudWatch metrics or your SIEM.
Note: The SQL in this post is illustrative. It assumes tables named waf_logs, alb_access_logs and cloudfront_standard_logs, created from the AWS-documented DDL with partition projection. Adjust table names, columns and partition keys to match your environment.
Finding blocked requests in AWS WAF
The first question in most WAF investigations is simple: what is being blocked, from where, and by which rule?
SELECT
from_unixtime(timestamp / 1000) AS event_time,
terminatingruleid,
httprequest.clientip AS client_ip,
httprequest.httpmethod AS method,
httprequest.uri AS uri,
httprequest.country AS country
FROM waf_logs
WHERE log_time BETWEEN '2026/11/26/00/00' AND '2026/11/26/23/59'
AND action = 'BLOCK'
ORDER BY timestamp DESC
LIMIT 100;
WAF stores timestamps in epoch milliseconds, so from_unixtime() makes them readable, and the log_time filter limits the scan to a single day.
[Screenshot 1: Athena console showing the blocked requests query and its results. Mask client IPs, account IDs, bucket and web ACL names.]
This quickly shows repeated blocks, the rules responsible and the endpoints being targeted. It also reveals rules that block legitimate traffic, which is where WAF tuning begins.
Which managed rule actually fired?
If you use AWS Managed Rules, terminatingruleid shows the rule group, such as the Core Rule Set, not the individual rule that matched. For tuning, you need the rule inside the group:
SELECT
rg.rulegroupid,
rg.terminatingrule.ruleid AS rule_id,
COUNT(*) AS blocked_requests
FROM waf_logs
CROSS JOIN UNNEST(rulegrouplist) AS t(rg)
WHERE log_time BETWEEN '2026/11/26/00/00' AND '2026/11/26/23/59'
AND action = 'BLOCK'
AND rg.terminatingrule IS NOT NULL
GROUP BY rg.rulegroupid, rg.terminatingrule.ruleid
ORDER BY blocked_requests DESC
LIMIT 20;
A single noisy rule, such as a body-size check firing on a legitimate file-upload API, is far easier to fix than an entire rule group.
High-volume blocked IPs need context
Grouping blocked requests by source IP highlights scanning and automated activity:
SELECT
httprequest.clientip AS client_ip,
httprequest.country AS country,
COUNT(*) AS blocked_requests,
COUNT(DISTINCT httprequest.uri) AS distinct_uris,
array_agg(DISTINCT terminatingruleid) AS rules_hit
FROM waf_logs
WHERE log_time BETWEEN '2026/11/26/00/00' AND '2026/11/26/23/59'
AND action = 'BLOCK'
GROUP BY httprequest.clientip, httprequest.country
ORDER BY blocked_requests DESC
LIMIT 20;
[Screenshot 2: Top blocked IP addresses with request counts. Mask or replace real IPs.]
A high count alone does not prove malicious intent; it could be a partner integration, a monitoring service or a corporate NAT. The distinct URI count adds context. One IP hitting hundreds of different paths looks like scanning, while thousands of requests to a single login endpoint looks like credential stuffing. Attackers using rotating proxies spread requests across many IPs, so rate-based rules and behavioural signals usually work better than blocking one address at a time.
Separating security errors from application errors
HTTP 4xx and 5xx responses can mean an attack, a misconfiguration or a failing backend. ALB logs help tell them apart because they record both the load balancer’s status code and the target’s.
SELECT
url_extract_path(request_url) AS path,
elb_status_code,
target_status_code,
COUNT(*) AS error_count
FROM alb_access_logs
WHERE day = '2026/11/26'
AND elb_status_code BETWEEN 500 AND 599
GROUP BY url_extract_path(request_url), elb_status_code, target_status_code
ORDER BY error_count DESC
LIMIT 50;
[Screenshot 3: ALB 5xx results showing ELB and target status codes.]
A target_status_code of “-” means the target never responded, so the ALB generated the error itself, often a 502 or 504 from an unhealthy or slow backend. Grouping on url_extract_path() instead of the raw URL stops query strings from splitting one endpoint into thousands of rows. The same approach works for CloudFront: filter sc_status between 400 and 599 and group by cs_uri_stem.
Correlating WAF, CloudFront and ALB
Correlation is where investigations are won or lost, and IP addresses alone can mislead you.
Figure 2: Which log sees what, and how to join them
When CloudFront sits in front of an ALB, the ALB access log records a CloudFront edge address as the client, not the visitor, and it does not include the X-Forwarded-For header. The reliable link between WAF and CloudFront is the request ID. For a web ACL attached to CloudFront, the WAF requestid matches CloudFront’s x_edge_request_id; for a web ACL attached to an ALB, it is the ALB trace ID.
SELECT
from_unixtime(w.timestamp / 1000) AS event_time,
w.httprequest.clientip AS client_ip,
w.action,
w.terminatingruleid,
c.cs_uri_stem,
c.sc_status,
c.x_edge_result_type
FROM waf_logs w
JOIN cloudfront_standard_logs c
ON w.httprequest.requestid = c.x_edge_request_id
WHERE w.log_time BETWEEN '2026/11/26/00/00' AND '2026/11/26/23/59'
AND c."date" = DATE '2026-11-26'
AND c.sc_status >= 400
LIMIT 100;
Rows where WAF allowed the request but CloudFront still returned an error point to the origin. Rows where WAF blocked it show a security control working as intended. To follow a request on to the ALB, correlate on time window and path, or log the X-Amz-Cf-Id or X-Forwarded-For header in your application.
Partitioning keeps queries fast and cheap
Athena charges for the data each query scans, with a 10 MB minimum per query, so an unfiltered query over months of logs is slow and expensive.
- Always filter on the partition column: log_time for WAF, day for ALB.
- Select only the columns you need instead of every column.
- Convert large or long-lived datasets to compressed Parquet. For CloudFront, standard logging (v2) can deliver partitioned Parquet output.
- Set a per-query data-scan limit in your Athena workgroup, so one mistyped query cannot scan terabytes.
[Screenshot 4: Athena query stats showing data scanned with and without a partition filter.]
Protect the logs as evidence
During an incident, logs become evidence. Treat the bucket accordingly:
- Encrypt with SSE-KMS, and give analysts read-only access through least-privilege IAM roles.
- Enable versioning and S3 Object Lock, so logs cannot be altered or deleted during an investigation.
- Redact sensitive fields such as Authorization and Cookie headers in WAF logging, and leave CloudFront cookie logging off unless you need it.
- Encrypt Athena query results, and set S3 lifecycle rules that match your retention policy.
From investigation to mitigation
Athena does not block threats itself. It gives SOC teams the evidence to act with confidence.
Figure 3: The SOC investigation loop
Keep mitigations as narrow as the evidence: a scoped rule exclusion for one path, a rate-based rule for a burst, or an IP set for a confirmed bad actor. Then re-run the same queries on later logs to prove the change worked.
Tune WAF rules safely with COUNT mode
Figure 4: Tune WAF rules safely with COUNT mode
Deploy new or changed rules in COUNT mode first. COUNT matches do not appear in BLOCK results; WAF records them in nonterminatingmatchingrules:
SELECT
r.ruleid,
httprequest.uri AS uri,
COUNT(*) AS would_have_blocked
FROM waf_logs
CROSS JOIN UNNEST(nonterminatingmatchingrules) AS t(r)
WHERE log_time BETWEEN '2026/11/26/00/00' AND '2026/11/26/23/59'
AND r.action = 'COUNT'
GROUP BY r.ruleid, httprequest.uri
ORDER BY would_have_blocked DESC
LIMIT 50;
If you override rules inside a managed rule group to COUNT, look in rulegrouplist instead. Once the results show only traffic you intend to stop, switch the rule to BLOCK and keep monitoring.
What changed for our SOC
[Author to add 2–3 real, anonymised results before publishing, for example: average WAF alert investigation time down from X to Y minutes; data scanned per query reduced by Z% after partition projection; N false-positive rules tuned in the first month. If no figures can be shared, keep the bullets below and delete this note.]
- Faster triage of blocked requests and suspicious IPs
- Clear separation of security events from application errors
- Evidence-based WAF tuning with fewer false positives
- Predictable, lower query costs through partitioning
Conclusion
Logs are only useful when you can read them quickly. Collecting them is the easy part. Knowing what normal looks like, spotting what is dangerous and acting on it with evidence is what lets a SOC respond faster and protect applications without adding noise.
Over to you: which log source gives your SOC the most trouble today: WAF, CloudFront or ALB? Tell us in the comments. [Marketing: add Cloud.in contact or services link here.]
The blog is written by Onkar Sadiwal,Soc Analyst, Cloud.in
No comments:
Post a Comment