CloudTrail in CloudWatch Logs Insights: who did what
If your trail delivers to CloudWatch Logs, each API call is a JSON event whose fields (eventName, eventSource, userIdentity.arn, sourceIPAddress, errorCode, requestParameters) are discovered automatically. Filter by eventName and the resource ID to see who made a change, from where and when.
The query: who stopped this instance?
filter eventSource = "ec2.amazonaws.com"
and eventName in ["StopInstances", "TerminateInstances", "RebootInstances"]
and requestParameters.instancesSet.items.0.instanceId = "i-0123456789abcdef0"
| fields eventTime, eventName, userIdentity.arn, userIdentity.type, sourceIPAddress, userAgent, errorCode
| sort eventTime descSelect the log group your trail writes to (for example aws-cloudtrail-logs-123456789012) and a range covering when the instance stopped.
Line by line
eventSourceidentifies the service;eventNamethe API action. Filtering on both is precise and cheap to read.requestParameters.instancesSet.items.0.instanceIdis the first instance ID in the request. CloudTrail records EC2's instance list as an array, which Logs Insights flattens with an index. A call that stopped several instances lists the others at.1,.2and so on; to catch those too, usefilter @message like "i-0123456789abcdef0"instead.userIdentity.arnis the caller. For an assumed role, it's the role session ARN, which often ends in the user's name or the automation's session name.sourceIPAddressanduserAgenttell you whether it came from the console, the CLI, an SDK or an AWS service acting for someone.errorCodeis empty for successful calls; a value such asClient.UnauthorizedOperationmeans someone tried and failed.
Variations
Who deleted something, across services:
filter eventName like /^Delete/ and not ispresent(errorCode)
| stats count(*) as deletes by userIdentity.arn, eventSource, eventName
| sort deletes descThrottled calls, to find the client hammering an API:
filter errorCode in ["ThrottlingException", "Throttling", "RequestLimitExceeded"]
| stats count(*) as throttled by eventSource, eventName, userIdentity.arn
| sort throttled descAccess denied, useful when an automation suddenly breaks:
filter errorCode like /AccessDenied|UnauthorizedOperation/
| fields eventTime, userIdentity.arn, eventName, errorMessage
| sort eventTime desc
| limit 50Console sign-ins:
filter eventName = "ConsoleLogin"
| fields eventTime, userIdentity.arn, sourceIPAddress, responseElements.ConsoleLogin, additionalEventData.MFAUsed
| sort eventTime descIf your trail doesn't go to CloudWatch Logs
Logs Insights only sees CloudTrail if a trail is configured to deliver to a log group. Without that, you still have options:
- CloudTrail Event history in the console, or
aws cloudtrail lookup-events, covers the last 90 days of management events in a region, with simple attribute filters. See find who stopped an EC2 instance. - CloudTrail Lake or Athena over the trail's S3 bucket for SQL queries over longer history.
- EventBridge rules matching the
AWS API Call via CloudTraildetail type, to react to specific calls as they happen rather than search afterwards.
Limits and cost
CloudTrail log groups grow quickly in busy accounts, especially with data events enabled, and queries are billed by GB scanned. Filter on eventSource and eventName early and keep the time range tight. Events reach CloudWatch Logs a few minutes after the call, so a change made seconds ago may not show yet. For standing alerts on sensitive calls, a metric filter with an alarm is cheaper than repeated queries.
Questions
How do I check who stopped an EC2 instance in CloudTrail?
Search for StopInstances events with that instance ID, in CloudTrail Event history or with Logs Insights if your trail delivers to CloudWatch Logs; userIdentity.arn shows who called it.
How do I find ThrottlingException errors in CloudTrail?
Filter on errorCode values such as ThrottlingException or RequestLimitExceeded and group by eventName and userIdentity.arn to find the caller.
What is 'AWS API Call via CloudTrail' in EventBridge?
The detail type for EventBridge events generated from CloudTrail-recorded API calls, used to trigger rules on specific actions.
'Who changed this?' is usually asked while something is broken. CloudConsole, a phone app for AWS, is built to show alarms, resources and recent activity on your phone.
About the app → · Download on the App Store