本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
Amazon EventBridge 事件模式
事件模式擁有與其相符事件相同的結構。規則使用事件模式以選取事件並將事件路由到目標。事件模式與一個事件相符或不相符。
重要
在中 EventBridge,可以建立可能導致電 higher-than-expected 荷和節流的規則。例如,您可能會不小心建立導致無限迴圈的規則,其中該規則會以遞迴方式觸發而不會結束。假設,您建立的規則可能會偵測到已在 Amazon S3 儲存貯體上變更 ACL,並觸發軟體來將它們變更為所需的狀態。如果未謹慎寫入規則,後續對 ACL 的變更會再次觸發規則,建立無限循環。
如需有關撰寫精確規則和事件模式以將此類非預期結果降到最低的指引,請參閱 定義 Amazon EventBridge 規則的最佳實務 和 定義 Amazon EventBridge 事件模式的最佳實務。
以下影片介紹了事件模式的基礎知識:
主題
以下事件顯示了來自 Amazon EC2 的簡單 AWS 事件。
{
"version": "0",
"id": "6a7e8feb-b491-4cf7-a9f1-bf3703467718",
"detail-type": "EC2 Instance State-change Notification",
"source": "aws.ec2",
"account": "111122223333",
"time": "2017-12-22T18:43:48Z",
"region": "us-west-1",
"resources": [
"arn:aws:ec2:us-west-1:123456789012:instance/i-1234567890abcdef0"
],
"detail": {
"instance-id": "i-1234567890abcdef0",
"state": "terminated"
}
}
下列事件模式會處理所有 Amazon EC2 instance-termination
事件。
{
"source": ["aws.ec2"],
"detail-type": ["EC2 Instance State-change Notification"],
"detail": {
"state": ["terminated"]
}
}
建立事件模式
若要建立事件模式,您必須指定要進行事件模式比對的事件欄位。僅指定用於比對的欄位。上一個事件模式範例僅提供三個欄位的值:頂層欄位"source"
和"detail-type"
,以及"detail"
物件"state"
欄位內的欄位。 EventBridge 套用規則時,會忽略事件中的所有其他欄位。
若要事件模式匹配事件,該事件必須包含事件模式中所列的所有欄位名稱。欄位名稱也必須顯示在具有相同巢狀結構的事件中。
當您撰寫事件模式來比對事件時,您可以使用 TestEventPattern
API 或 test-event-pattern
CLI 指令,來測試您的模式是否能比對出正確的事件。如需詳細資訊,請參閱TestEventPattern。
相符事件值
在事件模式中,要比對的值位於 JSON 數組中,由方括號 ("["、"]") 包圍,以便您可以提供多個值。例如,若要比對來自 Amazon EC2 的事件 AWS Fargate,或者您可以使用下列模式,該模式與"source"
欄位值為"aws.ec2"
或的事件相符"aws.fargate"
。
{
"source": ["aws.ec2", "aws.fargate"]
}
建立事件模式時的考量
以下是建構事件模式時需要考量的一些注意事項:
EventBridge 會忽略事件中未包含在事件模式中的欄位。其效果是,對於未出現在事件模式中的字段擁有一個
"*": "*"
萬用字元。事件模式比對的值遵循 JSON 規則。您可以加入括在引號 (") 中的字串,、數字和關鍵字
true
、false
、null
。對於字符串, EventBridge 使用精確 character-by-character 匹配而不折疊大小寫或任何其他字符串規範化。
對於數字, EventBridge 使用字符串表示。例如,300、300.0 和 3.0e2 不會被視為相等。
如果為相同的 JSON 欄位指定了多個模式,則 EventBridge 只會使用最後一個模式。
-
請注意,當 EventBridge 編譯事件模式以供使用時,它會使用 dot (.) 作為連接字元。
這表示 EventBridge 會將下列事件模式視為相同:
## has no dots in keys { "detail" : { "state": { "status": [ "running" ] } } } ## has dots in keys { "detail" : { "state.status": [ "running" ] } }
並且這兩種事件模式都將符合以下兩個事件:
## has no dots in keys { "detail" : { "state": { "status": "running" } } } ## has dots in keys { "detail" : { "state.status": "running" } }
注意
這描述了當前的 EventBridge 行為,不應該依賴於不改變。
-
包含重複欄位的事件模式無效。如果樣式包含重複的欄位,則 EventBridge 僅考慮最終欄位值。
例如,以下事件模式將匹配相同的事件:
## has duplicate keys { "source": ["aws.s3"], "source": ["aws.sns"], "detail-type": ["AWS API Call via CloudTrail"], "detail": { "eventSource": ["s3.amazonaws.com"], "eventSource": ["sns.amazonaws.com"] } } ## has unique keys { "source": ["aws.sns"], "detail-type": ["AWS API Call via CloudTrail"], "detail": { "eventSource": ["sns.amazonaws.com"] } }
並 EventBridge 將以下兩個事件視為相同:
## has duplicate keys { "source": ["aws.s3"], "source": ["aws.sns"], "detail-type": ["AWS API Call via CloudTrail"], "detail": [ { "eventSource": ["s3.amazonaws.com"], "eventSource": ["sns.amazonaws.com"] } ] } ## has unique keys { "source": ["aws.sns"], "detail-type": ["AWS API Call via CloudTrail"], "detail": [ { "eventSource": ["sns.amazonaws.com"] } ] }
注意
這描述了當前的 EventBridge 行為,不應該依賴於不改變。
用於事件模式的比較操作
以下是中所有可用比較運算子的摘要 EventBridge。
比較運算子僅能在分葉節點上運作,除了 $or
和 anything-but
。
Comparison (比較) | 範例 | Rule syntax (規則語法) |
---|---|---|
及 |
位置為「紐約」,日期是「週一」 |
|
無論如何,但 | State 是除了「初始化」之外的任何值。 |
|
無論如何,但(開始於) | 地區不在美國。 |
|
任何事情,但(以結束) | FileName 不會以 .png 副檔名結尾。 |
|
任何事情-但(忽略大小寫) | State 是除「初始化」或任何其他外殼變化之外的任何值,例如「初始化」。 |
|
任何事情-但使用通配符 | FileName 不是包含的檔案路徑 |
|
地區位於美國。 |
|
|
開頭為 (忽略大小寫) | 服務名稱以字母「eventb」開頭,無論大小寫如何。 |
|
LastName 是空的。 |
|
|
等於 |
名稱為「Alice」 |
|
名稱為「Alice」 |
|
|
FileName 以 .png 副檔名結束 |
|
|
以 (忽略大小寫) 結束 | 服務名稱以字母「tbridge」或任何其他大小寫變化結尾,例如「TBRIDGE」。 |
|
ProductName 存在 |
|
|
ProductName 不存在 |
|
|
天氣是「下雨」以外的任何天氣 |
|
|
UserID 為 Null 值 |
|
|
價格為 100 |
|
|
價格大於 10,且小於或等於 20 |
|
|
或 |
PaymentType 是「信用卡」或「借記卡」 |
|
位置為「紐約」,或日期是「週一」。 |
|
|
任何副檔名為 .png 的檔案,位於“dir”資料夾內 |
|
範例事件和事件模式
您可以使用所有 JSON 資料類型和值來比對事件。下列範例顯示事件以及符合這些事件的事件模式。
欄位比對
您可以在一個字段的值進行比對。請考量下列 Amazon EC2 Auto Scaling 事件。
{
"version": "0",
"id": "3e3c153a-8339-4e30-8c35-687ebef853fe",
"detail-type": "EC2 Instance Launch Successful",
"source": "aws.autoscaling",
"account": "123456789012",
"time": "2015-11-11T21:31:47Z",
"region": "us-east-1",
"resources": [],
"detail": {
"eventVersion": "",
"responseElements": null
}
}
針對上一個事件,您可以使用 "responseElements"
欄位進行比對。
{
"source": ["aws.autoscaling"],
"detail-type": ["EC2 Instance Launch Successful"],
"detail": {
"responseElements": [null]
}
}
值比對
請考量下面的 Amazon Macie 事件,這是經截斷的事件。
{
"version": "0",
"id": "0948ba87-d3b8-c6d4-f2da-732a1example",
"detail-type": "Macie Finding",
"source": "aws.macie",
"account": "123456789012",
"time": "2021-04-29T23:12:15Z",
"region":"us-east-1",
"resources": [
],
"detail": {
"schemaVersion": "1.0",
"id": "64b917aa-3843-014c-91d8-937ffexample",
"accountId": "123456789012",
"partition": "aws",
"region": "us-east-1",
"type": "Policy:IAMUser/S3BucketEncryptionDisabled",
"title": "Encryption is disabled for the S3 bucket",
"description": "Encryption is disabled for the Amazon S3 bucket. The data in the bucket isn’t encrypted
using server-side encryption.",
"severity": {
"score": 1,
"description": "Low"
},
"createdAt": "2021-04-29T15:46:02Z",
"updatedAt": "2021-04-29T23:12:15Z",
"count": 2,
.
.
.
下列事件模式會符合嚴重性分數為 1 且計數為 2 的任何事件。
{
"source": ["aws.macie"],
"detail-type": ["Macie Finding"],
"detail": {
"severity": {
"score": [1]
},
"count":[2]
}
}