DEV Community

EveCloudDev
EveCloudDev

Posted on • Edited on

SNS Topic Subscriptions Filter Demo

This is the blog post to the demo application on PubSub messaging with an Amazon Simple Notification Service (SNS) topic with multiple subscriptions. This post examines some of the features, capabilities, and limitations of message publishing with SNS.

Amazon SNS 

Amazon SNS is a fully managed service for publishing messages to multiple subscribers through an SNS topic. The relationship is one-to-many, one topic with many subscriptions. SNS supports application-to-application messaging and application-to-person notifications. 

Standard SNS Topic 

Topics of Standard type are fine when the message delivery order does not matter and the possibility of duplicate message delivery does not impact the expected outcome of your application.

Publishing messages

Message size is limited to a maximum of 256 KB. For larger messages under a maximum of 2 GB, the Amazon SNS Extended Client libraries are available to save these to an S3 bucket and publish just their references to the topic.

Message attributes are where you can provide custom, structured metadata in your messages. 

Subscription Filter Policy 

A filter policy on a subscription allows that subscriber to receive a subset of all the messages available. A filter policy is a JSON object, consisting of the property names and values, along with filtering operators, that will define what will make a match.

Demo application in CDK Python

GitHub repo:
https://github.com/evefonwu/pubsub-sns-topic-subscriptions-filter

The codebase consists of the following:

  1. An SNS Topic with two email subscriptions.
  2. A demo email subscription without a filter policy.
  3. A welcome_team email subscription with a filter policy to allow only 'demo' application, 'account_created' messages.
  4. A publisher with a FunctionURL publishes a hello message to the topic.
  5. A welcome_publisher with a FunctionURL publishes a 'demo' application, 'account_created' message to the topic.

Once deployed, the CDK will output the Function URLs of the two publishers.

Open email inboxes and confirm the email subscriptions to the SNS topic.

Access both URLs to trigger sending messages to the SNS topic. 

The demo subscription receives both emails since it does not have a filter policy. The welcome_team subscription receives only the message that matches its filter policy.

Resources

AWS SNS Documentation:
https://docs.aws.amazon.com/sns/latest/dg/welcome.html

AWS SNS Message Filtering Documentation:
https://docs.aws.amazon.com/sns/latest/dg/sns-message-filtering.html

FooBar Serverless Pub/Sub Architecture and AWS SNS - Exploring Event-Driven Patterns:
https://www.youtube.com/watch?v=rSIirRFa77s

Boto3 SNS Client:
https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sns.html

CDK Python Reference:
https://docs.aws.amazon.com/cdk/api/v2/python/

Top comments (0)