0Pricing
AWS Security Academy · Lesson

Investigating Incidents with SQL Queries

Filter millions of events down to the actions that matter.

Investigating Incidents with SQL Queries is a free AWS Security Academy lesson on CoddyKit — lesson 3 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.

Investigation as Querying

With CloudTrail and Flow Logs cataloged, an investigation becomes a series of SQL queries that filter billions of events down to the few that matter. The skill is knowing what to ask: who did what, from where, and when. This lesson covers the query patterns investigators rely on.

Finding Activity by a Principal

When a credential is suspected compromised, you query all actions by that principal. Filtering CloudTrail on userIdentity.arn or access key ID reveals every API call it made, in order. This timeline of the attacker's actions is often the single most valuable output of an investigation.

SELECT eventtime, eventname, sourceipaddress
FROM cloudtrail_logs
WHERE useridentity.accesskeyid = 'AKIAEXAMPLE'
ORDER BY eventtime

Tracing a Source IP

Filtering on sourceIPAddress shows everything done from a given address. If you have one suspicious IP from a GuardDuty finding, this surfaces every call it made across services, helping you scope the blast radius and spot which resources the attacker touched.

Hunting Denied Calls

Querying for events where errorCode is AccessDenied or UnauthorizedOperation reveals reconnaissance: an attacker testing what a stolen credential can do. A burst of denials from one principal followed by a successful call shows them finding a permission that works, a clear escalation signal.

Spotting Privilege Escalation

Watch for sensitive IAM events such as CreateUser, AttachUserPolicy, CreateAccessKey, or PutUserPolicy. An attacker often tries to establish persistence by creating new identities or keys. Querying for these event names quickly reveals attempts to deepen a foothold beyond the initial entry.

Correlating Across Logs

Powerful investigations join data sources: match a CloudTrail principal's actions against VPC Flow Logs from the same instance, or against ALB logs for the requests it served. Correlating control-plane and network activity reconstructs the full attack story rather than isolated fragments.

Time-Bounding Queries

Always constrain queries to a time window using the event timestamp. This focuses the investigation on the relevant period and, when combined with partitions, dramatically reduces data scanned and cost. Starting broad in time then narrowing is a natural rhythm as the picture sharpens.

Counting and Grouping

Aggregations expose patterns: GROUP BY source IP to find top talkers, count distinct event names per principal to gauge breadth of activity, or tally errors over time to find a spike. These summaries turn raw rows into the trends that point you toward the real incident.

Querying Flow Logs Too

The same SQL approach applies to Flow Logs in S3: find large outbound transfers, count rejected ports per source, or list external IPs an instance contacted. Pairing CloudTrail (what was done in the API) with Flow Logs (what moved on the network) gives both halves of the story.

Saving and Sharing Queries

Save proven investigation queries as named queries or views so your team can rerun them fast during the next incident. A library of vetted queries, like "all actions by an access key" or "denied calls in the last hour," turns ad-hoc effort into a repeatable response capability.

From Query to Action

Queries produce evidence; that evidence drives response: revoke the key, isolate the instance, remove the rogue IAM user. A disciplined query process, principal first, then IP, then escalation events, then correlation, gives you the confident, complete understanding needed to contain an incident properly.

Quick Check

Test your investigation querying.

Recap

Investigations are SQL queries over cataloged logs. Start with a principal or source IP, hunt denied calls for recon, watch IAM events like CreateAccessKey for escalation, and time-bound everything. Group and count to find patterns, correlate CloudTrail with Flow Logs, and save vetted named queries so evidence-gathering becomes a repeatable response capability.

Frequently asked questions

Is the “Investigating Incidents with SQL Queries” lesson free?

Yes — the full text of “Investigating Incidents with SQL Queries” 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 “Investigating Incidents with SQL Queries”?

Filter millions of events down to the actions that matter. 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 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Investigating Incidents with SQL Queries” 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