Sub topic 5
Creating a trail and choosing event selectors
This chapter assumes you know the basics of IAM users, roles and policies, can read JSON, have created an S3 bucket, and can run AWS CLI commands. Earlier chapters covered the event record and the three event categories. Here you create a trail and decide which events it records.
Why Event history is not enough
Event history gives you the last 90 days of management events in each Region, with no setup. That works for a quick look. It has limits:
- Records expire after 90 days.
- It does not include data events, such as S3 object reads or Lambda invocations.
- You cannot query it with SQL or send it to another tool.
- It is searched one Region at a time.
A trail is a configuration that tells CloudTrail to deliver events to an S3 bucket as log files. Those files stay until you delete them or an S3 lifecycle rule does. A trail can also forward events to CloudWatch Logs and EventBridge. Create a trail in every account you care about.
What a trail is made of
A trail has a few settings that matter for auditing:
| Setting | What it controls |
|---|---|
| S3 bucket and prefix | Where log files are delivered |
| Multi-Region flag | Whether the trail records activity in all Regions or only its home Region |
| Event selectors | Which events the trail records |
| Log file validation | Whether CloudTrail writes signed digest files for integrity checks |
| KMS key (optional) | Whether log files are encrypted with your own key |
| CloudWatch Logs group (optional) | Where events are also streamed for metric filters |
Event selectors are the part that determines coverage and cost, so most of this chapter is about them.
Step 1: Prepare the bucket policy
CloudTrail writes to your bucket as the service principal cloudtrail.amazonaws.com. The console adds the needed bucket policy for you. With the CLI you must add it yourself, or create-trail fails with an InsufficientS3BucketPolicyException.
Save this as policy.json. Replace the bucket name, account ID and trail name with yours:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AWSCloudTrailAclCheck",
"Effect": "Allow",
"Principal": { "Service": "cloudtrail.amazonaws.com" },
"Action": "s3:GetBucketAcl",
"Resource": "arn:aws:s3:::my-cloudtrail-logs-bucket",
"Condition": {
"StringEquals": {
"aws:SourceArn": "arn:aws:cloudtrail:us-east-1:123456789012:trail/org-audit-trail"
}
}
},
{
"Sid": "AWSCloudTrailWrite",
"Effect": "Allow",
"Principal": { "Service": "cloudtrail.amazonaws.com" },
"Action": "s3:PutObject",
"Resource": "arn:aws:s3:::my-cloudtrail-logs-bucket/AWSLogs/123456789012/*",
"Condition": {
"StringEquals": {
"s3:x-amz-acl": "bucket-owner-full-control",
"aws:SourceArn": "arn:aws:cloudtrail:us-east-1:123456789012:trail/org-audit-trail"
}
}
}
]
}Apply it:
aws s3api put-bucket-policy \
--bucket my-cloudtrail-logs-bucket \
--policy file://policy.jsonThe aws:SourceArn condition limits the permission to this one trail. Without it, any trail in any account could write to your bucket if it knew the name. This is the confused-deputy protection.
Step 2: Create and start the trail
This is the command from the plan. It creates a multi-Region trail with log file validation:
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-trailConfirm that the trail is recording:
aws cloudtrail get-trail-status --name org-audit-trailLook for "IsLogging": true. If LatestDeliveryError appears, the cause is almost always the bucket policy or a KMS key policy. Delivery usually begins within about five minutes of the first events.
By default a new trail records all management events, both read and write. That default is a sensible baseline, and the next section changes it deliberately.
Choosing event selectors
An event selector is a rule that says which events a trail records. CloudTrail offers two kinds:
- Basic event selectors can filter management events by read/write and can pick S3, Lambda and DynamoDB data events by resource.
- Advanced event selectors filter on fields such as
eventCategory,eventName,resources.type,resources.ARN,readOnly,userIdentity.arnanderrorCode. They support more resource types and finer filtering.
Use advanced selectors for new trails. A trail uses one kind or the other, not both. Once you set advanced selectors, you manage the trail through them.
How a selector is evaluated
Each advanced selector has a name and a list of field selectors. Inside one selector, every field condition must match (logical AND). An event is recorded if it matches any one selector (logical OR). The only required field is eventCategory.
Example: management events plus one bucket's objects
This command is also from the plan. It records management events and S3 object-level 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/"] }
]
}
]'Two details matter here:
- The trailing slash in
arn:aws:s3:::my-sensitive-bucket/makes the match cover objects in that bucket. Leave it off and the prefix could also match other buckets whose names start with the same text, such asmy-sensitive-bucket-archive. put-event-selectorsreplaces the whole configuration. If you leave out the management selector, the trail stops recording management events. Send the complete list every time.
Check the result:
aws cloudtrail get-event-selectors --trail-name org-audit-trailNarrowing further
You can cut volume by adding conditions to a selector. Some useful ones:
readOnlywithEquals: ["false"]records only write calls. Reads such asList*andDescribe*make up most management event volume.eventSourcewithNotEquals: ["kms.amazonaws.com"]drops a noisy service.resources.typewithAWS::Lambda::Functionrecords Lambda invocations.AWS::DynamoDB::Tablerecords DynamoDB item-level calls.
Be careful with exclusions. A read-only filter hides reconnaissance, such as an attacker running ListUsers or GetObject. Excluding a service also hides abuse of that service. Decide what to drop based on what your investigations need, not only on cost.
Insights events
Insights events are a separate feature. They flag unusual API call volume or error rates compared with a baseline. You enable them on the trail with put-insight-selectors, using the insight types ApiCallRateInsight and ApiErrorRateInsight. The trail must already log write management events for ApiCallRateInsight, and management events for ApiErrorRateInsight. Insights are billed separately, so turn them on after the basic trail works.
Choosing what to log
| Need | Selector choice |
|---|---|
| Who changed configuration or permissions | Management events, read and write |
| Cut volume, keep a change record | Management events with `readOnly` = false |
| Who read or changed objects in a sensitive bucket | S3 data events, scoped to that bucket's ARN |
| Function invocation audit | `AWS::Lambda::Function` data events for chosen functions |
| Detect unusual API bursts | Insights selectors |
Common errors
- Delivery fails right after creation. The bucket policy is missing or the ARN in it is wrong. Fix the policy, then run
get-trail-statusagain. - Data events never appear. The trail has only a management selector, or the
resources.ARNvalue does not match. Runget-event-selectorsand compare. - Management events stopped after an edit. A
put-event-selectorscall omitted the management selector. - Duplicate events. Several trails record the same events. This is allowed and each extra copy of management events costs money. It is also a reason to prefer one organization-wide trail, covered in a later chapter.
- Events from other Regions are missing. The trail is single-Region. Check
IsMultiRegionTrailindescribe-trails.
Exercise
A teammate runs put-event-selectors with a single advanced selector for S3 data events on one bucket. Afterwards, nobody can find IAM changes in the trail's logs. What happened, and how do you fix it?
Show answer
put-event-selectors replaces the full selector list. The call removed the selector with eventCategory = Management, so the trail no longer records management events such as IAM changes. Fix it by running put-event-selectors again with both selectors: one for eventCategory Management and one for the S3 data events. Then confirm with get-event-selectors. Events that occurred while the selector was missing were not recorded by this trail, but the last 90 days of management events are still in Event history.
Exercise
You want a trail that records only write management calls and S3 object events for the bucket arn:aws:s3:::payroll-data. Which fields and values do the two selectors need?
Show answer
Selector 1: eventCategory Equals Management, and readOnly Equals false. Selector 2: eventCategory Equals Data, resources.type Equals AWS::S3::Object, and resources.ARN StartsWith arn:aws:s3:::payroll-data/ (with the trailing slash). Conditions within a selector combine with AND, and the two selectors combine with OR.