-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Description
Describe the bug
When an EventPattern property (such as detailType, source, account, etc.) is set to an empty array [], CDK synthesizes the template without any error. However, EventBridge rejects empty arrays in event patterns, causing a CloudFormation deployment failure:
Resource handler returned message: "Event pattern is not valid. Reason: Empty arrays are not allowed
at [Source: (String)"{"detail-type":[],"source":["example.source"],"account":["123456789012"]}";
line: 1, column: 18] (Service: EventBridge, Status Code: 400, ...)
CDK should validate that no EventPattern field is set to an empty array during synthesis, providing a clear error message before deployment.
Expected Behavior
cdk synth should throw a validation error when any EventPattern field is an empty array, e.g.:
Error: Invalid event pattern: 'detailType' must be a non-empty array
Current Behavior
CDK synthesizes the template successfully. The error is only discovered at deploy time when CloudFormation calls the EventBridge PutRule API.
Reproduction Steps
import * as events from 'aws-cdk-lib/aws-events';
const rule = new events.Rule(this, 'Rule', {
eventPattern: {
source: ['my.source'],
detailType: [], // <-- empty array, should be caught at synth time
},
});Running cdk synth succeeds. Running cdk deploy fails with the EventBridge validation error above.
Additional Information/Context
- The
Matchclass already validates for non-empty collections (e.g.,Match.anythingBut()throws if given an empty list), so there's precedent for this kind of validation in the same module. - The fix would be in
renderEventPattern()inpackages/aws-cdk-lib/aws-events/lib/util.ts— adding a check that no array value is empty before rendering the pattern. - All array-typed fields on the
EventPatterninterface should be validated:account,detailType,detail,id,region,resources,source,time,version.
CDK CLI Version
2.1104.0
Framework Version
aws-cdk-lib 2.236.0
Node.js Version
24.13.0
OS
macOS (Darwin 25.2.0)
Language
TypeScript
Language Version
5.9.3
Other information
No response