AWS
SNS
SQS
message attributes
cloud computing

How to add SQS message attributes in SNS subscription?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

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 SQS as 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.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.