Advanced Message Filtering

In the previous section, you explored basic message filtering using string comparisons and demonstrated the SNS routing behavior using various message attribute configurations to verify message delivery. Next, you will expand on basic message filtering using additional comparison techniques.


In this section, you will use numeric comparisons to route extra large Orders (quantity is 100 or greater).

Step 1: Create the Orders-XL SQS queue

Create an Amazon SQS queue, called Orders-XL, to durably store XL Order messages.

  1. Open the AWS Management Console for SQS in a new tab or window, so you can keep this step-by-step guide open.

  2. Choose Create queue at the top of your queue list.

  3. Select Standard Queue for the type of queue you want to configure.

  4. Enter a queue name Orders-XL

  5. Use the default values for the other configurations and choose Create queue

Step 2: Subscribe the Orders-XL SQS Queue to the Orders SNS Topic

  1. On the Queue detail page, select Subscribe to Amazon SNS topic.

  2. Select the topic that ends with Orders and choose Save.

  3. Verify that the queue is subscribed to the topic.

    Subscribed Queue

Step 3: Create Orders-XL subscription filter policy

  1. Open the AWS Management Console for SNS in a new tab or window, so you can keep this step-by-step guide open.

  2. From the list of topics, choose the Orders topic.

  3. In the search box, enter Orders-XL to find the Orders-XL SQS queue subscription.

    Search Subscriptions

  4. Select the Orders-XL subscription and then choose Edit.

    Select Subscription

  5. On the Edit subscription page, expand the Subscription filter policy section.

  6. In the JSON editor field, paste the following filter policy into the JSON editor.

    {
      "quantity": [
        {
          "numeric": [ ">=", 100 ]
        }
      ]
    }
    
  7. Choose Save changes.

    Edit Subscription Filter Policy

    Amazon SNS applies your filter policy to the subscription.


In this section, you will combine an EU string prefix comparison with the XL numeric comparison to route extra large, EU Orders using an AND comparison.

Step 4: Create the Orders-XL-EU SQS queue

Create an Amazon SQS queue, called Orders-XL-EU, to durably store XL EU Order messages.

  1. Open the AWS Management Console for SQS in a new tab or window, so you can keep this step-by-step guide open.

  2. Choose Create queue at the top of your queue list.

  3. Select Standard Queue for the type of queue you want to configure.

  4. Enter a queue name Orders-XL-EU

  5. Use the default values for the other configurations and choose Create queue

Step 5: Subscribe the Orders-XL-EU SQS Queue to the Orders SNS Topic

  1. On the Queue detail page, select Subscribe to Amazon SNS topic.

  2. Select the topic that ends with Orders and choose Save.

  3. Verify that the queue is subscribed to the topic.

    Subscribed Queue

Step 6: Create Orders-XL-EU subscription filter policy

  1. Open the AWS Management Console for SNS in a new tab or window, so you can keep this step-by-step guide open.

  2. From the list of topics, choose the Orders topic.

    Select Topic

  3. In the search box, enter Orders-XL-EU to find the Orders-XL-EU SQS queue subscription.

    Search Subscriptions

  4. Select the Orders-XL-EU subscription and then choose Edit.

    Select Subscription

  5. On the Edit subscription page, expand the Subscription filter policy section.

  6. In the JSON editor field, paste the following filter policy into the JSON editor.

    {
      "location": [
        { "prefix": "eu" }
      ],
      "quantity": [
        {
          "numeric": [ ">=", 100 ]
        }
      ]
    }
    
  7. Choose Save changes.

    Edit Subscription Filter Policy

    Amazon SNS applies your filter policy to the subscription.


Step 7: Publish test messages using the Event Generator

  1. Open the Event Generator for SNS in a new tab or window, so you can keep this step-by-step guide open.

  2. The Orders topic is pre-selected from the list of Topics.

    Event Generator - Topic

Message 1: Non-matching numeric message attributes

  1. Choose Add attribute.

    Event Generator - Add Attribute

  2. For the attribute Type, select Number. For the attribute Name, enter quantity. For the attribute Value, enter 50.

    Event Generator - Non-Matching Attributes

  3. Choose Publish to publish the message to the Orders SNS topic.

    Event Generator - Non-matching Attributes Publish

Message 2: Matching numeric message attributes

  1. For the quantity attribute, enter 100.

    Event Generator - Matching Attributes

  2. Choose Publish to publish the message to the Orders SNS topic.

    Event Generator - Matching Attributes Publish

Message 3: Matching AND comparison message attributes

  1. Choose Add attribute.

    Event Generator - Matching Attributes

  2. For the attribute Type, select as String. For the attribute Name, enter location. For the attribute Value, enter eu-west.

    Event Generator - Matching Attributes

  3. Choose Publish to publish the message to the Orders SNS topic.

    Event Generator - Matching Attributes Publish

Step 8: Verify message delivery

  1. Open the AWS Management Console for SQS in a new tab or window, so you can keep this step-by-step guide open.

  2. Observe the list of Messages Available for the queues.

    Verify Messages

    • The Orders queue shows 3 messages delivered, because the Orders subscription has no filter policy.
    • The Orders-EU queue shows 1 message delivered, because one message was published with location = eu-west.
    • The Orders-XL queue shows 2 messages delivered, because two messages were published with quantity = 100.
    • The Orders-XL-EU queue shows 1 message delivered, because one message was published with quantity = 100 AND location = eu-west.

Step 9: Cleanup

Cleanup the messages by purging the Orders, Orders-EU, Orders-XL, and Order-XL-EU SQS queues.

  1. From the queue list, select the Order queue and click the Purge button.

  2. Enter purge in the confirmation text box and click Purge.

  3. Refresh the queue list to verify that the Orders queue has been purged.

  4. Repeat the steps above to purge the Orders-EU, Orders-XL, and Order-XL-EU queues.

CONGRATULATIONS! You have successfully used subscription filters to route messages published to an SNS topic subscription and verified that it was sent to the correct SQS queue. In the next section, you will review how to handle errors sending messages from topics to subscribers.