Skip to content

(events-targets): Wrong role used when adding multiple EventBus targets #19407

@gshpychka

Description

@gshpychka

What is the problem?

When adding an EventBus target to a rule, CDK creates a role that grants PutEvents on the event bus. If we add multiple event buses this way, they will all share a single role that only grant access to the first event bus that we pass.

Reproduction Steps

Consider the following code:

rule = events.Rule(
    self,
    "rule",
    event_pattern=events.EventPattern(
        source=["aws.ecr"],
    ),
)
for i in range(5):
   bus = events.EventBus(self, f"bus-{i}")
   rule.add_target(events_targets.EventBus(bus))

The rule will use a role that only has access to bus-0, and it will use this role for all targets.

What did you expect to happen?

Each target uses a different role with proper access.

What actually happened?

All targets share a role that only grant access to a single target.

CDK CLI Version

2.16

Framework Version

2.16

Node.js Version

17.7.1

OS

MacOS

Language

Typescript, Python

Language Version

No response

Other information

I understand the issue may be here:

const role = this.props.role ?? singletonEventRole(rule, [this.putEventStatement()]);

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions