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.51k stars 3.86k forks source link

(aws-events): EventPattern support for wildcard filters #28462

Open tyyzqmf opened 9 months ago

tyyzqmf commented 9 months ago

Describe the feature

As this whats-new: Amazon EventBridge announces support for wildcard filters in rules

Use Case

We can set event pattern in web console:

{
  "detail-type": ["Step Functions Execution Status Change"],
  "resources": [{
    "wildcard": "arn:aws:states:us-east-1:111122223333:execution:xxx:yyy*"
  }],
  "source": ["aws.states"]
}

But can not create this rule by CDK:

const ruleState = new Rule(this, 'ListenStateStatusChange', {
      description: 'Rule for listen SFN state machine status change',
      eventPattern: {
        source: ['aws.states'],
        detailType: ['Step Functions Execution Status Change'],
        resources: [`{ "wildcard": "arn:${Aws.PARTITION}:states:${Aws.REGION}:${Aws.ACCOUNT_ID}:execution:xxx:yyy*" }`],
      },
    });

Proposed Solution

No response

Other Information

No response

Acknowledgements

CDK version used

2.81.0

Environment details (OS name and version, etc.)

linux

pahud commented 9 months ago

But can not create this rule by CDK:

What error message was that?

Can you share the synthesized output specifically for the Rule resource?

tim-finnigan commented 9 months ago

Have you tried using the Match class? https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_events.Match.html

This came up in a similar issue earlier this year: https://github.com/aws/aws-cdk/issues/25424

tyyzqmf commented 8 months ago

@pahud this is synthesized output:

{
  "EventPattern": {
    "source": [
      "aws.states"
    ],
    "detail-type": [
      "Step Functions Execution Status Change"
    ],
    "resources": [
      {
        "Fn::Join": [
          "",
          [
            "{ \"wildcard\": \"arn:",
            {
              "Ref": "AWS::Partition"
            },
            ":states:",
            {
              "Ref": "AWS::Region"
            },
            ":",
            {
              "Ref": "AWS::AccountId"
            },
            ":execution:xxx:yyy*\" }"
          ]
        ]
      }
    ]
  }
}
tyyzqmf commented 8 months ago

Have you tried using the Match class? https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_events.Match.html

This came up in a similar issue earlier this year: #25424

@tim-finnigan Yes, I had try it. But there is no event pattern matcher can support for wildcard filter.

pahud commented 8 months ago

Yes it can deploy and I see this from the console

{
  "detail-type": ["Step Functions Execution Status Change"],
  "resources": ["{ \"wildcard\": \"arn:aws:states:us-west-2:903779448426:execution:xxx:yyy*\" }"],
  "source": ["aws.states"]
}

while this is expected:

{
  "detail-type": ["Step Functions Execution Status Change"],
  "resources": [{
    "wildcard": "arn:aws:states:us-east-1:111122223333:execution:xxx:yyy*"
  }],
  "source": ["aws.states"]
}

I guess the Match would need to support wildcard but before that you can use this workaround:

    const ruleState = new events.Rule(this, 'ListenStateStatusChange', {
      description: 'Rule for listen SFN state machine status change',
      eventPattern: {
        source: ['aws.states'],
        detailType: ['Step Functions Execution Status Change'],
        resources: [`{ "wildcard": "arn:${Aws.PARTITION}:states:${Aws.REGION}:${Aws.ACCOUNT_ID}:execution:xxx:yyy*" }`],
      },
    });

    (ruleState.node.defaultChild as events.CfnRule).addPropertyOverride('EventPattern.resources.0', 
     { wildcard: `arn:${Aws.PARTITION}:states:${Aws.REGION}:${Aws.ACCOUNT_ID}:execution:xxx:yyy*` },
    );

Let me know if it works for you.

tyyzqmf commented 8 months ago

@pahud Thank you for your prompt. I have successfully created the rule following code:

    const cfnRule = rule.node.defaultChild as events.CfnRule;
    cfnRule.addOverride('Properties.EventPattern.resources', [
     {wildcard: `arn:${Aws.PARTITION}:states:${Aws.REGION}:${Aws.ACCOUNT_ID}:execution:xxx:yyy*`},
    ]);
pahud commented 8 months ago

@tyyzqmf Awesome! I am leaving this issue open until we have better implementation in CDK.

jk2l commented 6 days ago

encountered this issue too