Skip to main content
This lesson shows an end-to-end workflow to query AWS CloudTrail logs using Amazon Athena. You’ll create a CloudTrail trail that delivers logs to an S3 bucket, create an Athena external table mapped to those JSON logs, configure Athena query results, and run SQL to analyze API activity. What you’ll learn:
  • Create a CloudTrail trail that writes logs to S3
  • Inspect CloudTrail objects in the S3 console
  • Auto-create an Athena external table for CloudTrail JSON
  • Configure Athena query results (S3)
  • Run SQL queries to analyze CloudTrail events
Prerequisites:
  • AWS account with permissions to create CloudTrail trails, S3 buckets, and Athena workgroups
  • Basic knowledge of SQL for simple SELECT queries

1) Create a CloudTrail trail and S3 delivery

  1. Open the CloudTrail console in the AWS Console. CloudTrail records API activity in your AWS account and can deliver logs to S3 for long-term storage and analysis.
  2. Create a new trail and give it a name (in this demo the default name remains management-events1). Configure the trail to store logs in the CloudTrail S3 bucket. The console shows the target S3 bucket where CloudTrail will deliver logs.
A screenshot of the AWS CloudTrail "Quick trail create" page showing the trail log bucket and folder field and an informational note that S3 storage may incur charges. The bottom-right displays a "Create trail" button.
  1. Click Create trail. After creation, the trail appears in the list with its Region, name (for example, management-events1), and the S3 bucket created for CloudTrail logs. Example ARN for the trail (single-line format):
  1. Open the target S3 bucket in a new tab to verify CloudTrail’s delivery location. Newly-created trails may take a short time before objects appear.

2) Inspect the CloudTrail S3 bucket

In the S3 console you’ll find the bucket CloudTrail created for log storage. CloudTrail writes logs under a prefix similar to AWSLogs/<account-id>/CloudTrail/.
A screenshot of the Amazon S3 web console showing a "General purpose buckets" list with several bucket names, AWS regions, creation dates, and buttons like Create bucket, Copy ARN, Empty, and Delete.
Open the bucket to inspect objects — you should see a CloudTrail/ folder then your account ID and subsequent CloudTrail log folders. Immediately after creation, logs may not yet be present until API events occur.
A screenshot of the Amazon S3 web console showing the Objects view for a bucket named aws-cloudtrail-logs... . It lists a single CloudTrail/ folder and displays controls like Upload, Copy S3 URI, Download, Actions, and Create folder.

3) Create an Athena table for CloudTrail JSON logs

CloudTrail provides an Event history helper in the console which can generate and create an Athena table for you. That helper builds a CREATE EXTERNAL TABLE statement mapped to the S3 prefix where CloudTrail writes JSON events. When CloudTrail creates the table for you, the console shows a success message.
A screenshot of the AWS CloudTrail "Event history" console showing a green success banner that an Amazon Athena table was successfully created. The page displays lookup filters and action buttons like "Download events," "Query in Lake," and "Create Athena table."
A typical CREATE EXTERNAL TABLE for CloudTrail logs looks like this (the helper fills in the exact table name and S3 location for your account):
Notes:
  • The LOCATION value points to the S3 prefix containing CloudTrail JSON files.
  • The JsonSerDe allows Athena to deserialize the JSON event records.

4) Configure Athena query result location

Before running queries in the Athena Query Editor you must configure a query result location — an S3 bucket where Athena writes query output and metadata.
Do not use your CloudTrail logs bucket as the Athena query results bucket. Keep query results in a separate S3 bucket (standard storage class) to avoid accidental tampering and unnecessary access patterns.
In the Athena console:
  1. Open Query settings (Manage).
  2. Set an S3 bucket for query results (choose a separate standard S3 bucket).
  3. Save settings and open the Query Editor.
Athena pricing is based on the amount of data scanned per query. To reduce costs, restrict scanned data using partitioned tables, LIMIT, WHERE filters, or by compressing and converting your logs to columnar formats like Parquet.

5) Run SQL queries against the CloudTrail table

Open the Athena Query Editor and run SQL queries against your CloudTrail table. A simple example to inspect recent activity:
  • Click Run to execute the query.
  • Athena displays query status, data scanned, runtime, and the results.
  • The output shows when events occurred, the API action names, and the IAM identity ARN that performed the action.
A screenshot of the AWS Console showing the Amazon Athena Query Editor with a results table listing CloudTrail event names (e.g., ListManagedNotificationEvents, GetDatabase, GetTable) and corresponding ARNs. The browser tabs and AWS navigation/header are visible across the top.

Quick reference and best practices

Useful links: Summary:
  1. Create a CloudTrail trail that delivers logs to an S3 bucket.
  2. Verify the S3 delivery location and objects.
  3. Use CloudTrail’s helper to create an Athena external table or hand-craft the CREATE statement.
  4. Configure Athena query results in a separate S3 bucket.
  5. Run SQL queries in Athena to analyze CloudTrail events and monitor API activity.

Watch Video

Practice Lab