Sub topic 5
Creating a trail and choosing event selectors
This chapter assumes you know basic IAM concepts, S3 buckets, how to read JSON, and how to run the AWS CLI. You should also know the difference between management events and data events from the previous chapters.
Why you need a trail
Event history keeps 90 days of management events in each Region. You can't change what it records, and you can't send it anywhere. A trail is a configuration that fixes both limits. It delivers events to an S3 bucket, keeps them as long as you choose, and lets you decide which event types to record.
The workflow has three parts: create the trail, start logging, and set the event selectors that define what the trail records.
Create the trail
Before you run the command, make sure the S3 bucket exists. Its bucket policy must allow the CloudTrail service to write to it. If the policy is missing or wrong, create-trail fails with an error about the bucket policy.
aws cloudtrail create-trail \
--name org-audit-trail \
--s3-bucket-name my-cloudtrail-logs-bucket \
--is-multi-region-trail \
--enable-log-file-validation
aws cloudtrail start-logging --name org-audit-trailThree details matter here:
--is-multi-region-trailmakes one trail record events from all Regions. Without it, the trail covers only the Region where you created it.--enable-log-file-validationtells CloudTrail to write digest files. You need them to check log integrity later.create-traildoes not start recording. Until you runstart-logging, the trail exists but records nothing.
What a new trail records by default
A trail created from the CLI records management events by default. It does not record data events. Data events are high-volume and they cost extra, so you have to ask for them.
Choose event selectors
Event selectors are the filters that decide which events the trail keeps. Use advanced event selectors. They are more expressive than the older basic selectors, and a trail can use only one of the two types at a time.
Each advanced selector has a name and a list of field selectors. A field selector tests one field, such as eventCategory, resources.type, or resources.ARN. An event must match every field selector inside a selector to be recorded.
The example below records management events, plus S3 object-level data events for one bucket.
aws cloudtrail put-event-selectors \
--trail-name org-audit-trail \
--advanced-event-selectors '[
{
"Name": "Management events",
"FieldSelectors": [
{ "Field": "eventCategory", "Equals": ["Management"] }
]
},
{
"Name": "S3 object-level events for one bucket",
"FieldSelectors": [
{ "Field": "eventCategory", "Equals": ["Data"] },
{ "Field": "resources.type", "Equals": ["AWS::S3::Object"] },
{ "Field": "resources.ARN", "StartsWith": ["arn:aws:s3:::my-sensitive-bucket/"] }
]
}
]'Read the second selector as one condition: the event is a data event, it targets an S3 object, and the object is in my-sensitive-bucket. The trailing slash in the ARN prefix limits the match to objects inside that bucket.
put-event-selectors replaces the whole selector set. If you run it with only the data selector, the management selector disappears and the trail stops recording management events. Always send the complete list.
Choosing what to log
Pick selectors by the questions you need to answer later.
| Goal | Selector choice |
|---|---|
| Track configuration and permission changes | Management events (read and write) |
| Reduce noise and cost | Management write-only, with `readOnly` equal to `false` |
| Know who read or changed sensitive objects | Data events for specific buckets only |
| Detect unusual API volume | Insights events, enabled separately on the trail |
Scope data events as narrowly as you can. Logging every S3 object in every bucket produces large volumes and a large bill. Start with the buckets that hold sensitive data.
Verify the configuration
Check that the trail is logging and that the selectors are what you intended.
aws cloudtrail get-trail-status --name org-audit-trail
aws cloudtrail get-event-selectors --trail-name org-audit-trailIn the status output, IsLogging should be true. Delivery to S3 usually begins within about five minutes of the first events, and log files then arrive roughly every five minutes.
Common errors
- Insufficient bucket policy: the trail cannot write to the bucket. Fix the bucket policy so CloudTrail is allowed to put objects.
- Trail exists but no logs: you did not run
start-logging, or logging was stopped. - Missing management events: a later
put-event-selectorscall replaced the set without them. - Mixing selector types: basic and advanced selectors cannot be combined on one trail.
Exercise
You run put-event-selectors on a working trail with one advanced selector that matches only S3 object-level data events for a single bucket. An hour later, you find no new IAM changes in the logs. Why, and how do you fix it?
Show answer
put-event-selectors replaces the entire selector set. Your call removed the management events selector, so the trail no longer records IAM changes. Run the command again with the complete list: the management events selector plus the S3 data events selector.