0Pricing
AWS Security Academy · Lesson

Building Tables Over CloudTrail Data

Define a schema that lets you query audit events directly.

Building Tables Over CloudTrail Data is a free AWS Security Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AWS Security Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why You Need a Table

Athena cannot query raw S3 files until you tell it how to read them. A table maps the CloudTrail JSON files in your S3 bucket to named columns with types. Once the table exists, the rich structure of CloudTrail events becomes addressable with ordinary SQL.

CloudTrail Log Structure

CloudTrail delivers events as JSON, each record describing one API call with nested fields like userIdentity, eventName, sourceIPAddress, and requestParameters. The table schema must mirror this nesting, including struct types for objects, so queries can reach fields like userIdentity.arn.

Using the AWS-Provided DDL

You do not have to hand-write the schema. AWS publishes a standard CREATE TABLE statement for CloudTrail, and the CloudTrail console and Athena can generate it for you. Using the provided DDL ensures every field, including deeply nested ones, is mapped correctly so your queries do not miss data.

The SerDe

A SerDe (Serializer/Deserializer) tells Athena how to parse each file. CloudTrail tables use a SerDe designed for the CloudTrail JSON layout, which understands the records array and nested structures. Choosing the correct SerDe is essential; the wrong one yields parse errors or null columns.

ROW FORMAT SERDE
  'com.amazon.emr.hive.serde.CloudTrailSerde'
STORED AS INPUTFORMAT
  'com.amazon.emr.cloudtrail.CloudTrailInputFormat'

Pointing at the S3 Location

The table's LOCATION clause points to the S3 prefix where CloudTrail stores logs, typically a path that includes the account ID and region. Getting this prefix right matters: too broad and you scan irrelevant data, too narrow and you miss events you meant to query.

Querying Nested Fields

With the table built, you query nested fields using dot notation, such as userIdentity.arn or requestParameters.bucketName. SQL functions help unpack arrays and JSON strings. This is where Athena shines for investigations: you can filter precisely on the event name, principal, and parameters of any API call.

Glue Crawlers as an Alternative

Instead of manual DDL, an AWS Glue crawler can scan the S3 data and infer a schema into the Data Catalog automatically. For well-known formats like CloudTrail the provided DDL is usually cleaner, but crawlers help for custom or evolving log formats where you would rather not write schema by hand.

Handling the JSON Quirks

CloudTrail JSON has fields that are themselves JSON strings, like requestParameters when values vary by service. You often parse these at query time with JSON functions. Knowing that some columns are strings to be parsed, not pre-structured columns, avoids confusion when a field does not behave like a normal column.

Organization Trail Tables

For an organization trail, logs from all accounts land under one bucket with account-specific prefixes. You can build a single table over the whole prefix to query every account at once, or per-account tables for isolation. A unified table is powerful for org-wide threat hunting in one query.

Verifying the Table

After creating the table, run a quick SELECT with a small LIMIT to confirm rows return and columns populate as expected. If columns are null, the SerDe, location, or schema is likely off. This sanity check saves you from building investigations on a silently broken table.

From Table to Investigation

Once the CloudTrail table is correct, you have a queryable audit trail spanning all recorded history. Every console login, API call, and error is now a row you can filter and aggregate. This table is the foundation for the incident queries you will write next.

Quick Check

Test your table-building knowledge.

Recap

To query CloudTrail in Athena you build a table that maps the JSON files to columns, using AWS's provided CREATE TABLE DDL, the correct SerDe, and the right S3 LOCATION. Query nested fields with dot notation, parse JSON-string columns at query time, and verify with a small SELECT. An organization trail can be one table for org-wide hunting.

Frequently asked questions

Is the “Building Tables Over CloudTrail Data” lesson free?

Yes — the full text of “Building Tables Over CloudTrail Data” is free to read here on the web, and the AWS Security Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AWS Security Academy course, upgrade to CoddyKit PRO.

What will I learn in “Building Tables Over CloudTrail Data”?

Define a schema that lets you query audit events directly. You practise AWS Security Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start AWS Security Academy?

No prior experience is required. AWS Security Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Building Tables Over CloudTrail Data” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this AWS Security Academy lesson?

Yes. Every AWS Security Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Querying S3 Logs with Athena
  2. Building Tables Over CloudTrail Data
  3. Investigating Incidents with SQL Queries
  4. Partitioning Logs for Speed and Cost
← Back to AWS Security Academy