@aws-cdk-lib/aws-events: EventBus resource policy naming collision across stacks #29627
Labels
@aws-cdk/aws-events
Related to CloudWatch Events
bug
This issue is a bug.
effort/small
Small work item – less than a day of effort
p2
Describe the bug
Deploying two instances of the same stack (within the same account) containing an event bus with the same resource policy fails. The issue is that the
StatementId
field of the synthesized AWS::Events::EventBusPolicy resource must be unique across stacks.StatementId
is internally populated from thesid
of the providediam.PolicyStatement
:aws-cdk/packages/aws-cdk-lib/aws-events/lib/event-bus.ts
Lines 336 to 355 in 9a34664
Expected Behavior
The
sid
is documented in the following manner:I don't think it's reasonable to assume that end users should be aware that this value must be globally unique in this particular context. While the EventBridge Bus resource requires adding a
sid
for each statement of its resource policy, the service supports having two buses with the same policy containing the samesid
.To troubleshoot this issue, I had to read the underlying CDK source code to understand the underlying assumptions.
Current Behavior
addToResourcePolicy
requires providing asid
. This value is used as theStatementId
of the AWS::Events::EventBusPolicy resource which must be unique across stacks.Reproduction Steps
Create the following stack:
Deploy that stack twice:
You'll get the following error message:
Possible Solution
I think the CDK should follow the documented best practice:
In the context of the addToResourcePolicy method in the EventBus class, it think the
StatementId
should be an autogenerated unique value for each policy statement. That autogenerated value could also be used as the underlyingsid
to respect the typing definition of theiam.PolicyStatement
.Additional Information/Context
Sidenote: the raw PutPermission API call doesn't require providing a
StatementId
when a raw JSON policy is provided via thePolicy
parameter while the CloudFormation resource requires aStatementId
but supports providing aStatement
(which has the same badly copy pasted documentation as the upstream Policy). This is incredibly confusing.CDK CLI Version
2.133.0 (build dcc1e75)
Framework Version
No response
Node.js Version
v20.11.1
OS
macOS 14.4.1
Language
TypeScript
Language Version
5.3.3
Other information
No response
The text was updated successfully, but these errors were encountered: