AWS CloudTrail
AWS CloudTrail for auditing API calls, trails and event history, management, data, network activity and Insights events, retention beyond 90 days, and reacting to API calls with EventBridge.
What it is
AWS CloudTrail provides governance, compliance and audit for an AWS account.
- CloudTrail is enabled by default: the event history keeps 90 days of management events.
- It records a history of events and API calls made within the account, by:
- the console;
- the SDK;
- the CLI;
- AWS services (acting on the account's behalf).
- Create a trail to put logs from CloudTrail into CloudWatch Logs or S3.
- A trail can apply to all Regions (the default) or a single Region. An organization trail logs every account in an AWS Organization.
- If a resource is deleted in AWS, investigate CloudTrail first!
CloudTrail events
- Management events:
- operations performed on resources in the account;
- examples: configuring security (IAM
AttachRolePolicy), configuring rules for routing data (EC2CreateSubnet), setting up logging (CloudTrailCreateTrail); - by default, trails log management events;
- read events (that don't modify resources) can be separated from write events (that may modify resources).
- Data events:
- not logged by default (because they're high-volume operations);
- S3 object-level activity (
GetObject,DeleteObject,PutObject), with read and write separated; - Lambda function execution activity (the
InvokeAPI).
- Network activity events (newer): API calls made through VPC endpoints, to see activity inside the VPC.
- CloudTrail Insights events: see below.
CloudTrail Insights
Enable CloudTrail Insights to detect unusual activity in the account:
- inaccurate resource provisioning;
- hitting service limits;
- bursts of AWS IAM actions;
- gaps in periodic maintenance activity.
CloudTrail Insights analyzes normal management events to create a baseline, then continuously analyzes write events (call rates, and error rates) to detect unusual patterns (it can now also watch data events):
- anomalies appear in the CloudTrail console;
- the event is sent to S3;
- an EventBridge event is generated, for automation.
Events retention
- Events are stored for 90 days in CloudTrail.
- To keep events beyond this period, log them to S3 with a trail and use Athena to query them.
CloudTrail Lake is another option: a managed store that keeps events (up to years) and query them with SQL.
EventBridge: intercept API calls
Every API call that CloudTrail logs becomes an event in EventBridge, so a rule can react to it:
- DynamoDB
DeleteTable→ alert through SNS; - a user assumes a role (
AssumeRolein IAM) → SNS; - someone edits security group inbound rules (EC2
AuthorizeSecurityGroupIngress) → SNS.
Real-world examples
- A production database disappears at 3 AM. CloudTrail event history shows
DeleteDBInstancecalled by a CI/CD role using a misconfigured script. - A security team gets an SNS alert within a minute whenever anyone opens port 22 to
0.0.0.0/0, through CloudTrail and EventBridge.
Exam tips
- "Who deleted / modified the resource?", "audit API calls" → CloudTrail.
- "Keep CloudTrail logs longer than 90 days and analyse them" → trail to S3 + Athena.
- "Log S3 object-level operations (GetObject, PutObject)" → enable data events.
- "Detect unusual API activity", "burst of IAM actions" → CloudTrail Insights.
- "Alert when a specific API call happens" → CloudTrail + EventBridge rule + SNS.
- "Audit all accounts in an organization" → organization trail.
Common traps
- "S3 GetObject calls are in CloudTrail by default." Data events must be enabled on a trail.
- "CloudTrail keeps events forever." Event history is 90 days; use a trail to S3 for longer.
Hands-on lab: find who did something
Goal: look up an API call in CloudTrail event history.
Costs money
Event history is free. Creating and deleting the security group below is free too. Any Region works.
Make a change
EC2 → Security groups → Create security group named saa-trail-test in the default VPC.
Find it
Wait about 5 minutes, then open CloudTrail → Event history. Filter on Event name = CreateSecurityGroup. Open the event and read its JSON: userIdentity, sourceIPAddress, eventTime.
Expected result: an event that shows your user (or role), your IP address and the time.
Clean up: delete the saa-trail-test security group.
AWS documentation
CloudWatch Insights
CloudWatch Container Insights, Lambda Insights, Contributor Insights and Application Insights, and which one answers which monitoring question.
AWS Config
AWS Config for recording resource configurations and compliance, managed and custom Config rules, pricing, resource timelines, automatic remediation with SSM Automation, and notifications.