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

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

亚马逊 EventBridge 事件模式

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

重要

在中 EventBridge,可以创建可能导致 higher-than-expected 收费和限制的规则。例如,您可能无意中创建了一条规则,导致无限循环,规则会以递归方式触发,不会结束。假设您创建了一条规则,来检测 S3 桶中的 ACL 更改,然后触发软件以将 ACL 更改为所需状态。如果编写该规则时不小心,则 ACL 的后续更改将再次触发该规则,从而产生无限循环。

有关如何编写精确的规则和事件模式,以最大限度地减少此类意外结果的指导,请参阅定义 Amazon EventBridge 规则的最佳实践定义 Amazon EventBridge 事件模式时的最佳实践

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

以下事件显示了一个来自 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 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 或的事件 Amazon Fargate,您可以使用以下模式,该模式匹配"source"字段值为"aws.ec2"或的事件"aws.fargate"

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

创建事件模式时的注意事项

构造事件模式时需要考虑以下事项:

  • EventBridge 忽略事件中未包含在事件模式中的字段。结果是,事件模式中没有出现的字段用 "*": "*" 通配符表示。

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

  • 对于字符串, EventBridge 使用精确 character-by-character 匹配而不进行大小写折叠或任何其他字符串规范化。

  • 对于数字, EventBridge 使用字符串表示形式。例如,300、300.0 和 3.0e2 不相等。

  • 如果为同一 JSON 字段指定了多个模式,则 EventBridge 只使用最后一个模式。

  • 请注意,在 EventBridge 编译事件模式以供使用时,它使用点 (.) 作为连接字符。

    这意味着 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。

比较运算符仅适用于叶节点,$oranything-but 除外。

Comparison(比较) 示例 Rule syntax(规则语法)

And

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

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

除此之外什么都行 状态是除了 “初始化” 之外的任何值。

"state": [ { "anything-but": "initializing" } ]

除此之外的任何东西(开头为)

地区不在美国。

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

除此之外的任何东西(结尾为)

FileName 不以.png 扩展名结尾。

"FileName": [ { "anything-but": { "suffix": ".png" } } ]

除此之外的任何东西(忽略大小写)

状态是除了 “初始化” 或任何其他大小写变体(例如 “初始化”)之外的任何值。

"state": : [{ "anything-but": { "equals-ignore-case": "initializing" }}]}

开头

地区在美国。

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

开头为(忽略大小写)

无论大小写如何,服务名称都以字母 “eventb” 开头。

{"service" : [{ "prefix": { "equals-ignore-case": "eventb" }}]}

LastName 是空的。

"LastName": [""]

等于

名字为“Alice”

"Name": [ "Alice" ]

等于(忽略大小写)

名字为“Alice”

"Name": [ { "equals-ignore-case": "alice" } ]

结束

FileName 以.png 扩展名结尾

"FileName": [ { "suffix": ".png" } ]

结尾为(忽略大小写)

服务名称以字母 “tbridge” 或任何其他大小写变体(例如 “TBRIDGE”)结尾。

{"service" : [{ "suffix": { "equals-ignore-case": "tBridge" }}]}

存在

ProductName 存在

"ProductName": [ { "exists": true } ]

不存在

ProductName 不存在

"ProductName": [ { "exists": false } ]

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

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

Null

用户 ID 为空

"UserID": [ null ]

数值(等于)

价格为 100

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

数值(范围)

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

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

Or

PaymentType 是 “贷方” 或 “借记卡”

"PaymentType": [ "Credit", "Debit"]

或(多个字段)

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

"$or": [ { "Location": [ "New York" ] }, { "Day": [ "Monday" ] } ]

通配符

位于“dir”文件夹中的任何扩展名为 .png 的文件

"FileName": [ { "wildcard": "dir/*.png" } ]

事件和事件模式示例

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