Sub topic 6
How logs are delivered and organized in S3
This chapter assumes you can read JSON, use the AWS CLI, and know what an S3 bucket, a prefix, and a bucket policy are. It also assumes you have created a trail, as covered in the previous chapter. Here you will learn what CloudTrail writes to S3, where each file lands, what permissions it needs, and how to read the files directly.
What a trail delivers
A trail does not stream events one at a time. CloudTrail batches events and writes them as compressed files. It delivers a new log file about every 5 minutes, and most events appear in S3 within about 5 minutes of the API call. This is a typical figure, not a guarantee. If no events occurred in a Region during an interval, no file is written for it.
Three kinds of objects can appear in the bucket:
| Object type | What it contains | Created when |
|---|---|---|
| Log file | A gzip-compressed JSON file with a `Records` array of events | Always, once the trail records events |
| Digest file | A signed summary of the log files delivered in the past hour | Log file integrity validation is enabled |
| Insights file | Insights events, which flag unusual API call rates or error rates | Insights is enabled on the trail |
Each type has its own top-level folder, so you can treat them separately in lifecycle rules, queries, and permissions.
The object key layout
CloudTrail builds every object key from fixed parts. For a log file, the key looks like this:
[prefix/]AWSLogs/[o-organization-id/]account-id/CloudTrail/region/yyyy/mm/dd/account-id_CloudTrail_region_yyyymmddThhmmZ_uniqueString.json.gzHere is a real-looking example:
AWSLogs/123456789012/CloudTrail/us-east-1/2026/11/20/123456789012_CloudTrail-us-east-1_20261120T0915Z_Xk3a9QpLm2Zr7TcB.json.gzThe parts mean the following:
- prefix is optional. You set it when you create the trail. It lets several trails, or other data, share one bucket.
- AWSLogs is a fixed folder name that CloudTrail always adds.
- o-organization-id appears only for an organization trail. It sits above the member account ID.
- account-id is the account whose activity the file describes, which can differ from the bucket's owner.
- region is the Region where the events occurred, not the Region of the bucket.
- yyyy/mm/dd is the date in UTC. A late-evening event in your local time zone may be in tomorrow's folder.
- The timestamp in the file name is the end of the delivery interval, also in UTC.
- uniqueString is a random string that prevents two files from having the same name.
Digest and Insights files use the same structure with a different second folder:
AWSLogs/123456789012/CloudTrail-Digest/us-east-1/2026/11/20/
AWSLogs/123456789012/CloudTrail-Insight/us-east-1/2026/11/20/Why the layout matters
The key layout is the main tool you have for narrowing a search. If you know the account, Region, and date, you can list one small folder instead of the whole bucket. It also makes several tasks easy:
- Lifecycle rules can target
AWSLogs/or a single prefix to move old logs to a cheaper storage class or delete them after your retention period. - Athena tables use the same folder structure for partition projection, so a query reads only the dates it needs. The querying chapter covers this.
- Bucket policies and IAM policies can grant an analyst read access to one account's folder with a resource such as
arn:aws:s3:::my-cloudtrail-logs-bucket/AWSLogs/123456789012/*.
Because logs from every Region go to the same bucket, a multi-Region trail produces one folder per Region under each account. Be careful when you look for activity: a call made in eu-west-1 is in the eu-west-1 folder even if the trail's home Region is us-east-1.
Global services, such as IAM, report their events in a single Region, which is us-east-1 for IAM. When you hunt for an IAM change, look in us-east-1, even if your workloads run elsewhere.
Permissions CloudTrail needs on the bucket
CloudTrail writes to your bucket as the service principal cloudtrail.amazonaws.com. The bucket policy must allow two actions: s3:GetBucketAcl on the bucket, and s3:PutObject on the log path. If either is missing, create-trail fails with an error about the bucket policy. If the policy is changed later, delivery stops and CloudTrail reports a delivery error on the trail.
This policy grants both and limits them to one trail with aws:SourceArn. That condition prevents another account's trail from writing into your bucket, a problem known as the confused deputy problem. Save it as policy.json:
{
"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 before you create the trail:
aws s3api put-bucket-policy \
--bucket my-cloudtrail-logs-bucket \
--policy file://policy.jsonNote these details:
- The trail ARN uses the trail's home Region, which is the Region where you created it, even for a multi-Region trail.
- If you set a prefix, the
PutObjectresource must include it, for example.../my-prefix/AWSLogs/123456789012/*. - For an organization trail, the resource must also cover
AWSLogs/o-organization-id/*. - For delivery to a bucket in another account, the bucket policy lives in the bucket owner's account and names the trail owner's account in the resource path.
Encryption at rest
By default, CloudTrail log files are encrypted with Amazon S3 managed keys (SSE-S3). You can instead provide a KMS key on the trail, which gives you SSE-KMS. With KMS, you control who can decrypt the logs through the key policy, and each decryption appears in CloudTrail as a Decrypt event. The key policy must let CloudTrail use the key to encrypt, and must let your analysts decrypt. The protection chapter covers the details. For now, remember that a reader of the bucket also needs kms:Decrypt on the key, or reads will fail with an access denied error even though the S3 permissions are correct.
Reading the files directly
You do not always need a query engine. For a quick look, list one day for one Region:
aws s3 ls s3://my-cloudtrail-logs-bucket/AWSLogs/123456789012/CloudTrail/us-east-1/2026/11/20/Then stream a file to standard output, decompress it, and filter it with jq:
aws s3 cp \
s3://my-cloudtrail-logs-bucket/AWSLogs/123456789012/CloudTrail/us-east-1/2026/11/20/123456789012_CloudTrail_us-east-1_20261120T0915Z_Xk3a9QpLm2Zr7TcB.json.gz - \
| gunzip \
| jq '.Records[] | {eventTime, eventName, user: .userIdentity.arn, ip: .sourceIPAddress}'The final - tells aws s3 cp to write to standard output. Each file is one JSON object with a single Records array, which is why the filter starts with .Records[].
To scan a whole day across all log files in one Region, a short script is easier. This Python example uses boto3, which you can install with pip install boto3. It finds every event with a given name:
import gzip
import json
import boto3
BUCKET = "my-cloudtrail-logs-bucket"
PREFIX = "AWSLogs/123456789012/CloudTrail/us-east-1/2026/11/20/"
EVENT_NAME = "StopLogging"
s3 = boto3.client("s3")
paginator = s3.get_paginator("list_objects_v2")
matches = []
for page in paginator.paginate(Bucket=BUCKET, Prefix=PREFIX):
for obj in page.get("Contents", []):
body = s3.get_object(Bucket=BUCKET, Key=obj["Key"])["Body"].read()
records = json.loads(gzip.decompress(body))["Records"]
matches.extend(r for r in records if r["eventName"] == EVENT_NAME)
for event in sorted(matches, key=lambda r: r["eventTime"]):
print(event["eventTime"], event["userIdentity"].get("arn"), event["sourceIPAddress"])The script sorts the matches at the end, because order within and across files is not guaranteed. It reads files one at a time, so it suits a single day and Region. For months of data, use Athena or CloudTrail Lake instead.
Common problems
| Symptom | Likely cause | What to check |
|---|---|---|
| `create-trail` fails with an insufficient permissions error | Bucket policy missing or wrong prefix in the `Resource` | Compare the policy's `Resource` path with the trail's prefix and account ID |
| Trail is logging but no files appear | Wrong folder, or no events in that interval | Run `get-trail-status` and check `LatestDeliveryTime` and `LatestDeliveryError` |
| Events appear in an unexpected date folder | Folders use UTC | Convert your local time to UTC before you build the prefix |
| Files exist but cannot be read | KMS key policy lacks `kms:Decrypt` for the reader | Check the key policy and the reader's IAM permissions |
| Logs from one Region seem missing | The trail is single-Region | Confirm `IsMultiRegionTrail` is true with `aws cloudtrail get-trail` |
| An IAM change is not in the Region you searched | Global service events are reported in one Region | Look in the `us-east-1` folder |
Exercise
A trail named org-audit-trail was created in us-east-1 with the prefix security. It delivers to my-cloudtrail-logs-bucket for account 123456789012. An engineer in eu-west-1 ran StopLogging at 23:50 UTC on 2026-11-19. Which S3 prefix should you list first to find the event, and which part of the bucket policy Resource must be present for delivery to work?
Show answer
List security/AWSLogs/123456789012/CloudTrail/eu-west-1/2026/11/19/. The folder is chosen by the Region where the call happened (eu-west-1), not the trail's home Region, and by the UTC date (19 November). The delivery interval ending shortly after 23:50 may also produce a file name with a timestamp just after midnight, so if you find nothing, also check the 2026/11/20 folder. The s3:PutObject statement's Resource must include the prefix: arn:aws:s3:::my-cloudtrail-logs-bucket/security/AWSLogs/123456789012/*.