Exploring Features in Amazon SNS: Delivery Policies and Message Formatting

Amazon Simple Notification Service (SNS) is a powerful service that enables you to send messages to multiple endpoints. Whether you are new to AWS or an experienced user, understanding the latest features can enhance your experience and improve your applications. In this blog post, we'll explore two new features introduced in Amazon SNS: Delivery Policies and Message Formatting.

Introduction to Amazon SNS

Amazon SNS allows you to create named Topics, subscribe to these Topics (with delivery options including email, HTTP/HTTPS, SMS, or an SQS queue), and publish messages to these Topics. It is a flexible, fully managed pub/sub messaging service that can be used to decouple and scale microservices, distributed systems, and serverless applications.

SNS Delivery Policies

The new SNS Delivery Policies feature gives you more control over how your messages are delivered, helping to manage delivery rates and error handling for each SNS endpoint. This is particularly useful when you need to avoid overwhelming a particular endpoint with a sudden influx of messages.

Key Components of Delivery Policies

  1. Retry Policy: Controls how retries are handled when message delivery fails.
  1. Throttle Policy: Manages the rate of message delivery to avoid overwhelming the endpoint.

Retry Policy Options

The Retry Policy includes several configurable options:

Here is an example of how you might configure a Retry Policy in JSON format:

{
  "minDelayTarget": 20,
  "maxDelayTarget": 20,
  "numNoDelayRetries": 3,
  "numMinDelayRetries": 2,
  "numMaxDelayRetries": 2,
  "backoffFunction": "linear"
}

Throttle Policy Options

The Throttle Policy has a single configurable option:

An example Throttle Policy in JSON format might look like this:

{
  "maxReceivesPerSecond": 1
}

Combining Policies

All message delivery attempts are governed by an "effective Delivery Policy" that combines the default policy with any custom policies you have set at the Topic and Subscription levels. Unspecified values at the Subscription level inherit from the Topic’s Delivery Policy, which in turn inherits from the default polic

SNS Message Formatting

The second feature, SNS Message Formatting, allows you to tailor the message content based on the type of subscription. This means you can send different messages to email, SMS, HTTP/HTTPS, or SQS endpoints.

How to Use Message Formatting

To use this feature, set the MessageStructure parameter to json when calling the SNS publish function. The message body must then contain a JSON object with a default message and optional messages for each protocol.

Here’s an example:

{
  "default": "Server busy.",
  "email": "Dear Jeff, your server is really busy and you should investigate. Best Regards, AWS.",
  "sms": "ALERT! Server Busy!!",
  "https": "ServerBusy"
}

In this example:

Example Code Snippet

Here’s how you can publish a message with different formats for each protocol using the AWS SDK for Python (Boto3):

Conclusion

Amazon SNS’s new Delivery Policies and Message Formatting features provide enhanced control and flexibility in managing your messaging needs. Delivery Policies allow you to fine-tune retry and throttle settings, ensuring your endpoints are not overwhelmed. Message Formatting lets you customize messages for different types of endpoints, improving communication effectiveness.

By leveraging these features, you can optimize message delivery and ensure that the right message reaches the right audience through the right channel. Whether you're new to AWS or an experienced user, these additions to Amazon SNS will help you build more robust and efficient messaging solutions.


Author / Developer : Divyansh Patel

Reference : https://aws.amazon.com/de/blogs/aws/new-features-for-amazon-sns-delivery-policies-and-message-formatting/