Sub topic 3
Querying historical events with CloudTrail Lake and Athena
Assume you know basic IAM, S3, JSON, and AWS CLI commands. This chapter focuses on historical searches, alerts, protection, and investigation.
Querying historical events with CloudTrail Lake and Athena
Event history provides recent management events, but CloudTrail Lake stores events in an event data store that you can query with SQL. Replace the example ID with your event data store ID.
SELECT eventTime,
eventName,
userIdentity.arn,
sourceIPAddress
FROM eds-0123456789abcdef0
WHERE eventTime > '2026-11-01 00:00:00'
AND eventName IN ('StopLogging', 'DeleteTrail', 'PutBucketPolicy')
ORDER BY eventTime DESC
LIMIT 50Athena queries CloudTrail log files delivered to S3. It requires an external table whose schema matches the JSON log structure. Athena is useful when you already centralize logs in S3 and want joins, partitions, or cross-account analysis. CloudTrail Lake is simpler for CloudTrail-only investigations because it manages event storage and querying.
Alerting on sensitive API calls
Use EventBridge for near-real-time alerts. Match events such as StopLogging, DeleteTrail, PutBucketPolicy, or changes to IAM policies, then send the matched event to SNS, a ticketing system, or an automated response.
{
"source": ["aws.cloudtrail"],
"detail-type": ["AWS API Call via CloudTrail"],
"detail": {
"eventSource": ["cloudtrail.amazonaws.com"],
"eventName": ["StopLogging", "DeleteTrail"]
}
}A trail can also deliver events to CloudWatch Logs. Metric filters and alarms there are useful when your operations team already monitors CloudWatch.
Protecting the trail and its logs
Restrict trail administration with IAM and, where appropriate, an organization SCP. Protect the S3 bucket with Block Public Access, a narrow bucket policy, and separate security-account ownership. Use SSE-KMS when your key policy permits CloudTrail to write. Give investigators read access without granting deletion.
Log file validation detects changed, deleted, or missing delivered files. It does not prevent a principal with enough access from changing the storage location, so protect both the trail configuration and the destination.
> callout note > Store audit logs in an account and region that ordinary application administrators cannot modify.
Putting it together: investigating a suspicious change
- Preserve the alert and its event ID.
- Read the event’s actor, action, target, time, source address, user agent, and error fields.
- Search nearby events for role assumption, policy changes, trail changes, and related resource updates.
- Check whether the actor and source match an approved change.
- Validate the affected log range.
- Contain the identity or resource, then preserve evidence before making changes.
Cost and coverage considerations
Management events usually provide broad control-plane coverage. Data events, such as S3 object access, must be selected and can produce many records. Insights events, CloudTrail Lake ingestion and retention, S3 storage, KMS requests, and Athena data scans can add charges. Select high-value data resources, partition S3 data for Athena, and review current AWS pricing.
Exercise
Which service should you choose for a SQL search over CloudTrail events already stored in S3?
Show answer
Athena, provided an external table and appropriate schema exist.