aws / aws/aws-cdk

events: EventPattern should validate against empty arrays at synth time

Open
#36,904 2 comments 0 reactions 0 assignees View on GitHub
@aws-cdk/aws-events bug effort/small p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

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

```ts
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 `Match` class 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()` in `packages/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 `EventPattern` interface 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_

Contributor guide

Open the contributing guide

Research direction

Read packages/aws-cdk-lib/aws-events/lib/util.ts and start at renderEventPattern(). Use the existing Match validation precedent, then verify that every listed array-typed EventPattern field rejects [] during synthesis with the stated error. Done means empty arrays cannot reach the rendered event pattern.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
cloud
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.