Sub topic 9
Querying historical events with CloudTrail Lake and Athena
This chapter assumes you can read a CloudTrail event record, you know the difference between Event history and a trail, and you have a trail that delivers logs to S3. You should also be comfortable with basic SQL and the AWS CLI.
Why Event history is not enough
Event history covers only management events from the last 90 days, one Region at a time. Its filters are limited to a single attribute per search. Audits and investigations often need more: a year of data, data events, and questions such as "which IAM users made the most calls this month?"
AWS offers two ways to run SQL over your CloudTrail data.
| Option | Where the data lives | You manage | Best for |
|---|---|---|---|
| CloudTrail Lake | An event data store inside CloudTrail | Retention and ingestion settings | Fast setup, ad hoc SQL, cross-account queries |
| Athena on trail logs | The S3 bucket your trail already writes to | A table, partitions, and query results bucket | Reusing existing logs, paying per data scanned |
Querying with CloudTrail Lake
CloudTrail Lake stores events in an event data store. You choose which event types it ingests and how long it keeps them. The data store has an ID, and that ID is the table name in your queries.
Lake queries use the same field names you saw in the event record. Nested fields such as userIdentity.type use dot notation. This query lists the ten most frequent API calls made by IAM users since the start of the month.
SELECT eventName, COUNT(*) AS calls
FROM <event-data-store-id>
WHERE eventTime > '2026-11-01 00:00:00'
AND userIdentity.type = 'IAMUser'
GROUP BY eventName
ORDER BY calls DESC
LIMIT 10Replace <event-data-store-id> with the ID shown in the console or returned by aws cloudtrail list-event-data-stores. Always filter on eventTime. Lake bills by the amount of data a query scans, and a time filter reduces that amount.
A Lake data store only holds events from the time you create it. It does not backfill history, unless you import existing trail logs from S3.
Querying with Athena
If you already deliver logs to S3, Athena can query those files in place. You create a table that maps to the trail's S3 location. The CloudTrail console can generate the table definition for you from the trail's details page.
Two practical points matter here:
- Partitioning. Logs sit under a path that includes account, Region, and date. A table with partitions on those values lets Athena read only the folders you need. Without partitions, every query scans the whole bucket and costs more.
- Results location. Athena writes query results to an S3 location you choose. Treat that bucket as sensitive, because results can contain the same details as the logs.
Athena reads whatever is in the bucket, so it relies on the bucket's own protections. Validate log integrity first if you need evidence you can trust.
Common errors
- Empty results. The data store was created after the activity happened, or the time filter excludes it. Check the date range and the store's creation time.
- Missing data events. Neither service shows events your trail or data store was not configured to capture. Data events must be selected explicitly.
- Access denied. Querying needs permissions on CloudTrail Lake or Athena, plus read access to the S3 log bucket and the KMS key if the logs are encrypted.
- Unexpectedly high cost. Queries without a time filter or partition filter scan everything.
Exercise
A teammate runs a CloudTrail Lake query for last month's S3 object deletions and gets zero rows, although you know deletions happened. Name two likely causes.
Show answer
First, the event data store may not ingest data events, because S3 object-level events must be selected explicitly. Second, the event data store may have been created after the deletions happened, since Lake does not backfill history on its own. An incorrect eventTime filter is also possible.
Exercise
Why should an Athena table over CloudTrail logs be partitioned by date?
Show answer
Partitions let Athena read only the S3 folders for the dates in the query. This reduces the data scanned, which lowers cost and speeds up the query.