Skip to content

Analyzing Cloudflare Logs with AWS Athena

aws, cloudflare, athena1 min read

As with many features with Cloudflare, you can enable their Logpush service with the click of a button. Logpush sends your HTTP request logs to your cloud storage provider every 5 minutes.

If you are using AWS S3 for your storage, you can then utilize Athena to analyze your logs.

Athena is an interactive query service that makes it easy to analyze data in Amazon S3 using standard SQL.

So with a little setup and some simple SQL, you can analyze your Cloudflare logs.

Table DDL

In order to being querying, however, you need to create an external table in Athena that matches the format of your Cloudflare logs, which are JSON with a newline delineating each record.

Luckily, this is pretty easy to setup in Athena. Here is the DDL for all of the fields currently included in Cloudflare Logpush.

Note: You can customize the fields that Logpush includes, so if you have, your list of fields may not match the below exactly.

1CREATE EXTERNAL TABLE cloudflare_logs (
2 CacheCacheStatus string,
3 CacheResponseBytes int,
4 CacheResponseStatus int,
5 CacheTieredFill boolean,
6 ClientASN int,
7 ClientCountry string,
8 ClientDeviceType string,
9 ClientIP string,
10 ClientIPClass string,
11 ClientRequestBytes int,
12 ClientRequestHost string,
13 ClientRequestMethod string,
14 ClientRequestPath string,
15 ClientRequestProtocol string,
16 ClientRequestReferer string,
17 ClientRequestURI string,
18 ClientRequestUserAgent string,
19 ClientSSLCipher string,
20 ClientSSLProtocol string,
21 ClientSrcPort int,
22 EdgeColoID int,
23 EdgeEndTimestamp string,
24 EdgePathingOp string,
25 EdgePathingSrc string,
26 EdgePathingStatus string,
27 EdgeRateLimitAction string,
28 EdgeRateLimitID int,
29 EdgeRequestHost string,
30 EdgeResponseBytes int,
31 EdgeResponseCompressionRatio double,
32 EdgeResponseContentType string,
33 EdgeResponseStatus int,
34 EdgeServerIP string,
35 EdgeStartTimestamp string,
36 FirewallMatchesActions ARRAY < string >,
37 FirewallMatchesSources ARRAY < string >,
38 FirewallMatchesRuleIDs ARRAY < string >,
39 OriginIP string,
40 OriginResponseBytes int,
41 OriginResponseHTTPExpires string,
42 OriginResponseHTTPLastModified string,
43 OriginResponseStatus int,
44 OriginResponseTime bigint,
45 OriginSSLProtocol string,
46 ParentRayID string,
47 RayID string,
48 SecurityLevel string,
49 WAFAction string,
50 WAFFlags string,
51 WAFMatchedVar string,
52 WAFProfile string,
53 WAFRuleID string,
54 WAFRuleMessage string,
55 WorkerCPUTime int,
56 WorkerStatus string,
57 WorkerSubrequest boolean,
58 WorkerSubrequestCount int,
59 ZoneID bigint)
60ROW FORMAT SERDE '' LOCATION 's3://my-cloudflare-logs/'

Of course, change s3://my-cloudflare-logs/ to the name of your bucket that you used when setting up Logpush.


An now that we have a table created in Athena, we can analyze our logs in a myriad of ways.

How about checking to see how many requests you've received by the request protocol?

1SELECT count(*) as requests,
2 c.clientrequestprotocol
3FROM "cloudflare_logs"."cloudflare_logs" c
4GROUP BY c.clientrequestprotocol
5ORDER BY count(*) DESC limit 10;
1requests clientrequestprotocol
21 15063737 HTTP/2
32 6842951 HTTP/1.1
43 4342 HTTP/1.0

Or maybe for reasons unknown, you want to see the average client request size in bytes for today, grouped by the Cloudflare edge colo ID.

1SELECT avg(clientrequestbytes),
2 edgecoloid
3FROM "cloudflare_logs"."cloudflare_logs"
4-- Assuming you are using the default date format with Logpush
5WHERE date_trunc('day', from_iso8601_timestamp(edgestarttimestamp)) = current_date
6GROUP BY edgecoloid
7ORDER BY avg(clientrequestbytes) ASC;

As you can imagine, the ways that you can slice and dice your Cloudflare HTTP logs is nearly limitless. Enjoy diving deep on your Cloudflare logs!