From 7c4d039ca0e70df9f4f5e031235485e80b17e448 Mon Sep 17 00:00:00 2001 From: Burak Aktas Date: Thu, 6 Jun 2024 14:44:47 +0300 Subject: [PATCH 1/2] refactor(events): Define limit on Rule Targets --- packages/aws-cdk-lib/aws-events/lib/rule.ts | 2 ++ .../aws-cdk-lib/aws-events/test/rule.test.ts | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/packages/aws-cdk-lib/aws-events/lib/rule.ts b/packages/aws-cdk-lib/aws-events/lib/rule.ts index b96347e20ba2b..840eed9e5f866 100644 --- a/packages/aws-cdk-lib/aws-events/lib/rule.ts +++ b/packages/aws-cdk-lib/aws-events/lib/rule.ts @@ -320,6 +320,8 @@ export class Rule extends Resource implements IRule { private renderTargets() { if (this.targets.length === 0) { return undefined; + } else if (this.targets.length > 5) { + throw new Error('Event rule cannot have more than 5 targets.'); } return this.targets; diff --git a/packages/aws-cdk-lib/aws-events/test/rule.test.ts b/packages/aws-cdk-lib/aws-events/test/rule.test.ts index 80f3c5cbe53d6..dbcf5ad9bd01c 100644 --- a/packages/aws-cdk-lib/aws-events/test/rule.test.ts +++ b/packages/aws-cdk-lib/aws-events/test/rule.test.ts @@ -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'); From ef3bd89baf6104780efe89919fe3091cb7fd4e25 Mon Sep 17 00:00:00 2001 From: Burak Aktas Date: Thu, 6 Jun 2024 20:47:59 +0300 Subject: [PATCH 2/2] move target length check into validate method --- packages/aws-cdk-lib/aws-events/lib/rule.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/aws-cdk-lib/aws-events/lib/rule.ts b/packages/aws-cdk-lib/aws-events/lib/rule.ts index 840eed9e5f866..bbac64b14e63a 100644 --- a/packages/aws-cdk-lib/aws-events/lib/rule.ts +++ b/packages/aws-cdk-lib/aws-events/lib/rule.ts @@ -314,14 +314,16 @@ 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; } private renderTargets() { if (this.targets.length === 0) { return undefined; - } else if (this.targets.length > 5) { - throw new Error('Event rule cannot have more than 5 targets.'); } return this.targets;