aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.67k stars 3.92k forks source link

aws-events/Rule: Cannot create an EventBridge rule with a filter on resource #20486

Closed rcalme closed 2 years ago

rcalme commented 2 years ago

Describe the bug

EventBridge rules allow for content filtering. One type is {"anything-but": ...}.

I want to create an EventBridge Rule with CDK that specifies an anything-but filter for the rule's resources block. The EventPattern of the CDK Rule construct requires that resources be a string[], assuming ARN(s) are the only kinds of values that might be used.

Can the CDK EventPattern construct be changed such that resources (and any others that can accept content filters via CFN) are any-typed, so that they can be used as fluently as raw CFN allows?

Expected Behavior

CDK Rule and EventPattern constructs should allow the specification of content filters, as documented here.

Current Behavior

If I use:

new events.Rule(this, "Trigger", {
  description: "...",
  eventPattern: {
    source: ["aws.dataexchange"],
    detailType: ["Revision Published To Data Set"]
    // All subscribed ADX data sets EXCEPT this one
    resources: [{"anything-but": "aae4c2cd145a48454f9369d4a4db5c66"}
  },
  targets: [new targets.SqsQueue(...)]
});

Then an error is thrown, that:

error TS2322: Type '{ "anything-but": string; }' is not assignable to type 'string'.

Understandable. The CDK EventPattern construct is too strictly typed, and should probably be any[] instead.

Reproduction Steps

Workaround:

I tried to use the escape hatch, and set the same in the CfnRule construct:

(rule.node.defaultChild as events.CfnRule).eventPattern["resources"] = [
  {"anything-but": "aae4c2cd145a48454f9369d4a4db5c66"}
] 

But it seems to have no effect on the generated CFN.

Trying it this way throws errors about the EventPattern being a LazyAny and fails to compile:

const cfnRule = rule.node.defaultChild as events.CfnRule
  cfnRule.eventPattern = {
    ...cfnRule.eventPattern,
    resources: [{"anything-but": "aae4c2cd145a48454f9369d4a4db5c66"}]
  }

This solution finally worked:

(rule.node.defaultChild as events.CfnRule).addPropertyOverride(
  "EventPattern.resources",
  [{"anything-but": "aae4c2cd145a48454f9369d4a4db5c66"}]
);

And generated the intended CFN that gets the desired effect when deployed.

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.x

Framework Version

No response

Node.js Version

14.x

OS

macOS

Language

Typescript

Language Version

No response

Other information

No response

hrenod commented 2 years ago

A simpler workaround could be to override the type as any[]

resources: [{"anything-but": "aae4c2cd145a48454f9369d4a4db5c66"}] as any[]
adrian-skybaker commented 2 years ago

I think this issue is broader - it doesn't appear possible to define expressions on any of the built in fields (source|account|region|resources|detail-type|time|version).

So far I'm also not able to workaround in the Java SDK using unchecked casts. Here's a workaround using an escape hatch:

// Workaround https://github.com/aws/aws-cdk/issues/20486
((CfnRule) rule.getNode().getDefaultChild()).setEventPattern(Map.of("source", List.of(Map.of("exists", true))));
github-actions[bot] commented 2 years ago

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.