The script and the failure

The script is idempotent and does five things in order. Create a dedicated S3 bucket with public access blocked and default SSE-S3 encryption, and attach the CloudTrail bucket policy. Create a CloudWatch Logs group and an IAM role the trail can assume to write to it. Create a multi-region trail with log file validation on, delivering management and global events to both the bucket and the log group. Start logging. Print the status. It had done this correctly in GovCloud accounts.

In a commercial account the first call, CreateBucket, returned InvalidLocationConstraint. Nothing after it ran.

Thing one: us-east-1 does not take a location

Every S3 region except one requires a CreateBucketConfiguration with a LocationConstraint naming the region. The exception is us-east-1, the original S3 region and the default, which rejects a constraint that names itself. My script passed the current region as the constraint unconditionally, which was correct in every region it had ever seen. The fix is four lines, with one unit test per branch:

def bucket_create_kwargs(bucket, region):
    kwargs = {"Bucket": bucket}
    if region != "us-east-1":
        kwargs["CreateBucketConfiguration"] = {"LocationConstraint": region}
    return kwargs

Thing two: the partition was a constant

Had the bucket been created, the next call would have failed anyway. Every ARN the script built began with arn:aws-us-gov:: the bucket in the policy, the log group, the IAM role, and later the trail itself. In a commercial account the partition is aws, and a policy granting access to a GovCloud bucket ARN from inside a commercial account grants access to nothing. The partition now follows the region: a region that starts with us-gov- is the GovCloud partition, anything else is aws. The region itself comes from an environment variable my tool exports per account, with the old literal kept only as a fallback for callers that bypass it.

That is the whole of the honest mistake. A script that had only ever run in one partition had the partition baked into it in two different ways, and neither was visible until it left home.

From one trail per account to one trail

The original script made a local trail: one per account, each with its own bucket. For an organization with a management account, a single organization trail created from the management account records management events for every member, current and future, and the members see it as a read-only shadow trail. The script gained an --org switch, and with it four additions.

Trusted access. An organization trail requires trusted access for cloudtrail.amazonaws.com on the organization. The script reads the list of enabled service principals, enables CloudTrail only if it is missing, and returns the organization ID from DescribeOrganization for the next step. Run it twice and the second run prints "already enabled".

The bucket policy admits the organization prefix. A local trail writes under AWSLogs/ACCOUNT_ID/. Member accounts in an organization trail write under AWSLogs/ORG_ID/, so the write statement needs both resources. The policy also pins aws:SourceArn to the trail's ARN on both statements, the ACL check and the write, so no other trail in any other account can deliver into this bucket even if someone learns its name. With placeholders in place of my values, the policy the script writes is:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "AWSCloudTrailAclCheck",
      "Effect": "Allow",
      "Principal": {"Service": "cloudtrail.amazonaws.com"},
      "Action": "s3:GetBucketAcl",
      "Resource": "arn:aws:s3:::BUCKET",
      "Condition": {"StringEquals": {
        "aws:SourceArn": "arn:aws:cloudtrail:REGION:MGMT_ACCOUNT_ID:trail/TRAIL_NAME"
      }}
    },
    {
      "Sid": "AWSCloudTrailWrite",
      "Effect": "Allow",
      "Principal": {"Service": "cloudtrail.amazonaws.com"},
      "Action": "s3:PutObject",
      "Resource": [
        "arn:aws:s3:::BUCKET/AWSLogs/MGMT_ACCOUNT_ID/*",
        "arn:aws:s3:::BUCKET/AWSLogs/ORG_ID/*"
      ],
      "Condition": {"StringEquals": {
        "s3:x-amz-acl": "bucket-owner-full-control",
        "aws:SourceArn": "arn:aws:cloudtrail:REGION:MGMT_ACCOUNT_ID:trail/TRAIL_NAME"
      }}
    }
  ]
}

One consequence of the source ARN condition: the trail's ARN is computed and written into the policy before the trail exists, from the region, the account and the trail name. Rename the trail later and the bucket stops accepting it. The bucket name itself ends in -org for an organization trail and -local otherwise, so the two kinds never collide.

The trail flag. IsOrganizationTrail=True on CreateTrail, alongside the existing multi-region, global-events and validation flags. If the trail already exists the script calls UpdateTrail with the same arguments, then starts logging either way.

Retention and a dry run. The log group had never expired anything, which is the CloudWatch Logs default. A --cwl-retention-days option puts a retention policy on the group; I use ninety days, which is long enough to search back through a quarter and short enough that the group does not grow without bound. And --dry-run prints the bucket name, log group, computed trail ARN, flags and retention, and whether trusted access would be enabled, without making a call. Run the dry run first and read the trail ARN before letting it create anything.

What happened

The trail was created from the management account on the first real run, logging on, with the log group at ninety days. Within minutes a member account showed it as a shadow trail. The one surprise was my own status command, which lists only the trails an account owns and so does not show the shadow trail at all; the API call that does is DescribeTrails with includeShadowTrails set. Ten unit tests cover the region default, the bucket arguments for us-east-1 and for another region, the policy's resources with and without an organization ID, the source ARN on every statement, and the partition mapping.

Two trade-offs stated plainly. The bucket uses SSE-S3 rather than a customer-managed key; the script leaves key management to a separate step so the key policy change is isolated from the trail creation. And there is a twelve-second sleep between creating the IAM role and creating the trail, because CloudTrail validates the role at create time and a brand-new role is not always visible yet. It is a sleep, not a retry loop, which is the kind of thing that works until the day it does not.

Before you run a trail script somewhere new

  • In us-east-1, omit the LocationConstraint. Everywhere else, include it.
  • Derive the ARN partition from the region. Grep the script for arn:aws and be suspicious of every literal.
  • For an organization trail: enable trusted access for CloudTrail, admit AWSLogs/ORG_ID/ in the bucket policy, set IsOrganizationTrail.
  • Pin aws:SourceArn to the trail ARN on both bucket statements, and know that renaming the trail breaks delivery.
  • Put a retention policy on the log group the day you create it.
  • Dry-run first, and confirm from a member account that the shadow trail appeared.