How to add SQS message attributes in SNS subscription?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Amazon Simple Notification Service (SNS) and Simple Queue Service (SQS) are integral components of AWS's messaging services, allowing for scalable and decoupled communication between microservices or distributed systems. SNS is a pub/sub message broker that enables applications to send time-critical messages to multiple subscribers through a "push" mechanism. SQS, on the other hand, is a queuing service designed to decouple system components by enabling asynchronous communication.
A common requirement in distributed architectures is to have SNS forward messages to an SQS queue with accompanying message attributes. This article will delineate the method and considerations for integrating SNS with an SQS queue in a way that encapsulates message attributes effectively.
Understanding SNS and SQS in Integration
When using SNS topics to notify SQS queues, one may need to include additional metadata in the form of message attributes. These attributes may contain essential information such as data type, source, priority, or any custom tag that might be useful to the consuming application.
Configuring an SNS to SQS Integration
Pre-requisites
To follow along, ensure you have:
- An AWS account with permissions to access SNS and SQS.
- The AWS CLI or AWS Management Console access.
- Basic familiarity with AWS cloud services.
Steps to Add SQS Message Attributes via SNS Subscription
1. Create an SNS Topic
To create an SNS topic:
- Using AWS Console: Navigate to the SNS dashboard and create a topic by specifying a name and other configurations as required.
- Using AWS CLI:
- Using AWS Console: Navigate to the SQS dashboard, create a new queue, and configure permissions.
- Using AWS CLI:
- SNS Console: On your topic's page, add a new subscription. Choose
SQSas the protocol and specify the queue's ARN. - AWS CLI:
- Using AWS SDK (e.g., Python's boto3):
- Using AWS SDK (e.g., Python's boto3):
- Permissions: Ensure SNS has the necessary permissions to send messages to the SQS queue. This involves setting the correct permission policy on the SQS queue.
- Attribute Limits: SNS supports up to 10 message attributes per message. Each attribute key, value, and the total byte size also have limitations. Refer to the AWS documentation for detailed quotas.
- Message Format: By default, SNS delivers messages in a JSON format that wraps the actual message and attributes. Applications consuming these messages must parse the JSON accordingly.

