Skip to content

Cloudtrail


AWS CloudTrail is a service that enables governance, compliance, operational auditing, and risk auditing of your AWS account.

AWS CloudTrail Overview
+------------------------------------------------------------------+
| |
| +------------------------+ |
| | AWS CloudTrail | |
| +------------------------+ |
| | |
| +---------------------+---------------------+ |
| | | | | |
| v v v v |
| +----------+ +----------+ +----------+ +----------+ |
| | Management| | Data | | Insights | | Lake | |
| | Events | | Events | | | | | |
| | | | | | | | | |
| | - Control | | - S3 | | - Anomaly| | - Query | |
| | Plane | | - Lambda | | - Detect | | - Analyze| |
| | - API | | - DynamoDB| | - Alert | | - Store | |
| +----------+ +----------+ +----------+ +----------+ |
| |
+------------------------------------------------------------------+
FeatureDescription
Management EventsControl plane operations
Data EventsResource-level operations
InsightsAnomaly detection
CloudTrail LakeQuery and analyze events

CloudTrail Trail Types
+------------------------------------------------------------------+
| |
| Single-Region Trail |
| +------------------------------------------------------------+ |
| | | |
| | - Logs events from one region | |
| | - Lower cost | |
| | - Suitable for single-region workloads | |
| | | |
| +------------------------------------------------------------+ |
| |
| Multi-Region Trail |
| +------------------------------------------------------------+ |
| | | |
| | - Logs events from all regions | |
| | - Recommended for security auditing | |
| | - Single S3 bucket for all regions | |
| | | |
| +------------------------------------------------------------+ |
| |
| Organization Trail |
| +------------------------------------------------------------+ |
| | | |
| | - Logs events from all accounts in organization | |
| | - Centralized logging | |
| | - Created in management account | |
| | | |
| +------------------------------------------------------------+ |
| |
+------------------------------------------------------------------+
CloudTrail Trail Configuration
+------------------------------------------------------------------+
| |
| Storage Options |
| +------------------------------------------------------------+ |
| | | |
| | S3 Bucket: | |
| | +------------------------------------------------------+ | |
| | | - Primary storage for log files | | |
| | | - Object lock for immutability | | |
| | | - Lifecycle policies for retention | | |
| | +------------------------------------------------------+ | |
| | | |
| | CloudWatch Logs: | |
| | +------------------------------------------------------+ | |
| | | - Real-time log analysis | | |
| | | - Metric filters and alarms | | |
| | +------------------------------------------------------+ | |
| | | |
| | CloudTrail Lake: | |
| | +------------------------------------------------------+ | |
| | | - SQL query capability | | |
| | | - 7-year retention | | |
| | +------------------------------------------------------+ | |
| | | |
| +------------------------------------------------------------+ |
| |
| Log File Validation |
| +------------------------------------------------------------+ |
| | | |
| | - Digest files for integrity verification | |
| | - Detect log modification or deletion | |
| | - Required for compliance (PCI, HIPAA) | |
| | | |
| +------------------------------------------------------------+ |
| |
+------------------------------------------------------------------+

CloudTrail Management Events
+------------------------------------------------------------------+
| |
| Control Plane Operations |
| +------------------------------------------------------------+ |
| | | |
| | IAM Operations: | |
| | +------------------------------------------------------+ | |
| | | - CreateUser, DeleteUser | | |
| | | - AttachRolePolicy, DetachRolePolicy | | |
| | | - CreateAccessKey, DeleteAccessKey | | |
| | +------------------------------------------------------+ | |
| | | |
| | EC2 Operations: | |
| | +------------------------------------------------------+ | |
| | | - RunInstances, TerminateInstances | | |
| | | - CreateSecurityGroup, AuthorizeSecurityGroupIngress | | |
| | | - CreateVolume, DeleteVolume | | |
| | +------------------------------------------------------+ | |
| | | |
| | S3 Operations: | |
| | +------------------------------------------------------+ | |
| | | - CreateBucket, DeleteBucket | | |
| | | - PutBucketPolicy, DeleteBucketPolicy | | |
| | +------------------------------------------------------+ | |
| | | |
| +------------------------------------------------------------+ |
| |
| Read vs Write Events |
| +------------------------------------------------------------+ |
| | | |
| | Read-only: | |
| | +------------------------------------------------------+ | |
| | | - Describe*, Get*, List* | | |
| | | - No resource modification | | |
| | +------------------------------------------------------+ | |
| | | |
| | Write: | |
| | +------------------------------------------------------+ | |
| | | - Create*, Delete*, Update*, Put* | | |
| | | - Resource modification | | |
| | +------------------------------------------------------+ | |
| | | |
| +------------------------------------------------------------+ |
| |
+------------------------------------------------------------------+
CloudTrail Data Events
+------------------------------------------------------------------+
| |
| Supported Resources |
| +------------------------------------------------------------+ |
| | | |
| | Amazon S3: | |
| | +------------------------------------------------------+ | |
| | | - GetObject, PutObject, DeleteObject | | |
| | | - Object-level operations | | |
| | | - Can log all buckets or specific buckets | | |
| | +------------------------------------------------------+ | |
| | | |
| | AWS Lambda: | |
| | +------------------------------------------------------+ | |
| | | - Invoke function | | |
| | | - Can log all functions or specific functions | | |
| | +------------------------------------------------------+ | |
| | | |
| | Amazon DynamoDB: | |
| | +------------------------------------------------------+ | |
| | | - GetItem, PutItem, DeleteItem | | |
| | | - Table-level operations | | |
| | +------------------------------------------------------+ | |
| | | |
| +------------------------------------------------------------+ |
| |
| Note: Data events are not logged by default |
| +------------------------------------------------------------+ |
| | - Must be explicitly enabled | |
| | - Additional cost applies | |
| +------------------------------------------------------------+ |
| |
+------------------------------------------------------------------+

CloudTrail Insights
+------------------------------------------------------------------+
| |
| Anomaly Detection |
| +------------------------------------------------------------+ |
| | | |
| | What it detects: | |
| | +------------------------------------------------------+ | |
| | | - Unusual API call volume | | |
| | | - Unusual API error rates | | |
| | | - Suspicious activity patterns | | |
| | +------------------------------------------------------+ | |
| | | |
| | How it works: | |
| | +------------------------------------------------------+ | |
| | | 1. Establishes baseline of normal activity | | |
| | | 2. Continuously monitors API calls | | |
| | | 3. Detects deviations from baseline | | |
| | | 4. Creates insight events | | |
| | +------------------------------------------------------+ | |
| | | |
| +------------------------------------------------------------+ |
| |
| Insight Types |
| +------------------------------------------------------------+ |
| | | |
| | ApiCallRateInsight: | |
| | +------------------------------------------------------+ | |
| | | - Unusual spike in API call volume | | |
| | | - Example: 100x normal TerminateInstances calls | | |
| | +------------------------------------------------------+ | |
| | | |
| | ApiErrorRateInsight: | |
| | +------------------------------------------------------+ | |
| | | - Unusual spike in API errors | | |
| | | - Example: Auth failures on AssumeRole | | |
| | +------------------------------------------------------+ | |
| | | |
| +------------------------------------------------------------+ |
| |
+------------------------------------------------------------------+

{
"eventVersion": "1.08",
"userIdentity": {
"type": "IAMUser",
"principalId": "AIDACKCEVSQ6C2EXAMPLE",
"arn": "arn:aws:iam::123456789012:user/alice",
"accountId": "123456789012",
"accessKeyId": "AKIAIOSFODNN7EXAMPLE",
"userName": "alice"
},
"eventTime": "2024-01-15T12:00:00Z",
"eventSource": "ec2.amazonaws.com",
"eventName": "StartInstances",
"awsRegion": "us-east-1",
"sourceIPAddress": "192.0.2.1",
"userAgent": "aws-cli/2.0.0",
"requestParameters": {
"instancesSet": {
"items": [
{
"instanceId": "i-1234567890abcdef0"
}
]
}
},
"responseElements": {
"instancesSet": {
"items": [
{
"instanceId": "i-1234567890abcdef0",
"currentState": {
"code": 0,
"name": "pending"
},
"previousState": {
"code": 80,
"name": "stopped"
}
}
]
}
},
"resources": [
{
"ARN": "arn:aws:ec2:us-east-1:123456789012:instance/i-1234567890abcdef0",
"type": "AWS::EC2::Instance"
}
],
"eventType": "AwsApiCall",
"recipientAccountId": "123456789012"
}

Terminal window
# Create trail
aws cloudtrail create-trail \
--name my-trail \
--s3-bucket-name my-cloudtrail-logs \
--include-global-service-events \
--is-multi-region-trail
# Start logging
aws cloudtrail start-logging \
--name my-trail
# Stop logging
aws cloudtrail stop-logging \
--name my-trail
# Get trail status
aws cloudtrail get-trail-status \
--name my-trail
# List trails
aws cloudtrail describe-trails
# Get trail
aws cloudtrail get-trail \
--name my-trail
# Put event selectors (enable data events)
aws cloudtrail put-event-selectors \
--trail-name my-trail \
--event-selectors '[
{
"ReadWriteType": "All",
"IncludeManagementEvents": true,
"DataResources": [
{
"Type": "AWS::S3::Object",
"Values": ["arn:aws:s3:::my-bucket/"]
}
]
}
]'
# Look up events
aws cloudtrail lookup-events \
--lookup-attributes AttributeKey=Username,AttributeValue=alice \
--start-time 2024-01-01T00:00:00Z \
--end-time 2024-01-31T23:59:59Z
# Get event selectors
aws cloudtrail get-event-selectors \
--trail-name my-trail
# Enable insights
aws cloudtrail put-insight-selectors \
--trail-name my-trail \
--insight-selectors '[{"InsightType":"ApiCallRateInsight"}]'
# Delete trail
aws cloudtrail delete-trail \
--name my-trail

CloudTrail Best Practices
+------------------------------------------------------------------+
| |
| 1. Enable multi-region trails |
| +------------------------------------------------------------+ |
| | - Capture all regional activity | |
| | - Single source of truth | |
| +------------------------------------------------------------+ |
| |
| 2. Enable log file validation |
| +------------------------------------------------------------+ |
| | - Detect tampering | |
| | - Required for compliance | |
| +------------------------------------------------------------+ |
| |
| 3. Use S3 object lock |
| +------------------------------------------------------------+ |
| | - Write-once, read-many (WORM) | |
| | - Prevent deletion | |
| +------------------------------------------------------------+ |
| |
| 4. Enable CloudTrail Insights |
| +------------------------------------------------------------+ |
| | - Detect anomalous activity | |
| | - Proactive security monitoring | |
| +------------------------------------------------------------+ |
| |
| 5. Integrate with CloudWatch Logs |
| +------------------------------------------------------------+ |
| | - Real-time analysis | |
| | - Metric filters and alarms | |
| +------------------------------------------------------------+ |
| |
| 6. Use organization trails |
| +------------------------------------------------------------+ |
| | - Centralize logging for all accounts | |
| | - Simplify compliance | |
| +------------------------------------------------------------+ |
| |
+------------------------------------------------------------------+

Exam Tip

Key Exam Points
+------------------------------------------------------------------+
| |
| 1. CloudTrail logs API calls (management and data events) |
| |
| 2. Management events are logged by default |
| |
| 3. Data events must be explicitly enabled |
| |
| 4. Multi-region trails capture all regional activity |
| |
| 5. Organization trails capture all account activity |
| |
| 6. Log file validation ensures integrity |
| |
| 7. Insights detect anomalous API activity |
| |
| 8. CloudTrail Lake enables SQL queries on events |
| |
| 9. Events are delivered within 15 minutes |
| |
| 10. S3 with object lock prevents log tampering |
| |
+------------------------------------------------------------------+

Chapter 37 Summary
+------------------------------------------------------------------+
| |
| CloudTrail Core Concepts |
| +------------------------------------------------------------+ |
| | - Trails: Configuration for logging | |
| | - Events: API call records | |
| | - Insights: Anomaly detection | |
| +------------------------------------------------------------+ |
| |
| Event Types |
| +------------------------------------------------------------+ |
| | - Management events: Control plane operations | |
| | - Data events: Resource-level operations | |
| | - Insights events: Anomalous activity | |
| +------------------------------------------------------------+ |
| |
| Best Practices |
| +------------------------------------------------------------+ |
| | - Enable multi-region trails | |
| | - Enable log file validation | |
| | - Use S3 object lock | |
| | - Enable Insights | |
| +------------------------------------------------------------+ |
| |
+------------------------------------------------------------------+

Previous Chapter: Chapter 36: Amazon CloudWatch - Metrics & Alarms Next Chapter: Chapter 38: AWS X-Ray - Distributed Tracing