AWS EmulatorでEventBridgeのイベントを絞り込む方法
EventBridge のルールは、イベントパターンに書いた条件に合うイベントだけをターゲットに送ります。「高額な注文だけを別のキューに送る」「テスト用のイベントを除く」「クーポンを使っていない注文だけを拾う」のような振り分けを、前方一致・否定・項目の有無・数値の範囲などの条件で書けます。AWS のアカウントを使わずに AWS Emulator を手元で動かし、test-event-pattern で条件ごとの判定を確かめたうえで、同じパターンのルールに put-events でイベントを送り、キューに届いたものを確かめました。コマンドと出力は、検証で実行したものを載せています(アカウントIDの置き換えなど、変えた箇所はそのつど断っています)。
検証環境は Ubuntu 24.04.5(x86_64)、AWS Emulator 1.0.602(2026年11月24日リリース、Debian パッケージ)、AWS CLI 2.42.2、jq 1.7 です。AWS のアカウントと認証情報は使っていません。判定の結果はすべてエミュレーターでのもので、本物の Amazon EventBridge で同じ結果になるかは確かめていません。本物の AWS に触れている箇所は公式ドキュメントにもとづいています。料金、EventBridge Pipes のフィルター、カスタムのイベントバス、コンソールの Sandbox、$or の組み合わせの数の上限は試していません。
エミュレーターを起動し、判定を試す関数を用意する
AWS Emulator は、公式ガイドにある Linux での入れ方のとおりに Debian パッケージで入れ、署名を確かめてから apt-get install しました。起動は aws-emulator -t off > emulator.log 2>&1 のようにし、エミュレーターのログを emulator.log というファイルに書き出しました(-t off はテレメトリーを切る指定)。AWS CLI は、SDK の接続先をエミュレーターに向ける手順にある環境変数を設定して使っています。入れ方の詳しい説明は「AWS EmulatorをUbuntuに入れて使い始める方法」に載せました。以下の出力の中のアカウントIDは、<12桁のアカウントID> に置き換えています。
パターンが合うかどうかは、イベントを実際に送らなくても test-event-pattern で確かめられます。TestEventPattern の API リファレンスによると、渡すイベントには id、account、source、time、region、resources、detail-type が必要です。
$ cat event-1.json
{"id":"1","account":"123456789012","source":"example.orders","time":"2026-11-24T00:00:00Z","region":"us-east-1","resources":[],"detail-type":"OrderCreated","detail":{"orderId":"1001","sku":"BK-0001","amount":2500}}
$ aws events test-event-pattern --event-pattern '{"source":["example.orders"],"detail":{"sku":["BK-0001"]}}' --event file://event-1.json
{
"Result": true
}
$ aws events test-event-pattern --event-pattern '{"source":["example.orders"],"detail":{"sku":["bk-0001"]}}' --event file://event-1.json
{
"Result": false
}
ファイルの account は例示用の値です。必要な項目を省いて {"source":"example.orders","detail":{}} だけを渡すと、ValidationException(Parameter Event is not valid.)のエラーになりました。
条件をたくさん試すので、detail の部分だけを変えてイベントを組み立てる関数 tp を作りました。1つめの引数がパターン、2つめが detail の JSON で、結果の True か False だけを表示します。
# tp '<イベントパターン>' '<detail の JSON>'
# detail 以外の項目は固定のイベントを組み立て、test-event-pattern の結果(True / False)だけを表示する
tp() {
aws events test-event-pattern --event-pattern "$1" \
--event "$(jq -cn --argjson d "$2" '{id:"1",account:"123456789012",source:"example.orders",time:"2026-11-24T00:00:00Z",region:"us-east-1",resources:[],"detail-type":"OrderCreated",detail:$d}')" \
--query Result --output text
}
値は配列で書き、並べると「どれか」になる
イベントパターンの書き方の説明にあるとおり、照合する値は角かっこの配列で書きます。配列に値を並べると、どれか1つに合えば合ったことになります。配列にせず文字列をそのまま書くと、パターンとして受け付けられませんでした。
$ tp '{"detail":{"status":"paid"}}' '{"status":"paid"}'
aws: [ERROR]: An error occurred (InvalidEventPatternException) when calling the TestEventPattern operation: Event pattern is not valid. Reason: "status" must be an object or an array
at [Source: (String)"{"detail":{"status":"paid"}}"; line: 1, column: 22]
$ tp '{"detail":{"status":["paid","shipped"]}}' '{"status":"shipped"}'
True
同じ説明によると、パターンに書かなかった項目は、イベントに何が入っていても判定に使われません。
前方一致・後方一致と、大文字小文字を区別しない比較
最初の例で bk-0001 が合わなかったように、文字列は大文字と小文字を区別して比べます(イベントパターンの書き方の説明にも、文字列は1文字ずつそのまま比べるとあります)。比較演算子の一覧にある prefix(前方一致)、suffix(後方一致)は、中に equals-ignore-case を入れると、大文字と小文字を区別しなくなります。
$ tp '{"detail":{"sku":[{"prefix":"BK-"}]}}' '{"sku":"BK-0001"}'
True
$ tp '{"detail":{"sku":[{"prefix":"BK-"}]}}' '{"sku":"bk-0001"}'
False
$ tp '{"detail":{"sku":[{"prefix":{"equals-ignore-case":"bk-"}}]}}' '{"sku":"bk-0001"}'
True
$ tp '{"detail":{"file":[{"suffix":".png"}]}}' '{"file":"cover.PNG"}'
False
$ tp '{"detail":{"file":[{"suffix":{"equals-ignore-case":".png"}}]}}' '{"file":"cover.PNG"}'
True
$ tp '{"detail":{"status":[{"equals-ignore-case":"paid"}]}}' '{"status":"PAID"}'
True
wildcard の * は / をまたいで合う
wildcard では、* が任意の文字の並びに合います。images/*.png は、images/ の下の深い階層にあるファイルにも合いました。* は / で止まりません。パターンは値の先頭から照合されるので、thumbs/images/ で始まる値には合いませんでした。
$ tp '{"detail":{"file":[{"wildcard":"images/*.png"}]}}' '{"file":"images/cover.png"}'
True
$ tp '{"detail":{"file":[{"wildcard":"images/*.png"}]}}' '{"file":"images/2026/11/cover.png"}'
True
$ tp '{"detail":{"file":[{"wildcard":"images/*.png"}]}}' '{"file":"thumbs/images/cover.png"}'
False
$ tp '{"detail":{"file":[{"wildcard":"images/**.png"}]}}' '{"file":"images/cover.png"}'
aws: [ERROR]: An error occurred (InvalidEventPatternException) when calling the TestEventPattern operation: Event pattern is not valid. Reason: Consecutive wildcard characters at pos 8
at [Source: (String)"{"detail":{"file":[{"wildcard":"images/**.png"}]}}"; line: 1, column: 47]
** のように * を続けると、エラーになりました。公式ドキュメントのワイルドカードの説明にも、* を続けて書くことはできないとあります。
anything-but は、項目がないイベントには合わない
テスト用のイベントを除きたいときは、anything-but で「この値以外」を書きます。値の配列を渡すと、そのどれでもない値に合います。prefix と組み合わせると、「この文字列で始まらない値」になります。
$ tp '{"detail":{"channel":[{"anything-but":["test","internal"]}]}}' '{"channel":"web"}'
True
$ tp '{"detail":{"channel":[{"anything-but":["test","internal"]}]}}' '{"channel":"test"}'
False
$ tp '{"detail":{"channel":[{"anything-but":["test","internal"]}]}}' '{"orderId":"1001"}'
False
$ tp '{"detail":{"sku":[{"anything-but":{"prefix":"TEST-"}}]}}' '{"sku":"TEST-0001"}'
False
$ tp '{"detail":{"sku":[{"anything-but":{"prefix":"TEST-"}}]}}' '{"sku":"BK-0001"}'
True
3つめの結果のとおり、channel という項目そのものがないイベントは、anything-but に合いませんでした。イベントパターンの書き方の説明にも、パターンに書いた項目はすべてイベントに含まれている必要があるとあります。「test 以外」のつもりで書いても、項目を入れ忘れたイベントは届きません。
exists、null、空文字列はそれぞれ別のもの
クーポンを使っていない注文を拾うときは、coupon の項目が「ない」のか、null なのか、空文字列なのかで書き方が変わります。exists、[null]、[""] の3つを、4種類のイベントで試しました(全16通りのうち、null のイベントの4通りを載せます)。
$ tp '{"detail":{"coupon":[{"exists":true}]}}' '{"coupon":null}'
True
$ tp '{"detail":{"coupon":[{"exists":false}]}}' '{"coupon":null}'
False
$ tp '{"detail":{"coupon":[null]}}' '{"coupon":null}'
True
$ tp '{"detail":{"coupon":[""]}}' '{"coupon":null}'
False
16通りの結果を表にまとめると、次のようになりました。
![coupon の項目がない、null、空文字列、WELCOME10 の4種類のイベントを、exists:true、exists:false、[null]、[""] の4つの条件で判定した表。null は exists:true に合い、exists:false には合わない](https://neko-note.org/wp-content/uploads/2026/11/aws-emulator-eventbridge-pattern-exists-null-empty.png)
"coupon": null のイベントは、exists: true に合い、exists: false には合いませんでした。エミュレーターでは、値が null でも、項目があるものとして扱われています。null と空文字列の説明にあるとおり、[null] と [""] も入れ替えがききません。
エミュレーターでは、exists: false だけの条件で、null を入れた注文を取りこぼしました。「クーポンなし」を表すのに、送る側が項目を省く場合と null を入れる場合があるなら、注意が要ります。送る側の決まりを確かめてから条件を選びます。
exists は、値が文字列や数値の項目(いちばん下の階層の項目)に使います。オブジェクトの項目 customer に exists: true を書くと合わず、その中の rank に書くと合いました。比較演算子の一覧にも、exists はいちばん下の階層の項目にしか使えないとあります。
$ tp '{"detail":{"customer":[{"exists":true}]}}' '{"customer":{"rank":"gold"}}'
False
$ tp '{"detail":{"customer":{"rank":[{"exists":true}]}}}' '{"customer":{"rank":"gold"}}'
True
numeric で範囲を書く。文字列の数字は合わない
numeric には、">=" と "<" のように、比べ方と値を組にして並べます。[">=",1000,"<",5000] は「1000 以上 5000 未満」です。
$ tp '{"detail":{"amount":[{"numeric":[">=",1000,"<",5000]}]}}' '{"amount":1000}'
True
$ tp '{"detail":{"amount":[{"numeric":[">=",1000,"<",5000]}]}}' '{"amount":4999.5}'
True
$ tp '{"detail":{"amount":[{"numeric":[">=",1000,"<",5000]}]}}' '{"amount":5000}'
False
$ tp '{"detail":{"amount":[{"numeric":[">=",1000,"<",5000]}]}}' '{"amount":"2500"}'
False
$ tp '{"detail":{"amount":[{"numeric":[">=","1000"]}]}}' '{"amount":2500}'
aws: [ERROR]: An error occurred (InvalidEventPatternException) when calling the TestEventPattern operation: Event pattern is not valid. Reason: Value of >= must be numeric
at [Source: (String)"{"detail":{"amount":[{"numeric":[">=","1000"]}]}}"; line: 1, column: 40]
イベントの金額が文字列の "2500" だと、範囲の中の値でも合いませんでした。パターンの側に文字列の "1000" を書くと、パターンそのものがエラーになります。真偽値も同じで、true のイベントにパターンの ["true"](文字列)は合わず、[true] は合いました。
$ tp '{"detail":{"gift":[true]}}' '{"gift":true}'
True
$ tp '{"detail":{"gift":["true"]}}' '{"gift":true}'
False
数値をそのまま並べたときの比べ方には、エミュレーターと公式ドキュメントで違いがありました。イベントパターンの書き方の説明では、数値は文字列としての表し方で比べ、300 と 300.0 と 3.0e2 は同じとみなさない、とされています。エミュレーターでは、イベントに 300.0 と書いたファイルに、パターンの [300] が合いました。
$ jq -c .detail event-300.json; grep -o "\"amount\":[0-9.]*" event-300.json
{"amount":300.0}
"amount":300.0
$ aws events test-event-pattern --event-pattern '{"detail":{"amount":[300]}}' --event file://event-300.json --query Result --output text
True
イベントの 300.0 とパターンの 300 は、エミュレーターでは合いましたが、ドキュメントの説明では同じ値とはみなされません。比較演算子の一覧によると、numeric は値を倍精度の浮動小数点数として比べます。
IP アドレスの範囲と、配列の値
cidr は、IP アドレスが範囲に入っているかを見ます。IPv6 の範囲でも合いました(アドレスはどれも例示用のものです)。
$ tp '{"detail":{"clientIp":[{"cidr":"192.0.2.0/24"}]}}' '{"clientIp":"192.0.2.10"}'
True
$ tp '{"detail":{"clientIp":[{"cidr":"192.0.2.0/24"}]}}' '{"clientIp":"198.51.100.7"}'
False
$ tp '{"detail":{"clientIp":[{"cidr":"2001:db8::/32"}]}}' '{"clientIp":"2001:db8::1"}'
True
イベントの値が配列のときは、複数の値の照合の説明にあるとおり、パターンの値とイベントの配列に共通の値が1つでもあれば合います。注文の明細のように、オブジェクトが並んだ配列でも、どれか1つの sku が BK- で始まれば合いました。
$ tp '{"detail":{"tags":["gift"]}}' '{"tags":["sale","gift"]}'
True
$ tp '{"detail":{"tags":["gift"]}}' '{"tags":["sale"]}'
False
$ tp '{"detail":{"items":{"sku":[{"prefix":"BK-"}]}}}' '{"items":[{"sku":"CD-0001"},{"sku":"BK-0002"}]}'
True
別々の項目を「または」でつなぐ $or
パターンに項目を並べると、すべてに合うイベントだけが残ります。「金額が 10000 以上、または会員ランクが gold」のように、別々の項目のどちらかでよいときは $or に条件を並べます。
$ tp '{"detail":{"$or":[{"amount":[{"numeric":[">=",10000]}]},{"customer":{"rank":["gold"]}}]}}' '{"amount":12000,"customer":{"rank":"silver"}}'
True
$ tp '{"detail":{"$or":[{"amount":[{"numeric":[">=",10000]}]},{"customer":{"rank":["gold"]}}]}}' '{"amount":500,"customer":{"rank":"gold"}}'
True
$ tp '{"detail":{"$or":[{"amount":[{"numeric":[">=",10000]}]},{"customer":{"rank":["gold"]}}]}}' '{"amount":500,"customer":{"rank":"silver"}}'
False
ルールに付けて、届くイベントを確かめる
test-event-pattern の結果が、実際のルールでも同じになるかを確かめました。上の $or に「channel が test 以外」を足したパターンでルール priority-order を作り、SQS のキュー priority-orders をターゲットにします。
$ jq . pattern-priority.json
{
"source": [
"example.orders"
],
"detail-type": [
"OrderCreated"
],
"detail": {
"$or": [
{
"amount": [
{
"numeric": [
">=",
10000
]
}
]
},
{
"customer": {
"rank": [
"gold"
]
}
}
],
"channel": [
{
"anything-but": "test"
}
]
}
}
$ aws sqs create-queue --queue-name priority-orders --query QueueUrl --output text
https://127.0.0.1:9297/<12桁のアカウントID>/priority-orders
$ aws events put-rule --name priority-order --event-pattern file://pattern-priority.json --query RuleArn --output text
arn:aws:events:us-east-1:<12桁のアカウントID>:rule/priority-order
put-targets の前に、キューのポリシーでこのルールからの sqs:SendMessage を許可しています。ポリシーの書き方と、許可がないとキューに届かないことは「AWS EmulatorでEventBridgeからSQSに送る方法」に載せました。
$ aws events put-targets --rule priority-order --targets Id=priority-orders-queue,Arn=arn:aws:sqs:us-east-1:<12桁のアカウントID>:priority-orders
{
"FailedEntryCount": 0,
"FailedEntries": []
}
6件の注文を、1回の put-events で送りました。entries.json の各 Detail は次のとおりです。
$ jq -r ".[].Detail" entries.json
{"orderId":"5001","amount":12000,"channel":"web","customer":{"rank":"silver"}}
{"orderId":"5002","amount":500,"channel":"app","customer":{"rank":"gold"}}
{"orderId":"5003","amount":500,"channel":"web","customer":{"rank":"silver"}}
{"orderId":"5004","amount":20000,"channel":"test","customer":{"rank":"gold"}}
{"orderId":"5005","amount":20000,"customer":{"rank":"gold"}}
{"orderId":"5006","amount":"20000","channel":"web","customer":{"rank":"silver"}}
$ aws events put-events --entries file://entries.json --query FailedEntryCount
0
キューから3回受信し、届いた注文番号だけを並べました。
$ for i in 1 2 3; do aws sqs receive-message --queue-url https://127.0.0.1:9297/<12桁のアカウントID>/priority-orders --wait-time-seconds 2 --max-number-of-messages 10 --query "Messages[].Body" --output json | jq -r ".[]? | fromjson | .detail.orderId"; done | sort
5001
5002
届いたのは、高額の 5001 と gold 会員の 5002 だけでした。ほかの4件が届かなかった理由は、ここまでの判定と同じです。
| 注文 | 届いたか | 理由 |
|---|---|---|
| 5003 | 届かない | 金額も会員ランクも $or に合わない |
| 5004 | 届かない | channel が test |
| 5005 | 届かない | channel の項目がなく、anything-but に合わない |
| 5006 | 届かない | 金額が文字列の "20000" で、numeric に合わない |
5005 と 5006 は、条件を読むと通りそうに見えて、実際には落ちる例です。put-events の FailedEntryCount は 0 のままで、エミュレーターのログ(emulator.log)にも警告の行はありませんでした。
$ grep -c WARN emulator.log; true
0
; true は、一致する行が0件のときに grep が終了コード 1 で終わるのを避けるために付けています。
パターンに合わなかったイベントは、エラーにならずに届かないだけなので、届かないときは test-event-pattern に同じイベントを渡して、どの条件で落ちているかを1つずつ確かめると原因を絞り込めます。





