[英]How to create an EventPattern that filters multiple sources (within an EventBridge rule)?
我目前正在使用帶有EventRule
的EventPattern
,它允許多個來源 - 例如 CloudFormation 片段:
EventRule:
Type: AWS::Events::Rule
Properties:
EventBusName: MyEventBus
Name: MyRuleName1
EventPattern:
source:
- eventSource1
- eventSource2
- eventSource3
但是我現在需要在這 3 個事件源中過濾不同的東西—— eventSource1
、 eventSource2
和eventSource3
有不同的模式。
我知道如果只有一個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.