繁体   English   中英

如何创建过滤多个源的 EventPattern(在 EventBridge 规则内)?

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

我目前正在使用带有EventRuleEventPattern ,它允许多个来源 - 例如 CloudFormation 片段:

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

但是我现在需要在这 3 个事件源中过滤不同的东西—— eventSource1eventSource2eventSource3有不同的模式。

我知道如果只有一个source (或者它们都共享被过滤模式的同一部分),我可以使用detail进行过滤:

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

但是,我怎样才能对三个事件源中的每一个中的不同字段执行上述操作? 我知道我可以创建多个规则,但这似乎不是一个理想的解决方案(例如,需要注意使它们相互排斥)。

一个规则可以设置一种模式。 一些资源确实允许一个或一个列表,但在这种情况下,文档会说出来。 现在您需要提供 json 而不是列表,因此您必须创建多个规则。

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.

在此处输入图像描述

添加模式为 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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