Skip to Content

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: API calls made through the SDK, the CLI or the console, by users, roles and AWS services, are recorded by CloudTrail, which keeps 90 days of event history; a trail sends them to CloudWatch Logs for search and metric filters, or to an S3 bucket to keep them for years and query them with Athena AWS CloudTrail: API calls made through the SDK, the CLI or the console, by users, roles and AWS services, are recorded by CloudTrail, which keeps 90 days of event history; a trail sends them to CloudWatch Logs for search and metric filters, or to an S3 bucket to keep them for years and query them with Athena

Every API call, recorded.

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 (EC2 CreateSubnet), setting up logging (CloudTrail CreateTrail);
    • 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 Invoke API).
  • Network activity events (newer): API calls made through VPC endpoints, to see activity inside the VPC.
  • CloudTrail Insights events: see below.

CloudTrail Insights

CloudTrail Insights: it learns a baseline from normal management events, then watches for unusual API call rates or error rates; the resulting Insights events appear in the CloudTrail console, are stored in S3 with the trail, and are sent to EventBridge to automate a response CloudTrail Insights: it learns a baseline from normal management events, then watches for unusual API call rates or error rates; the resulting Insights events appear in the CloudTrail console, are stored in S3 with the trail, and are sent to EventBridge to automate a response

Spot activity that doesn't look normal.

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

Reacting to an API call: a user calls DeleteTable on DynamoDB, CloudTrail records the call, an EventBridge rule matches the event, and SNS alerts the security team Reacting to an API call: a user calls DeleteTable on DynamoDB, CloudTrail records the call, an EventBridge rule matches the event, and SNS alerts the security team

Turn any API call into an alert.

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 (AssumeRole in 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 DeleteDBInstance called 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

On this page

Open notes

Spotted a mistake?

These notes are open source. Suggest a fix, or star the repo if they helped you.

Suggest an edit

For AI agents: llms.txt · llms-full.txt