SkillSprout
Chapter 10 · Lesson 2
Intermediate
6 min read

CloudTrail

Overview

AWS CloudTrail records who did what, when and from where in your AWS account. Every API call, whether made from the console, CLI, SDK or another AWS service, becomes an event containing the caller identity, time, source IP, request parameters and response.

CloudTrail is the security camera for your account. If an S3 bucket vanished, a security group was opened to the world or an IAM user was created at 3 a.m., CloudTrail tells you exactly who did it. That makes it essential for auditing, forensics and compliance.

CloudTrail is enabled by default with a free 90-day event history, but to keep records longer or to capture more event types you create a trail that delivers logs to S3.

Key Concepts

  • CloudTrail records API activity (control plane and optionally data plane) across consoles, CLI, SDKs and services.
  • Event history is on by default: the last 90 days of management events, per Region, free and searchable.
  • Management events (create/modify/delete resources, IAM, sign-ins) are logged by default; data events (S3 object-level, Lambda invoke, DynamoDB item-level) must be enabled and cost extra.
  • Insights events detect unusual API activity, such as spikes in write calls or error rates.
  • A trail delivers events to S3 (and optionally CloudWatch Logs and EventBridge); it can be multi-Region and organisation-wide.
  • Log file integrity validation produces signed hourly digest files (SHA-256 with RSA) to prove logs were not altered.
  • CloudTrail Lake stores events in a managed event data store and allows SQL queries, with retention up to 7 years (or longer with extendable options).
  • Log files are encrypted with SSE-S3 by default; use SSE-KMS for stronger control.

Detailed Notes

What CloudTrail captures

Event typeExamplesDefault?
Management events (control plane)RunInstances, CreateBucket, AttachRolePolicy, ConsoleLoginYes (read and write)
Data events (data plane)S3 GetObject/PutObject, Lambda Invoke, DynamoDB GetItemNo - enable per trail
Insights eventsUnusual API call rate or error rateNo
Network activity eventsVPC endpoint API callsNo

Each event is a JSON record with eventTime, eventSource, eventName, userIdentity, sourceIPAddress, requestParameters and responseElements.

json
{
  "eventTime": "2025-03-14T02:11:09Z",
  "eventSource": "ec2.amazonaws.com",
  "eventName": "AuthorizeSecurityGroupIngress",
  "userIdentity": { "type": "IAMUser", "userName": "dev-bob" },
  "sourceIPAddress": "203.0.113.25",
  "requestParameters": { "groupId": "sg-0abc", "ipPermissions": "0.0.0.0/0 port 22" }
}

Event history vs. trails

  • Event history: automatic, 90 days, management events only, searchable in the console, cannot be exported as a continuous stream.
  • Trail: a configuration that delivers events to an S3 bucket for as long as you wish. Delivery typically happens within about 5 minutes of the API call, and log files are written roughly every 5 minutes.

A trail can be single-Region or multi-Region (recommended; it also records global service events such as IAM and STS). An organisation trail created in the management account logs all member accounts and cannot be modified by them.

Integrations

  • S3: long-term storage; apply lifecycle policies and Object Lock for immutability.
  • CloudWatch Logs: stream events to enable metric filters and alarms (e.g. alert on root-account use or console logins without MFA).
  • EventBridge: react to API calls in near real time (e.g. auto-remediate when a security group is opened).
  • Athena: query log files in S3 with SQL.
  • CloudTrail Lake: managed store with SQL queries, no Athena set-up, and cross-account/Region aggregation.

Integrity and security

Enable log file integrity validation so CloudTrail writes digest files every hour signed by AWS. You can verify with aws cloudtrail validate-logs. Protect the destination bucket: deny deletes, enable MFA Delete/Object Lock, and ideally centralise logs in a dedicated log archive account.

bash
aws cloudtrail create-trail --name org-trail --s3-bucket-name central-audit-logs \
  --is-multi-region-trail --is-organization-trail --enable-log-file-validation
aws cloudtrail start-logging --name org-trail
aws cloudtrail put-event-selectors --trail-name org-trail \
  --advanced-event-selectors '[{"Name":"S3 data","FieldSelectors":[{"Field":"eventCategory","Equals":["Data"]},{"Field":"resources.type","Equals":["AWS::S3::Object"]}]}]'

CloudTrail vs. CloudWatch vs. Config

QuestionService
Who deleted the bucket?CloudTrail
Is CPU high right now?CloudWatch
What did this security group look like last Tuesday, and is it compliant?Config

Limits to remember

  • Event history: 90 days, per Region.
  • One management-event copy per Region is free in the first trail; additional copies and data events are charged.
  • CloudTrail only records API activity; it does not record OS-level activity inside instances.

Real World Example

Scenario — a healthcare company investigates an S3 data exposure.

A security scanner flags that a bucket holding lab reports became public. The security team opens the CloudTrail logs from the organisation trail stored in the dedicated log-archive account and queries them with Athena. The query shows that a developer's access key called PutBucketPolicy from an unfamiliar IP address at 02:11 UTC. Data events show which objects were later downloaded.

The company then hardens the setup:

  • An organisation trail, multi-Region with log file integrity validation, delivers to an S3 bucket with Object Lock and KMS encryption.
  • An EventBridge rule on PutBucketPolicy/PutBucketAcl triggers a Lambda that checks for public access and reverts the change, then alerts the security channel through SNS.
  • CloudWatch Logs metric filters alert on root sign-in and ConsoleLogin without MFA.

Auditors later accept the immutable logs and digest files as evidence for HIPAA controls.

Architecture Diagram

CloudTrail audit pipeline
Architecture
AWS Cloud
Users and rolesconsole, CLI, SDK
AWS servicesAPI calls
CloudTrailmulti-Region trail
S3 log bucketKMS, Object Lock
CloudWatch Logsmetric filters
EventBridgeauto-remediate

API calls become events, delivered to S3 for retention and to CloudWatch/EventBridge for real-time reaction.

  1. 1Any principal or AWS service makes an API call to AWS.
  2. 2CloudTrail records the call as an event (management by default, data and Insights if enabled).
  3. 3The trail delivers log files and hourly digest files to a protected S3 bucket.
  4. 4Optionally, events go to CloudWatch Logs for alarms and to EventBridge for automated remediation.

Best Practices

  • ✓Create an organisation-wide, multi-Region trail from the management account so no account or Region is missed.
  • ✓Enable log file integrity validation and store logs in a dedicated log archive account.
  • ✓Protect the S3 bucket: SSE-KMS, bucket policy denying deletion, Object Lock, lifecycle rules to Glacier.
  • ✓Enable data events selectively (critical buckets, sensitive tables) to control cost.
  • ✓Send events to CloudWatch Logs/EventBridge and alarm on root usage, IAM changes, and failed console logins.
  • ✓Turn on CloudTrail Insights for anomaly detection of unusual API volumes.
  • ✓Use Athena or CloudTrail Lake for investigations instead of grepping raw files.
  • ✓Review and rotate access keys that appear in suspicious events; correlate with GuardDuty findings.

Common Interview Questions

Exam Tips

SAA-C03 focus

  • "Who made this API call / deleted this resource / when" -> CloudTrail.
  • Default event history = 90 days, management events only. Longer retention -> create a trail to S3.
  • S3 object-level (GetObject, PutObject) and Lambda invoke logging -> data events (must be enabled).
  • Detect tampering with logs -> log file integrity validation (digest files).
  • Log all accounts in an AWS Organization -> organisation trail from the management account.
  • Query logs with SQL -> Athena on S3 logs or CloudTrail Lake.
  • Real-time reaction to an API call -> EventBridge rule (or CloudWatch Logs metric filter).
  • CloudTrail logs API activity only, not network traffic (VPC Flow Logs) or OS logs (CloudWatch agent).

Finished this lesson?

0 of 64 lessons done · 0% of the course