Auditing and Debugging API Calls with AWS CloudTrail
This lesson explains how AWS CloudTrail records the API calls made in your account by users, roles and AWS services, showing who did what, when and from where.
Learning outcomes
After completing this lesson, you will be able to:
- Distinguish management events from data events and event history from trails.
- Identify the fields of a CloudTrail event that answer most investigation questions.
- Use CloudTrail to find the principal and action behind an AccessDenied error.
- Describe how CloudTrail events trigger automated responses.
Event history and trails
CloudTrail records two main kinds of activity. Management events are operations that create, change or delete resources and their configuration, such as CreateBucket, UpdateFunctionConfiguration or PutRolePolicy. Data events are operations on the data inside resources, such as reading an S3 object, invoking a Lambda function or writing a DynamoDB item. Data events occur in far greater numbers, so they are not recorded unless enabled.
Event history is available in every account without any setup. It shows the last 90 days of management events in each Region, can be searched in the console or with the LookupEvents API, and costs nothing. It is enough for questions such as who changed a security group yesterday. A trail is needed for anything more: it delivers events continuously as log files to an S3 bucket, where they are kept for as long as required, and can also send them to CloudWatch Logs for metric filters and alarms. A trail can record data events, cover all Regions and, as an organisation trail, every account. The first copy of management events is free; data events and further copies are charged. The following table summarises the differences.
The following list describes further characteristics of CloudTrail logging.
- Data events, such as S3 object reads and Lambda invocations, are not recorded by default; enable them on a trail.
- Trails can cover every Region and every account in an organisation.
- Log file integrity validation proves delivered logs have not been altered.
- Events typically appear within about five minutes; trails in S3 can be queried with SQL through Athena (CloudTrail Lake closed to new customers on 31 May 2026).
aws cloudtrail lookup-events \
--lookup-attributes AttributeKey=EventName,AttributeValue=DeleteBucket \
--max-results 5 --query "Events[].[EventTime,Username,CloudTrailEvent]"Management or data event?
Reading an event
Each CloudTrail event is a JSON record of one API call, and a handful of its fields answer most investigation questions.
userIdentity identifies the caller. Its type shows whether the call came from an IAM user, an assumed role, the root user or an AWS service, and for an assumed role the ARN includes the role name and the session name, which often identifies the function, task or person behind the call. eventSource and eventName identify the service and operation, such as s3.amazonaws.com and PutObject, and eventTime records when it happened. sourceIPAddress shows where the request came from, which may be a private address for calls through a VPC endpoint or a service name for calls that AWS makes on your behalf, and userAgent shows the tool, such as the console, the CLI or an SDK. errorCode and errorMessage appear only when the call failed, and the message often names the exact permission that was missing. The following table summarises these fields.
| Field | Tells you |
|---|---|
userIdentity | Who made the call, including an assumed role's session name |
eventSource, eventName | Which API was called |
sourceIPAddress, userAgent | Where it came from and which tool |
errorCode | Any failure, such as AccessDenied |
{
"eventName": "PutObject",
"eventSource": "s3.amazonaws.com",
"userIdentity": {"type": "AssumedRole", "arn": "arn:aws:sts::111122223333:assumed-role/OrdersApiRole/orders-api"},
"sourceIPAddress": "10.0.3.17",
"errorCode": "AccessDenied",
"errorMessage": "User is not authorized to perform: kms:GenerateDataKey on resource: arn:aws:kms:..."
}Example. The role has s3:PutObject, but the bucket uses SSE-KMS and the role lacks kms:GenerateDataKey on the key. (PutObject is a data event, so it appears only if data events are enabled.)
Read a CloudTrail event
Troubleshooting and automation
CloudTrail records support troubleshooting and automated responses. The following list describes these uses.
- An
AccessDeniedorUnauthorizedOperationevent shows exactly which principal called which action on which resource. - Trace who changed or deleted a resource.
- EventBridge can react to CloudTrail events in near real time.
The following diagram shows a CloudTrail event used for both automation and troubleshooting.

An AccessDenied in production
A deployed function intermittently fails with AccessDenied, but the developer cannot reproduce it. CloudTrail records every denied call with the exact action and identity:
aws cloudtrail lookup-events --lookup-attributes AttributeKey=Username,AttributeValue=orders-fn \
--max-results 20 --query "Events[].CloudTrailEvent" --output text | grep -i AccessDeniedThe event shows kms:GenerateDataKey denied: on the rare path where the function writes an encrypted object to S3, the execution role lacks permission to use the bucket's KMS key. CloudTrail revealed an action the developer had not realised the code needed.
How do you find out who deleted a DynamoDB table yesterday?
Show answer
Search CloudTrail event history for the DeleteTable event. It shows the identity, time, source IP address and request parameters of management events from the last 90 days.
Exam Tips
Remember for the exam
- To find out who deleted or changed a resource, use CloudTrail.
- To log S3 object-level access or Lambda invocations, enable data events on a trail.
- To keep API history for longer than 90 days, create a trail that delivers to S3.
- To see which role session made a call, read
userIdentityin the event. - To prove logs have not been tampered with, enable log file integrity validation.