Amazon EventBridge 事件模式 - 亚马逊 EventBridge
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

Amazon EventBridge 事件模式

事件模式与它们匹配的事件具有相同的结构。规则使用事件模式来选择事件并将其发送到目标。事件模式匹配或不匹配事件。

重要

在中 EventBridge,可能创建导致无限循环的规则,即反复触发一个规则,即反复触发一个规则。例如,某规则可能检测到 S3 存储桶上的 ACL 已更改,然后触发软件以将 ACL 更改为所需状态。如果编写该规则时不小心,则 ACL 的后续更改将再次触发该规则,从而产生无限循环。

为防止出现这种情况,请在编写规则时使触发的操作不会重复激发同一规则。例如,您的规则可能仅在发现 ACL 处于错误状态时而不是在进行任何更改之后激发。

无限循环可能快速导致费用超出预期。我们建议您使用预算功能,以便在费用超出您指定的限制时提醒您。有关更多信息,请参阅通过预算管理成本

以下视频介绍了事件模式的基础知识:

以下事件演示了 Amazon EC2 的一个简单Amazon事件。

{ "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 EC2instance-termination 事件。

{ "source": ["aws.ec2"], "detail-type": ["EC2 Instance State-change Notification"], "detail": { "state": ["terminated"] } }

创建事件模式

要创建事件模式,请指定希望事件模式匹配的事件字段。仅指定用于匹配的字段。前面的事件模式示例仅提供三个字段的值:顶级字段"source""detail-type",以及"detail"对象"state"字段内的字段。 EventBridge 应用规则时忽略事件中的所有其他字段。

要使事件模式与事件相符,事件必须包含事件模式中列出的所有字段名称。字段名称还必须以相同的嵌套结构出现在事件中。

EventBridge 忽略事件中未包含在事件模式中的字段。结果是事件模式中未出现的字段有一个 “*”: “*” 通配符。

事件模式匹配的值遵循 JSON 规则。可以包括用引号(")括起来的字符串、数字和关键字 truefalsenull

对于字符串, EventBridge 使用精确的精确的逐 character-by-character 个字符串标准化,而不进行小写化或任何其他字符串标准化。

此外, EventBridge 使用字符串表示。例如,300、300.0 和 3.0e2 不相等。

在编写事件模式来匹配事件时,您可以使用TestEventPattern API 或test-event-pattern CLI 命令来测试您的模式是否匹配正确的事件。有关更多信息,请参阅TestEventPattern

以下是中可用的所有比较运算符的汇总 EventBridge。

注意

Pipes 目前并不支持所有的比较运算EventBridge符

Comparison(比较) 示例 Rule syntax(规则语法) 由管道支持

Null

用户 ID 为空

"UserID": [ null ]

Empty

LastName 是空的

"LastName": [""]

等于

名字为“Alice”

"Name": [ "Alice" ]

等于(忽略大小写)

名字为“Alice”

“名称”:[{"equals-ignore-case“: “爱丽丝”}]

And

位置为“纽约”,日期为“星期一”

"Location": [ "New York" ], "Day": ["Monday"]

或者

PaymentType 是 “信用卡” 或 “借记卡”

““: [PaymentType“信用卡”: [“信用卡”: “信用卡”: “Credi

或(多个字段)

位置为 “纽约”,或日期为 “星期一”。

“$or”]

天气是除“下雨”以外的任何天气

"Weather": [ { "anything-but": [ "Raining" ] } ]

数值(等于)

价格为 100

"Price": [ { "numeric": [ "=", 100 ] } ]

数值(范围)

价格大于 10,且小于等于 20

"Price": [ { "numeric": [ ">", 10, "<=", 20 ] } ]

Exists

ProductName 存在

“: [[[ProductName“exists”: [[“exists”: [[

不存在

ProductName 不存在

“: [[[[ProductName“exists”: [“exists”: [[

始于

地区位于美国

"Region": [ {"prefix": "us-" } ]

Ends with

FileName 以.png 扩展名结尾。

FileName“: [{“后缀”: “.png”}]

匹配值

在事件模式中,要匹配的值位于 JSON 数组中,由方括号(“[”、“]”)包围,因此您可以提供多个值。例如,要匹配来自 Amazon EC2 或Amazon Fargate的事件,您可以使用以下模式,该模式匹配"source"字段值为"aws.ec2"或的事件"aws.fargate"

{ "source": ["aws.ec2", "aws.fargate"] }

示例事件和事件模式

您可以使用所有 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] } }