AWS Compute Blog
Managing Amazon SNS Subscription Attributes with AWS CloudFormation
This post is courtesy of Otavio Ferreira, Manager, Amazon SNS, AWS Messaging.
Amazon SNS is a fully managed pub/sub messaging and event-driven computing service that can decouple distributed systems and microservices. By default, when your publisher system posts a message to an Amazon SNS topic, all systems subscribed to the topic receive a copy of the message. By using Amazon SNS subscription attributes, you can customize this default behavior and make Amazon SNS fit your use cases even more naturally. The available set of Amazon SNS subscription attributes includes FilterPolicy, DeliveryPolicy, and RawMessageDelivery.
You can manually manage your Amazon SNS subscription attributes via the AWS Management Console or programmatically via AWS Development Tools (SDK and AWS CLI). Now you can automate their provisioning via AWS CloudFormation templates as well. AWS CloudFormation lets you use a simple text file to model and provision all the Amazon SNS resources for your messaging use cases, across AWS Regions and accounts, in an automated and secure manner.
The following sections describe how you can simultaneously create Amazon SNS subscriptions and set their attributes via AWS CloudFormation templates.
Setting the FilterPolicy attribute
The FilterPolicy attribute is valid in the context of message filtering, regardless of the delivery protocol, and defines which type of message the subscriber expects to receive from the topic. Hence, by applying the FilterPolicy attribute, you can offload the message-filtering logic from subscribers and the message-routing logic from publishers.
To set the FilterPolicy attribute in your AWS CloudFormation template, use the syntax in the following JSON snippet. This snippet creates an Amazon SNS subscription whose endpoint is an AWS Lambda function. Simultaneously, this code also sets a subscription filter policy that matches messages carrying an attribute whose key is “pet” and value is either “dog” or “cat.”
{
"Resources": {
"mySubscription": {
"Type" : "AWS::SNS::Subscription",
"Properties" : {
"Protocol": "lambda",
"Endpoint": "arn:aws:lambda:us-east-1:000000000000:function:SavePet",
"TopicArn": "arn:aws:sns:us-east-1:000000000000:PetTopic",
"FilterPolicy": {
"pet": ["dog", "cat"]
}
}
}
}
}
Setting the DeliveryPolicy attribute
The DeliveryPolicy attribute is valid in the context of message delivery to HTTP endpoints and defines a delivery-retry policy. By applying the DeliveryPolicy attribute, you can control the maximum number of retries the subscriber expects, the time delay between each retry, and the backoff function. You should fine-tune these values based on the traffic volume your subscribing HTTP server can handle.
To set the DeliveryPolicy attribute in your AWS CloudFormation template, use the syntax in the following JSON snippet. This snippet creates an Amazon SNS subscription whose endpoint is an HTTP address. The code also sets a delivery policy capped at 10 retries for this subscription, with a linear backoff function.
{
"Resources": {
"mySubscription": {
"Type" : "AWS::SNS::Subscription",
"Properties" : {
"Protocol": "https",
"Endpoint": "https://api.myendpoint.ca/pets",
"TopicArn": "arn:aws:sns:us-east-1:000000000000:PetTopic",
"DeliveryPolicy": {
"healthyRetryPolicy": {
"numRetries": 10,
"minDelayTarget": 10,
"maxDelayTarget": 30,
"numMinDelayRetries": 3,
"numMaxDelayRetries": 7,
"numNoDelayRetries": 0,
"backoffFunction": "linear"
}
}
}
}
}
}
Setting the RawMessageDelivery attribute
The RawMessageDelivery attribute is valid in the context of message delivery to Amazon SQS queues and HTTP endpoints. This Boolean attribute eliminates the need for the subscriber to process the JSON formatting that is created by default to decorate all published messages with Amazon SNS metadata. When you set RawMessageDelivery to true, you get two outcomes. First, your message is delivered as is, with no metadata added. Second, your message attributes propagate from Amazon SNS to Amazon SQS, when the subscribing endpoint is an Amazon SQS queue.
To set the RawMessageDelivery attribute in your AWS CloudFormation template, use the syntax in the following JSON snippet. This snippet creates an Amazon SNS subscription whose endpoint is an Amazon SQS queue. This code also enables raw message delivery for the subscription, which prevents Amazon SNS metadata from being added to the message payload.
{
"Resources": {
"mySubscription": {
"Type" : "AWS::SNS::Subscription",
"Properties" : {
"Protocol": "sqs",
"Endpoint": "arn:aws:sqs:us-east-1:000000000000:PetQueue",
"TopicArn": "arn:aws:sns:us-east-1:000000000000:PetTopic",
"RawMessageDelivery": "true"
}
}
}
}
Applying subscription attributes in a use case
Here’s how everything comes together. The following example is based on a car dealer company, which operates with the following distributed systems hosted on Amazon EC2 instances:
- Car-Dealer-System – Front-office system that takes orders placed by car buyers
- ERP-System – Enterprise resource planning, the back-office system that handles finance, accounting, human resources, and related business activities
- CRM-System – Customer relationship management, the back-office system responsible for storing car buyers’ profile information and running sales workflows
- SCM-System – Supply chain management, the back-office system that handles inventory tracking and demand forecast and planning
Whenever an order is placed in the car dealer system, this event is broadcasted to all back-office systems interested in this type of event. As shown in the preceding diagram, the company applied AWS Messaging services to decouple their distributed systems, promoting more scalability and maintainability for their architecture. The queues and topic used are the following:
- Car-Sales – Amazon SNS topic that receives messages from the car dealer system. All orders placed by car buyers are published to this topic, then delivered to subscribers (two Amazon SQS queues and one HTTP endpoint).
- ERP-Integration – Amazon SQS queue that feeds the ERP system with orders published by the car dealer system. The ERP pulls messages from this queue to track revenue and trigger related bookkeeping processes.
- CRM-Integration – Amazon SQS queue that feeds the CRM system with orders published by the car dealer system. The CRM pulls messages from this queue to track car buyers’ interests and update sales workflows.
The company created the following three Amazon SNS subscriptions:
- The first subscription refers to the ERP-Integration queue. This subscription has the RawMessageDelivery attribute set to true. Hence, no metadata is added to the message payload, and message attributes are propagated from Amazon SNS to Amazon SQS.
- The second subscription refers to the CRM-Integration queue. Like the first subscription, this one also has the RawMessageDelivery attribute set to true. Additionally, it has the FilterPolicy attribute set to {“buyer-class”: [“vip”]}. This policy defines that only orders placed by VIP buyers are managed in the CRM system, and orders from other buyers are filtered out.
- The third subscription points to the HTTP endpoint that serves the SCM-System. Unlike ERP and CRM, the SCM system provides its own HTTP API. Therefore, its HTTP endpoint was subscribed to the topic directly without a queue in between. This subscription has a DeliveryPolicy that caps the number of retries to 20, with exponential back-off function.
The company didn’t want to create all these resources manually, though. They wanted to turn this infrastructure into versionable code, and the ability to quickly spin up and tear down this infrastructure in an automated manner. Therefore, they created an AWS CloudFormation template to manage these AWS messaging resources: Amazon SNS topic, Amazon SNS subscriptions, Amazon SNS subscription attributes, and Amazon SQS queues.
Executing the AWS CloudFormation template
Now you’re ready to execute this AWS CloudFormation template yourself. To bootstrap this architecture in your AWS account:
-
- Download the sample AWS CloudFormation template from the repository.
- Go to the AWS CloudFormation console.
- Choose Create Stack.
- For Select Template, choose to upload a template to Amazon S3, and choose Browse.
- Select the template you downloaded and choose Next.
- For Specify Details:
- Enter the following stack name: Car-Dealer-Stack.
- Enter the HTTP endpoint to be subscribed to your topic. If you don’t have an HTTP endpoint, create a temp one.
- Choose Next.
- For Options, choose Next.
- For Review, choose Create.
- Wait until your stack creation process is complete.
Now that all the infrastructure is in place, verify the Amazon SNS subscriptions attributes set by the AWS CloudFormation template as follows:
- Go to the Amazon SNS console.
- Choose Topics and then select the ARN associated with Car-Sales.
- Verify the first subscription:
- Select the subscription related to ERP-Integration (Amazon SQS protocol).
- Choose Other subscription actions and then choose Edit subscription attributes.
- Note that raw message delivery is enabled, and choose Cancel to go back.
- Verify the second subscription:
- Select the subscription related to CRM-Integration (Amazon SQS protocol).
- Choose Other subscription actions and then choose Edit subscription attributes.
- Note that raw message delivery is enabled and then choose Cancel to go back.
- Choose Other subscription actions and then choose Edit subscription filter policy.
- Note that the filter policy is set, and then choose Cancel to go back
- Confirm the third subscription.
- Verify the third subscription:
- Select the subscription related to SCM-System (HTTP protocol).
- Choose Other subscription actions and then choose Edit subscription delivery policy.
- Choose Advanced view.
- Note that an exponential delivery retry policy is set, and then choose Cancel to go back.
Now that you have verified all subscription attributes, you can delete your AWS CloudFormation stack as follows:
- Go to the AWS CloudFormation console.
- In the list of stacks, select Car-Dealer-Stack.
- Choose Actions, choose Delete Stack, and then choose Yes Delete.
- Wait for the stack deletion process to complete.
That’s it! At this point, you have deleted all Amazon SNS and Amazon SQS resources created in this exercise from your AWS account.
Summary
AWS CloudFormation templates enable the simultaneous creation of Amazon SNS subscriptions and their attributes (such as FilterPolicy, DeliveryPolicy, and RawMessageDelivery) in an automated and secure manner. AWS CloudFormation support for Amazon SNS subscription attributes is available now in all AWS Regions.
For information about pricing, see AWS CloudFormation Pricing. For more information on setting up Amazon SNS resources via AWS CloudFormation templates, see:
- AWS CloudFormation User Guide: Resource Type AWS::SNS::Subscription
- Amazon SNS Developer Guide: Using an AWS CloudFormation Template to Create a Topic That Sends Messages to Amazon SQS Queues