Fn AWS Cloud Front Normalized
Query
// REVIEW ONLY - Reusable Workspace Function Spec: fn_AWS_CloudFrontNormalized
// Purpose: Resilient CloudFront access-log normalizer using column_ifexists() so custom S3/CCF DCR tables
// (AWSCloudFront_AccessLog_CL) do not fail compilation when field casing or optional columns vary.
// Validate actual schema first using discovery/D08-cloudfront-and-netfw-discovery.kql.
let Lookback = 24h;
AWSCloudFront_AccessLog_CL
| where TimeGenerated > ago(Lookback)
| extend ClientIp = tostring(column_ifexists("c_ip_s", column_ifexists("ClientIp", ""))),
DistributionDomain = tostring(column_ifexists("cs_host_s", column_ifexists("x_host_header_s", column_ifexists("HostHeader", "")))),
UriStem = tostring(column_ifexists("cs_uri_stem_s", column_ifexists("UriStem", ""))),
UriQuery = tostring(column_ifexists("cs_uri_query_s", column_ifexists("UriQuery", ""))),
StatusCode = toint(column_ifexists("sc_status_d", column_ifexists("sc_status_s", column_ifexists("StatusCode", 0)))),
UserAgent = tostring(column_ifexists("cs_user_agent_s", column_ifexists("UserAgent", ""))),
CookieHeader = tostring(column_ifexists("cs_cookie_s", "")),
EdgeResultType = tostring(column_ifexists("x_edge_result_type_s", column_ifexists("EdgeResultType", ""))),
EdgeLocation = tostring(column_ifexists("x_edge_location_s", column_ifexists("EdgeLocation", ""))),
BytesSent = tolong(column_ifexists("sc_bytes_d", column_ifexists("BytesSent", 0)))
| extend HasSignedArtifact = (UriQuery has_any ("Policy=", "Signature=", "Key-Pair-Id=") or CookieHeader has_any ("CloudFront-Policy", "CloudFront-Signature", "CloudFront-Key-Pair-Id"))
| project TimeGenerated, IngestTime=ingestion_time(), ClientIp, DistributionDomain, UriStem, UriQuery,
StatusCode, UserAgent, HasSignedArtifact, EdgeResultType, EdgeLocation, BytesSent
| take 2000Explanation
This query is designed to process and normalize access logs from AWS CloudFront. Here's a simple breakdown of what it does:
-
Lookback Period: It considers logs generated in the last 24 hours.
-
Data Source: It pulls data from the
AWSCloudFront_AccessLog_CLtable. -
Field Normalization: It uses the
column_ifexists()function to handle variations in field names and optional columns. This ensures the query doesn't fail if the log schema changes or if certain columns are missing. It extracts and normalizes the following fields:ClientIp: The IP address of the client.DistributionDomain: The domain of the CloudFront distribution.UriStem: The URI stem of the request.UriQuery: The query part of the URI.StatusCode: The HTTP status code returned.UserAgent: The user agent string from the client.CookieHeader: The cookie header from the request.EdgeResultType: The result type from the CloudFront edge location.EdgeLocation: The location of the CloudFront edge server.BytesSent: The number of bytes sent in the response.
-
Signed Artifacts Check: It checks if the request URI or cookies contain signed artifacts, which are indicators of secure content delivery.
-
Projection: It selects specific fields to output, including the time the log was generated, the ingestion time, and the normalized fields.
-
Limit: It limits the output to the first 2000 records.
Overall, this query is used to create a consistent and resilient view of CloudFront access logs, accommodating variations in the log schema.