What is AWS CloudTrail, and why is it important for security and incident investigation?
Answer
AWS CloudTrail records API activity in your AWS account: who made a call, from which IP, when, against which resource, and whether it succeeded. Every console click, CLI command, and SDK call ends up as an event, which lets you answer "who deleted this Security Group rule?" or "which role read objects from this bucket at 2 a.m.?". That makes it the foundation of auditing, compliance, and incident response on AWS.
What CloudTrail records
Management events (control plane): creating an EC2 instance, changing an IAM policy, modifying a Security Group, console sign-ins. Recorded by default.
Data events (data plane): S3 object-level calls like GetObject and PutObject, Lambda Invoke, DynamoDB item operations. High volume, so they are off by default and charged when enabled.
Insights events: CloudTrail detects unusual spikes in API call rates or error rates compared with a baseline.
Without any setup, the Event history in the console shows the last 90 days of management events for one Region. For anything longer or more complete, you create a trail.
Trails and where logs go
A trail delivers events as JSON log files to an S3 bucket, and optionally to CloudWatch Logs for real-time alerting. Good practice:
One organization trail, created from AWS Organizations, covering all accounts and all Regions.
Logs written to a dedicated, locked-down log-archive account, so an attacker in a workload account cannot delete them.
Log file integrity validation turned on: CloudTrail writes signed digest files (SHA-256 hashes) so you can prove logs were not altered.
S3 Object Lock or tight bucket policies and KMS encryption on the log bucket.
Anatomy of an event
Key fields you will search in an investigation:
Field
Meaning
eventTime
When the call happened (UTC)
eventName
The API action, e.g. DeleteBucketPolicy
userIdentity
Who: IAM user, assumed role session, root, or AWS service
sourceIPAddress
Caller's IP or the AWS service name
requestParameters
What was asked for
errorCode
Present if the call failed, e.g. AccessDenied
Using it for security
Alert in real time on high-risk events: root login, StopLogging on a trail, IAM policy changes, Security Groups opened to 0.0.0.0/0. Use EventBridge rules or CloudWatch metric filters on the trail.
Amazon GuardDuty analyses CloudTrail management events (among other sources) to detect threats like credential misuse.
For investigations, query logs in S3 with Athena.
Gotchas
Events are usually delivered within minutes, not instantly; use EventBridge for faster reactions.
CloudTrail records API calls, not what happens inside an instance. SSH sessions or application logs need other tools (CloudWatch Logs, Session Manager logging).
If an attacker calls StopLogging, that call itself is logged. Alarm on it.
From API call to security alert. Every API call becomes a CloudTrail event that is stored, analysed, and alerted on.
API call: Console, CLI, SDK, or an AWS service acting for you.
CloudTrail event: Who, what, when, where, and the result.
S3 log archive: Encrypted, integrity-validated, separate account.
EventBridge and GuardDuty: Real-time rules and threat detection.
Alert or Athena query: Notify on-call or investigate history.
Code examples
Create a multi-Region trail and look up recent events
aws cloudtrail create-trail --name org-audit \
--s3-bucket-name company-cloudtrail-logs \
--is-multi-region-trail --enable-log-file-validation
aws cloudtrail start-logging --name org-audit
# Who deleted a security group in the last few days?
aws cloudtrail lookup-events \
--lookup-attributes AttributeKey=EventName,AttributeValue=DeleteSecurityGroup \
--max-results 5 \
--query 'Events[].{time:EventTime,user:Username,name:EventName}'
lookup-events searches the 90-day management event history; the trail keeps a durable copy in S3 for longer retention and Athena queries.
EventBridge pattern that fires when someone stops CloudTrail logging
{
"source": ["aws.cloudtrail"],
"detail-type": ["AWS API Call via CloudTrail"],
"detail": {
"eventSource": ["cloudtrail.amazonaws.com"],
"eventName": ["StopLogging", "DeleteTrail", "UpdateTrail"]
}
}
Attach an SNS or Lambda target to this rule so tampering with audit logging alerts the security team immediately.
Key points
CloudTrail logs API calls: who, what, when, from where, and the result
Management events are on by default; data events (S3 objects, Lambda invokes) must be enabled and cost extra
Event history keeps 90 days of management events; trails store logs in S3 for long-term retention
Use an organization trail, a separate log archive account, and log file integrity validation
CloudTrail does not see activity inside an instance
Common follow-up questions
What is the difference between CloudTrail and CloudWatch?
How would you find out who made an S3 bucket public?
How do you protect CloudTrail logs from being deleted by an attacker?
What is the difference between management events and data events?
Interview tips
Contrast CloudTrail (who did what, API audit) with CloudWatch (metrics and logs about performance) early; interviewers often test that confusion
Mention organization trails and a separate log archive account; it shows real security thinking
Give a concrete investigation example using userIdentity and sourceIPAddress