简体   繁体   中英

How to create an EventPattern that filters multiple sources (within an EventBridge rule)?

I'm currently using an EventRule with an EventPattern allowing more than one source - example CloudFormation fragment:

EventRule:
  Type: AWS::Events::Rule
  Properties:
    EventBusName: MyEventBus
    Name: MyRuleName1
    EventPattern:
      source:
        - eventSource1
        - eventSource2
        - eventSource3

But I now need to filter different things within these 3 event sources - eventSource1 , eventSource2 and eventSource3 have differing schemas.

Am aware I can use detail to filter if there is just one source (or they all share the same part of the schema being filtered):

    EventPattern:
      source:
        - eventSource1
      detail:
        fieldToFilter1:
          - 'yes'
        fieldToFilter2:
          - 'no'

But how can I do the above for different fields in each of the three event sources? Am aware I could create multiple rules but this doesn't seem an ideal solution (eg care would need to be taken to make them mutually exclusive).

One Rule can set one pattern. Some resources do allow either one or list of things yet in that case documentation would say it. Now you are expected to provide json and not a list, so you must create several rules.

btw, in your sample code you provided yaml instead json and it works cause yaml will be rendered to json but you were able to use json too, which in this case is often easier while you can copy json from aws console or some sample online.

在此处输入图像描述

Adding rule sample with pattern as json:

 GitCommitTrigger:
    Type: AWS::Events::Rule
    Properties:
      Description: !Sub "Trigger build project '${BuildProjectName}' after git commit"
      EventPattern: !Sub
        - |
          {
            "source": ["aws.codecommit"],
            "detail-type": ["CodeCommit Repository State Change"],
            "resources": ["${CodeCommitRepoArn}"],
            "detail": {
              "event": ["referenceCreated", "referenceUpdated"],
              "referenceType": ["branch"],
              "referenceName": ["${GitBranchName}"]
            }
          }
        - CodeCommitRepoArn: !Ref CodeCommitRepoArn
          GitBranchName: !Ref GitBranchName
      Name: !Sub "codebuild_${BuildProjectName}"
      RoleArn: !GetAtt GitCommitTriggerRole.Arn
      State: ENABLED
      Targets:
        - Arn: !GetAtt CodeBuild.Arn
          Id: CodeBuild-1
          RoleArn: !GetAtt GitCommitTriggerRole.Arn

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM