06 Data Health Per Account Source Freshness And Gb
Query
// REVIEW ONLY - AWS Security Operations Workbook Visual Query
// Tab: DataHealth | Panel: per-account-source-freshness-and-gb | Visualization: table | Mode: All
// Security Question: Are all expected AWS accounts and log tables delivering within SLA, and what is the billable volume?
// Drill-Through: Cross-check accounts marked MISSING_EXPECTED_TELEMETRY against AWS_AccountCoverage watchlist and Panel 2.2.
// Empty/No-Data Contract: No AWS events or AWS_AccountCoverage rows matched the selected account/region filters.
let Start = {TimeRange:start};
let End = {TimeRange:end};
let OrgFilter = base64_decode_tostring("{Organization:base64}");
let AccountFilter = base64_decode_tostring("{AwsAccount:base64}");
let RegionFilter = base64_decode_tostring("{AwsRegion:base64}");
let EnvFilter = base64_decode_tostring("{Environment:base64}");
let WorkloadFilter = base64_decode_tostring("{Workload:base64}");
let SeverityFilter = base64_decode_tostring("{Severity:base64}");
let SourceFilter = base64_decode_tostring("{SourceTable:base64}");
let SummaryPref = base64_decode_tostring("{SummaryMode:base64}");
let SelectedPrincipalFilter = base64_decode_tostring("{SelectedPrincipal:base64}");
let SelectedIpFilter = base64_decode_tostring("{SelectedIp:base64}");
let ExpectedAccounts = union isfuzzy=true
(datatable(SearchKey:string)[]),
(_GetWatchlist('AWS_AccountCoverage'))
| where isnotempty(tostring(column_ifexists("SearchKey", "")))
| project ExpectedAccount=tostring(column_ifexists("AWSAccountId", "")),
CloudTrailExpected=tostring(column_ifexists("CloudTrailExpected", "")),
S3DataEventsExpected=tostring(column_ifexists("S3DataEventsExpected", ""))
| where isnotempty(ExpectedAccount);
let PerAccountSource = union isfuzzy=true
(datatable(SourceTable:string, TimeGenerated:datetime, Received:datetime, AWSAccountId:string, AWSRegion:string, Billed:real)[]),
(AWSCloudTrail | where TimeGenerated between (Start .. End) | project SourceTable="AWSCloudTrail", TimeGenerated, Received=todatetime(column_ifexists("_TimeReceived", TimeGenerated)), AWSAccountId=RecipientAccountId, AWSRegion, Billed=todouble(_BilledSize)),
(AWSGuardDuty | where TimeGenerated between (Start .. End) | project SourceTable="AWSGuardDuty", TimeGenerated, Received=todatetime(column_ifexists("_TimeReceived", TimeGenerated)), AWSAccountId=AccountId, AWSRegion=Region, Billed=todouble(_BilledSize)),
(AWSVPCFlow | where TimeGenerated between (Start .. End) | project SourceTable="AWSVPCFlow", TimeGenerated, Received=todatetime(column_ifexists("_TimeReceived", TimeGenerated)), AWSAccountId=AccountId, AWSRegion=Region, Billed=todouble(_BilledSize)),
(AWSRoute53Resolver | where TimeGenerated between (Start .. End) | project SourceTable="AWSRoute53Resolver", TimeGenerated, Received=todatetime(column_ifexists("_TimeReceived", TimeGenerated)), AWSAccountId=AccountId, AWSRegion=Region, Billed=todouble(_BilledSize)),
(AWSWAF | where TimeGenerated between (Start .. End) | project SourceTable="AWSWAF", TimeGenerated, Received=todatetime(column_ifexists("_TimeReceived", TimeGenerated)), AWSAccountId=WebAclId, AWSRegion=HttpSourceName, Billed=todouble(_BilledSize)),
(AWSNetworkFirewallAlert | where TimeGenerated between (Start .. End) | project SourceTable="AWSNetworkFirewallAlert", TimeGenerated, Received=todatetime(column_ifexists("_TimeReceived", TimeGenerated)), AWSAccountId=FirewallName, AWSRegion=AvailabilityZone, Billed=todouble(_BilledSize))
| where (AccountFilter == "*" or AWSAccountId == AccountFilter) and (RegionFilter == "*" or AWSRegion == RegionFilter) and (SourceFilter == "*" or SourceTable == SourceFilter)
| summarize Events=count(), RegionsSeen=dcount(AWSRegion), LastEventUtc=max(TimeGenerated), LastReceivedUtc=max(Received),
MedianLagMin=round(max_of(0.0, percentile(datetime_diff('second', Received, TimeGenerated), 50)) / 60.0, 1),
P95LagMin=round(max_of(0.0, percentile(datetime_diff('second', Received, TimeGenerated), 95)) / 60.0, 1),
BillableGB=round(sum(Billed) / 1073741824.0, 4)
by SourceTable, AWSAccountId;
ExpectedAccounts
| where AccountFilter == "*" or ExpectedAccount == AccountFilter
| join kind=fullouter PerAccountSource on $left.ExpectedAccount == $right.AWSAccountId
| extend EffectiveAccount = coalesce(iff(isnotempty(AWSAccountId), AWSAccountId, ""), ExpectedAccount)
| where isnotempty(EffectiveAccount)
| extend FreshnessMin = iff(isnull(LastEventUtc), -1.0, round(datetime_diff('second', now(), LastEventUtc) / 60.0, 1)),
HealthState = case(
isnull(LastEventUtc) and tolower(CloudTrailExpected) == "true", "🔴 MISSING_EXPECTED_TELEMETRY",
isnull(LastEventUtc), "🟡 NO_DATA_IN_WINDOW",
round(datetime_diff('second', now(), LastEventUtc) / 60.0, 1) > 120.0, "🔴 STALE_SOURCE",
P95LagMin > 45.0, "🟠UPSTREAM_OR_QUEUE_LAG",
"🟢 HEALTHY")
| project EffectiveAccount, SourceTable=coalesce(iff(isnotempty(SourceTable), SourceTable, ""), "ExpectedInWatchlist"), HealthState,
Events=coalesce(Events, 0), RegionsSeen=coalesce(RegionsSeen, 0), LastEventUtc, LastReceivedUtc,
FreshnessMin, MedianLagMin, P95LagMin, BillableGB, CloudTrailExpected, S3DataEventsExpected
| order by HealthState asc, BillableGB desc
| take 5000Explanation
This query is part of an AWS Security Operations Workbook and is designed to assess the health and data delivery status of AWS accounts and log tables. Here's a simplified breakdown of what the query does:
-
Setup and Filters: The query starts by setting up various filters based on time range, organization, AWS account, region, environment, workload, severity, source table, summary mode, selected principal, and selected IP. These filters are decoded from base64 strings.
-
Expected Accounts: It retrieves a list of expected AWS accounts from a watchlist called
AWS_AccountCoverage. This list includes information about whether CloudTrail and S3 data events are expected for each account. -
Data Collection: The query gathers data from various AWS services (like CloudTrail, GuardDuty, VPC Flow Logs, etc.) within the specified time range. It collects information such as the source table, time generated, time received, AWS account ID, AWS region, and billed size.
-
Data Analysis: For each source table and AWS account, it calculates:
- The number of events.
- The number of unique regions seen.
- The time of the last event and when it was received.
- The median and 95th percentile lag times between event generation and reception.
- The total billable volume in gigabytes.
-
Health Assessment: The query joins the expected accounts with the collected data and assesses the health of each account-source combination. It assigns a health state based on:
- Missing expected telemetry.
- No data within the time window.
- Stale data sources.
- Upstream or queue lag.
- Healthy status.
-
Output: The results are projected into a table showing the effective account, source table, health state, event count, regions seen, last event and received times, freshness, lag times, billable volume, and expectations for CloudTrail and S3 data events. The results are ordered by health state and billable volume, and up to 5000 records are returned.
In essence, this query helps determine if all expected AWS accounts and log tables are delivering data within the service level agreement (SLA) and provides insights into the billable data volume.