Steve Chambers
Steve Chambers

Reputation: 39384

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 (e.g. care would need to be taken to make them mutually exclusive).

Upvotes: 2

Views: 3009

Answers (1)

Lukas Liesis
Lukas Liesis

Reputation: 26393

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.

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.

enter image description here

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

Upvotes: 2

Related Questions