SNS Message Filtering

In Simple Notification Service (SNS) filtering messages based on the attributes in the message is one of the great features offered by SNS. Message filtering within SNS has many advantages. One key benefit is that you don’t have to set up a separate filtering process or service which will be resource intensive. You can save significant compute resources (hence money) with filtering within SNS, which otherwise should do using ec2 or Lambda.

Here, I would like to discuss how SNS message filtering feature can be configured.

Let’s assume there is an application which sends messages to a particular SNS topic which has two SQS subscriptions to perform different tasks as below:

  • SNS topic name: myevents
  • Message attribute “event” with value DoCharge (To be send to SQS queue queue_payment)
  • Message attribute “event” with value SendEmail (To be send to SQS queue queue_send_email)

Please note: Of course, here you can use two different sns topics for the two types of events which can be a suitable approach depends on your application, but to demonstrate the SNS filtering functionality, here I use single SNS topic.

1. Set up SNS topic
  1. Goto the AWS SNS console > Topics > Create topic
  2. Enter a name (myevents) and leave other options as default.
  3. Click on Create topic button which will create the topic.

To create the SNS topic in the AWS CLI, use below command:

aws sns create-topic --name myevents

This will output the ARN of the created topic as below:

{
    "TopicArn": "arn:aws:sns:eu-central-1:123456789012:myevents"
}
2. Set up SQS queues
  1. Goto AWS SQS console > Create New Queue
  2. Enter a name (queue_payment) for the queue and click on _Quick-Create Queue to create a standard queue with default settings.
  3. Once queue is created, select the queue from the list of queues and click on Permissions tab below.
  4. Click on Add a Permission to set up permission for SNS topic to use this SQS queue.
  5. In the Add permissions form, Action as Send Message and the SNS topic’s ARN as shown below.

    Image: Set up permission in SQS Queue for SNS to Send Message

    Image: Set up permission in SQS Queue for SNS to Send Message

  6. Do the same for the 2nd queue (queue_send_email).

  7. Copy both the ARNs of the SQS queues created which is required for the next step.

3. Set up SNS Subscriptions
  1. Go back to SNS and choose the topic myevents
  2. Click on Create Subscription and enter below values and create the first subscription:
    • Topic ARN: use the default
    • Protocol : Amazon SQS
    • Endpoint : ARN of queue queue_payment
    • Subscription filter policy: {event": ["DoCharge"]}
  3. Use below values to create the second subscription:
    • Topic ARN: use the default
    • Protocol : Amazon SQS
    • Endpoint : ARN of queue queue_send_email
    • Subscription filter policy: {event": ["SendEmail"]}
4. Testing

Once above steps are completed, you can use the Publish Message option on the topic to test the functionality.

Use the message payload on the body section and add message attribute event with the value SendEmail and publish the message. You can see the particular message is appeared in queue_send_email SQS queue.

Image: Publish SNS message with attributes

Image: Publish SNS message with attributes