在 Amazon EventBridge 事件模式中使用的比较运算符
以下是 EventBridge 中可用的所有比较运算符的汇总。
比较运算符仅适用于叶节点,$or
和 anything-but
除外。
Comparison(比较) | 示例 | Rule syntax(规则语法) | 事件总线支持 | 管道支持 |
---|---|---|---|---|
并且 |
位置为“纽约”,日期为“星期一” |
|
是 |
是 |
Anything-but | 状态是除了“initializing”之外的任何值。 |
|
是 |
是 |
Anything-but (begins with) | 区域不在美国。 |
|
是 |
否 |
Anything-but (ends with) | 文件名不以 .png 扩展名结尾。 |
|
是 |
否 |
Anything-but (ignore case) | 状态是除了“initializing”或任何其他大小写变体(例如“INITIALIZING”)之外的任何值。 |
|
是 |
否 |
Anything-but using a wildcard | 文件名不是包含 |
|
是 |
否 |
区域位于美国。 |
|
是 |
是 |
|
Begins with (ignore case) | 服务名称以字母“eventb”开头(不区分大小写)。 |
|
是 |
是 |
姓氏为空。 |
|
是 |
是 |
|
等于 |
名字为“Alice” |
|
是 |
是 |
名字为“Alice” |
|
是 |
是 |
|
FileName 以 .png 扩展名结尾 |
|
是 |
是 |
|
Ends with (ignore case) | 服务名称以字母“tbridge”或任何其他大小写变体(例如“TBRIDGE”)结尾。 |
|
是 |
是 |
产品名存在 |
|
是 |
是 |
|
产品名不存在 |
|
是 |
是 |
|
天气是除“下雨”以外的任何天气 |
|
是 |
是 |
|
用户 ID 为空 |
|
是 |
是 |
|
价格为 100 |
|
是 |
是 |
|
价格大于 10,且小于等于 20 |
|
是 |
是 |
|
Or |
付款类型为“信用卡”或“借记卡” |
|
是 |
是 |
位置为“纽约”,或日期为“星期一”。 |
|
是 |
是 |
|
位于“dir”文件夹中的任何扩展名为 .png 的文件 |
|
是 |
否 |
前缀匹配
您可以根据事件源中值的前缀匹配事件。您可以对字符串值使用 prefix 匹配。
例如,以下事件模式将匹配 "time"
字段以 "2017-10-02"
开头的任何事件,例如 "time": "2017-10-02T18:43:48Z"
。
{
"time": [ { "prefix": "2017-10-02" } ]
}
Prefix 匹配(忽略大小写)
您也可以将 equals-ignore-case
与 prefix.
搭配使用,在匹配前缀值时,不考虑值开头字符的大小写。
例如,以下事件模式将匹配其中 service
字段以 EventB
、EVENTB
、eventb
或这些字符的任何其他大写开头的任何事件。
{
"detail": {"service" : [{ "prefix": { "equals-ignore-case": "EventB" }}]}
}
后缀匹配
您可以根据事件源中值的后缀匹配事件。您可以对字符串值使用后缀匹配。
例如,以下事件模式将匹配 "FileName"
字段以 .png
文件扩展名结尾的任何事件。
{
"FileName": [ { "suffix": ".png" } ]
}
Suffix 匹配(忽略大小写)
您也可以将 equals-ignore-case
与 suffix.
搭配使用,在匹配后缀值时,不考虑值开头字符的大小写。
例如,以下事件模式将匹配其中 FileName
字段以 .png
、.PNG
或这些字符的任何其他大写结尾的任何事件。
{
"detail": {"FileName" : [{ "suffix": { "equals-ignore-case": ".png" }}]}
}
Anything-but 匹配
Anything-but 匹配会匹配除规则中指定的内容之外的任何内容。
您可以将 Anything-but 匹配与字符串和数值一起使用,包括仅包含字符串或仅包含数值的列表。
以下事件模式显示了 Anything-but 匹配与字符串和数字。
{
"detail": {
"state": [ { "anything-but": "initializing" } ]
}
}
{
"detail": {
"x-limit": [ { "anything-but": 123 } ]
}
}
以下事件模式显示了 Anything-but 匹配与一组字符串。
{
"detail": {
"state": [ { "anything-but": [ "stopped", "overloaded" ] } ]
}
}
以下事件模式显示了 Anything-but 匹配与一组数字。
{
"detail": {
"x-limit": [ { "anything-but": [ 100, 200, 300 ] } ]
}
}
Anything-but 匹配(忽略大小写)
您也可以将 equals-ignore-case
与 anything-but
搭配使用,匹配字符串值,不考虑字符大小写。
以下事件模式将匹配不包含字符串“initializing”、“INITIALIZING”、“Initializing”或这些字符的任何其他大写字母的 state
字段。
{
"detail": {"state" : [{ "anything-but": { "equals-ignore-case": "initializing" }}]}
}
您也可以将 equals-ignore-case
与 anything-but
搭配使用匹配值列表:
{
"detail": {"state" : [{ "anything-but": { "equals-ignore-case": ["initializing", "stopped"] }}]}
}
对前缀使用 anything-but 匹配
您可以将 prefix
与 anything-but
搭配使用来匹配不以指定值开头的字符串值。这包括单个的值或值的列表。
以下事件模式显示了与 "state"
字段中没有 "init"
前缀的任何事件相匹配的 anything-but 匹配。
{
"detail": {
"state": [ { "anything-but": { "prefix": "init" } } ]
}
}
以下事件模式显示了与一个前缀值列表搭配使用的 anything-but 匹配。此事件模式将匹配 "state"
字段中没有前缀 "init"
或 "stop"
的任何事件。
{
"detail": {
"state" : [{ "anything-but": { "prefix": ["init", "stop"] } } ] }
}
}
对后缀使用 anything-but 匹配
您可以将 suffix
与 anything-but
搭配使用来匹配不以指定值结尾的字符串值。这包括单个的值或值的列表。
以下事件模式将匹配 FileName
字段中不以 .txt
结尾的任何值。
{
"detail": {
"FileName": [ { "anything-but": { "suffix": ".txt" } } ]
}
}
以下事件模式显示了与一个后缀值列表搭配使用的 anything-but 匹配。此事件模式将匹配 FileName
字段中不以 .txt
或 .rtf
结尾的任何值。
{
"detail": {
"FileName": [ { "anything-but": { "suffix": [".txt", ".rtf"] } } ]
}
}
Anything-but 匹配与通配符搭配使用
您可以在为 anything-but 匹配指定的值中使用通配符(*)。这包括单个的值或值的列表。
以下事件模式将匹配 FileName
字段中不包含 /lib/
的任何值。
{
"detail": {
"FilePath" : [{ "anything-but": { "wildcard": "*/lib/*" }}]
}
}
以下事件模式显示了与一个含通配符的值列表搭配使用的 anything-but 匹配。此事件模式将匹配 FileName
字段中不含 /lib/
或 /bin/
的任何值。
{
"detail": {
"FilePath" : [{ "anything-but": { "wildcard": ["*/lib/*", "*/bin/*"] }}]
}
}
有关更多信息,请参阅 使用通配符进行匹配。
数值匹配
数值匹配适用于 JSON 数字值。仅限于 -5.0e9 和 +5.0e9(含)之间的值,精度为 15 位(小数点右侧为六位)。
以下代码显示的事件模式的数值匹配,仅匹配所有字段均为真的事件。
{
"detail": {
"c-count": [ { "numeric": [ ">", 0, "<=", 5 ] } ],
"d-count": [ { "numeric": [ "<", 10 ] } ],
"x-limit": [ { "numeric": [ "=", 3.018e2 ] } ]
}
}
IP 地址匹配
可以将 IP 地址匹配用于 IPv4 和 IPv6 地址。以下事件模式显示的 IP 地址匹配,匹配以 10.0.0 开头并以 0 到 255 之间的数字结尾的 IP 地址。
{
"detail": {
"sourceIPAddress": [ { "cidr": "10.0.0.0/24" } ]
}
}
Exists 匹配
Exists 匹配 用于确定事件的 JSON 中存在或不存在某个字段。
Exists 匹配仅适用于叶节点。它对于中间节点不起作用。
以下事件模式与任何具有 detail.state
字段的事件相匹配。
{
"detail": {
"state": [ { "exists": true } ]
}
}
上一事件模式与以下事件匹配。
{
"version": "0",
"id": "7bf73129-1428-4cd3-a780-95db273d1602",
"detail-type": "EC2 Instance State-change Notification",
"source": "aws.ec2",
"account": "123456789012",
"time": "2015-11-11T21:29:54Z",
"region": "us-east-1",
"resources": ["arn:aws:ec2:us-east-1:123456789012:instance/i-abcd1111"],
"detail": {
"instance-id": "i-abcd1111",
"state": "pending"
}
}
上一事件模式与以下事件不匹配,因为它没有 detail.state
字段。
{
"detail-type": [ "EC2 Instance State-change Notification" ],
"resources": [ "arn:aws:ec2:us-east-1:123456789012:instance/i-02ebd4584a2ebd341" ],
"detail": {
"c-count" : {
"c1" : 100
}
}
}
等于-忽略-大小写匹配
等于-忽略-大小写 匹配适用于字符串值,无论大小写如何。
以下事件模式匹配 detail-type
字段与指定字符串相匹配(无论大小写如何)的任何事件。
{
"detail-type": [ { "equals-ignore-case": "ec2 instance state-change notification" } ]
}
上一事件模式与以下事件匹配。
{
"detail-type": [ "EC2 Instance State-change Notification" ],
"resources": [ "arn:aws:ec2:us-east-1:123456789012:instance/i-02ebd4584a2ebd341" ],
"detail": {
"c-count" : {
"c1" : 100
}
}
}
使用通配符进行匹配
您可以使用通配符 (*) 匹配事件模式中的字符串值。
注意
目前,只有事件总线规则支持通配符。
在事件模式中使用通配符的注意事项:
-
可以在给定的字符串值中指定任意数量的通配符;但是,不支持连续通配符。
-
EventBridge 支持使用反斜杠字符 (\) 在通配符筛选器中指定文字 * 和 \ 字符:
-
字符串
\*
代表文字 * 字符 -
字符串
\\
代表文字 \ 字符
不支持使用反斜杠对其他字符进行转义。
-
通配符和事件模式的复杂性
规则使用通配符的复杂程度是有限的。如果规则过于复杂,在尝试创建规则时,EventBridge 会返回 InvalidEventPatternException
。如果您的规则生成此类错误,请考虑使用以下指南来降低事件模式的复杂性:
-
减少使用的通配符数量
仅在确实需要匹配多个可能值的位置使用通配符。例如,考虑以下事件模式,您想匹配同一区域中的事件总线:
{ "EventBusArn": [ { "wildcard": "*:*:*:*:*:event-bus/*" } ] }
在上述情况下,ARN 的许多部分将直接取决于您的事件总线所在的区域。因此,如果您使用
us-east-1
区域,则以下示例可能是不太复杂,但仍可与所需值匹配的模式:{ "EventBusArn": [ { "wildcard": "arn:aws:events:us-east-1:*:event-bus/*" } ] }
-
减少通配符后出现的重复字符序列
使用通配符后多次出现相同的字符序列,会增加处理事件模式的复杂性。请修改您的事件模式,最大限度地减少重复序列。例如,考虑以下示例,该示例与任何用户的文件名为
doc.txt
的文件相匹配:{ "FileName": [ { "wildcard": "/Users/*/dir/dir/dir/dir/dir/doc.txt" } ] }
如果您知道
doc.txt
文件只会出现在指定路径中,则可以通过以下方式减少重复的字符序列:{ "FileName": [ { "wildcard": "/Users/*/doc.txt" } ] }
具有多个匹配的复杂示例
您可以将多匹配条件组合成更复杂的事件模式。例如,以下事件模式组合了 anything-but
和 numeric
。
{
"time": [ { "prefix": "2017-10-02" } ],
"detail": {
"state": [ { "anything-but": "initializing" } ],
"c-count": [ { "numeric": [ ">", 0, "<=", 5 ] } ],
"d-count": [ { "numeric": [ "<", 10 ] } ],
"x-limit": [ { "anything-but": [ 100, 200, 300 ] } ]
}
}
注意
在构建事件模式时,如果您多次包含一个密钥,则最后一次引用将用于评估事件。例如,对于以下模式:
{ "detail": { "location": [ { "prefix": "us-" } ], "location": [ { "anything-but": "us-east" } ] } }
只有在评估 location
时才会考虑 { "anything-but": "us-east" }
。
具有 $or
匹配的复杂示例
您还可以创建复杂的事件模式,检查多个字段中是否有任何 字段值匹配。$or
用于创建事件模式,匹配多个字段中有任何值匹配的情况。
请注意,在 $or
构造中,您可以在各个字段的模式匹配中包含其他筛选器类型,例如数值匹配和数组。
满足以下任意条件时,匹配以下事件模式:
-
c-count
字段大于 0 或小于等于 5。 -
d-count
字段小于 10。 -
x-limit
字段等于 3.018e2。
{
"detail": {
"$or": [
{ "c-count": [ { "numeric": [ ">", 0, "<=", 5 ] } ] },
{ "d-count": [ { "numeric": [ "<", 10 ] } ] },
{ "x-limit": [ { "numeric": [ "=", 3.018e2 ] } ] }
]
}
}
注意
如果使用 $or
会导致超过 1000 个规则组合,接受事件模式(例如 PutRule
、CreateArchive
、UpdateArchive
和 TestEventPattern
)的 API 会引发 InvalidEventPatternException
。
要确定事件模式中规则组合的数量,请将事件模式中每个 $or
数组的参数总数相乘。例如,上面的模式包含一个 $or
数组,带有三个参数,因此规则组合的总数也是三个。如果您再添加一个包含两个参数的 $or
数组,则规则组合总数将为六个。