Filter events from an Amazon MQ event source
You can use event filtering to control which records from a stream or queue Lambda sends to your function. For general information about how event filtering works, see Control which events Lambda sends to your function.
This section focuses on event filtering for Amazon MQ event sources.
Amazon MQ event filtering basics
Suppose your Amazon MQ message queue contains messages either in valid JSON format or as plain strings. An example record would look like the
following, with the data converted to a Base64 encoded string in the data
field.
For both Active MQ and Rabbit MQ brokers, you can use event filtering to filter records using the data
key. Suppose your
Amazon MQ queue contains messages in the following JSON format.
{ "timeout": 0, "IPAddress": "203.0.113.254" }
To filter only those records where the timeout
field is greater than 0, the FilterCriteria
object would be
as follows.
{ "Filters": [ { "Pattern": "{ \"data\" : { \"timeout\" : [ { \"numeric\": [ \">\", 0] } } ] } }" } ] }
For added clarity, here is the value of the filter's Pattern
expanded in plain JSON.
{ "data": { "timeout": [ { "numeric": [ ">", 0 ] } ] } }
You can add your filter using the console, AWS CLI or an AWS SAM template.
With Amazon MQ, you can also filter records where the message is a plain string. Suppose you want to process only records where the
message begins with "Result: ". The FilterCriteria
object would look as follows.
{ "Filters": [ { "Pattern": "{ \"data\" : [ { \"prefix\": \"Result: \" } ] }" } ] }
For added clarity, here is the value of the filter's Pattern
expanded in plain JSON.
{ "data": [ { "prefix": "Result: " } ] }
You can add your filter using the console, AWS CLI or an AWS SAM template.
Amazon MQ messages must be UTF-8 encoded strings, either plain strings or in JSON format. That's because Lambda decodes Amazon MQ byte arrays into UTF-8 before
applying filter criteria. If your messages use another encoding, such as UTF-16 or ASCII, or if the message format doesn't match the
FilterCriteria
format, Lambda processes metadata filters only. The following table summarizes the specific behavior:
Incoming message format | Filter pattern format for message properties | Resulting action |
---|---|---|
Plain string |
Plain string |
Lambda filters based on your filter criteria. |
Plain string |
No filter pattern for data properties |
Lambda filters (on the other metadata properties only) based on your filter criteria. |
Plain string |
Valid JSON |
Lambda filters (on the other metadata properties only) based on your filter criteria. |
Valid JSON |
Plain string |
Lambda filters (on the other metadata properties only) based on your filter criteria. |
Valid JSON |
No filter pattern for data properties |
Lambda filters (on the other metadata properties only) based on your filter criteria. |
Valid JSON |
Valid JSON |
Lambda filters based on your filter criteria. |
Non-UTF-8 encoded string |
JSON, plain string, or no pattern |
Lambda filters (on the other metadata properties only) based on your filter criteria. |