Skip to content

Commit

Permalink
chore(events): rule cannot have more than 5 targets (#30470)
Browse files Browse the repository at this point in the history
### Issue # (if applicable)

Closes #<issue number here>.

### Reason for this change
This is a minor refactor for preventing an Event Rule to have at most 5 associated targets defined during a synth. This is the main limit set by EventBridge https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-quota.html#eb-limits



### Description of changes
This is a very simple change in `aws-events` module to check if the `targets` array length of an `Event` is greater than 5. If it is then it throws an Error during build time.



### Description of how you validated changes
Added a unit test by in `rule.test.ts` by verifying an Error is being thrown when a Rule has more than 5 associated targets.



### Checklist
- [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md)

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
flexelem committed Jun 6, 2024
1 parent 312235a commit a05591d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/aws-cdk-lib/aws-events/lib/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,10 @@ export class Rule extends Resource implements IRule {
errors.push('Either \'eventPattern\' or \'schedule\' must be defined');
}

if (this.targets.length > 5) {
errors.push('Event rule cannot have more than 5 targets.');
}

return errors;
}

Expand Down
19 changes: 19 additions & 0 deletions packages/aws-cdk-lib/aws-events/test/rule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,25 @@ describe('rule', () => {
});
});

test('rule cannot have more than 5 targets', () => {
const app = new cdk.App();
const stack = new cdk.Stack(app);
const resource = new Construct(stack, 'Resource');
const rule = new Rule(stack, 'MyRule', {
schedule: Schedule.rate(cdk.Duration.minutes(10)),
targets: [
new SomeTarget('T1', resource),
new SomeTarget('T2', resource),
new SomeTarget('T3', resource),
new SomeTarget('T4', resource),
new SomeTarget('T5', resource),
new SomeTarget('T6', resource),
],
});

expect(() => app.synth()).toThrow(/Event rule cannot have more than 5 targets./);
});

test('get rate as token', () => {
const app = new cdk.App();
const stack = new cdk.Stack(app, 'MyScheduledStack');
Expand Down

0 comments on commit a05591d

Please sign in to comment.