aws_cloudtrail: Create Trail from CloudTrail delegated admin
- Dominant language
- TypeScript
- Stars
- 12.9k
- Forks
- 4.6k
- Avg merge
- 2d 3h
- Merged PRs (30d)
- 83
Description
### Describe the bug
I want to create an organization trail from a CloudTrail delegated administration account. The trail is supposed to ship all the logs to a S3 bucket in a centralized logging account.
When I try to create the trail from the management console, everything works. However when I create it with CDK, I get the following error:
> Error: The stack named my-stack failed creation, it may need to be manually deleted
from the AWS console: ROLLBACK_COMPLETE: Resource handler returned message: "Resource of type 'AWS::CloudTrail::Trail' with identifier 'my-trail' was not found."
From what I saw in the logs, the trail is created in the management account and when CFN calls the StartLogging API, it looks for the train in the delegated admin account.
This is the CDK code:
```python
from constructs import Construct
from aws_cdk import (
Stack,
aws_s3 as s3,
aws_cloudtrail as cloudtrail,
)
class CloudTrailManagementStack(Stack):
def __init__(
self,
scope: Construct,
id: str,
trail_name: str,
target_bucket: s3.Bucket,
**kwargs
) -> None:
super().__init__(scope, id, **kwargs)
trail = cloudtrail.Trail(
self,
"MyTrail",
bucket=target_bucket,
enable_file_validation=True,
is_multi_region_trail=True,
is_organization_trail=True,
management_events=cloudtrail.ReadWriteType.ALL,
trail_name=trail_name,
)
```
N.B: When running this stack in the management account, everything worked.
Another weir behavior, when I look at the logs, I can see the API StartLogging is called before CreateTrail.
### Expected Behavior
I would expect from the Trail to be created normally from the delegated admin account.
### Current Behavior
I get the following issue:
> Error: The stack named my-stack failed creation, it may need to be manually deleted
from the AWS console: ROLLBACK_COMPLETE: Resource handler returned message: "Resource of type 'AWS::CloudTrail::Trail' with identifier 'my-trail' was not found."
### Reproduction Steps
Run the stack. Verify the bucket has the [correct policy](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/create-s3-bucket-policy-for-cloudtrail.html).
### Possible Solution
_No response_
### Additional Information/Context
_No response_
### CDK CLI Version
2.90.0 (build 8c535e4)
### Framework Version
_No response_
### Node.js Version
v18.12.1
### OS
amazon linux
### Language
Python
### Language Version
_No response_
### Other information
_No response_
Contributor guide
Research direction
Start with the aws_cloudtrail.Trail definition and the synthesized AWS::CloudTrail::Trail resource for the delegated-admin case. Reproduce the stack using the provided Python configuration, then inspect CloudFormation and CloudTrail logs to confirm the StartLogging/CreateTrail ordering and account context. Done means the trail creates successfully from a delegated administration account and logging starts without the missing-resource error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- aws, python
- Domain
- cloud, devops
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100