Sub topic 15
Raw message delivery and the SNS message envelope
This chapter assumes you can create an SNS topic and subscribe an SQS queue to it, and that you know the fan-out pattern from the previous chapters. You should also be comfortable reading JSON in Python.
The envelope SNS adds
By default, SNS wraps every message it delivers to an SQS queue in a JSON document called the envelope. Your original message becomes a string in the Message field. SNS adds metadata around it, including the message ID, the topic ARN, a timestamp, and any message attributes.
An SQS consumer that reads the body of such a message gets this:
{
"Type": "Notification",
"MessageId": "5f1c0e0a-1111-2222-3333-444455556666",
"TopicArn": "arn:aws:sns:us-east-1:123456789012:order-events",
"Message": "{\"orderId\": \"1001\"}",
"Timestamp": "2026-11-26T18:27:24.957Z",
"MessageAttributes": {
"eventType": {"Type": "String", "Value": "order_created"}
}
}Notice that Message is a string that contains JSON. To get the order you must parse the body twice.
Raw message delivery
Raw message delivery is a subscription attribute. When it is true, SNS puts the published message directly in the SQS message body and skips the envelope. Message attributes are then sent as SQS message attributes instead of being nested in the body.
You set it when you subscribe, or change it later on an existing subscription:
import boto3
sns = boto3.client("sns")
topic_arn = sns.create_topic(Name="order-events")["TopicArn"]
queue_arn = "arn:aws:sqs:us-east-1:123456789012:orders"
resp = sns.subscribe(
TopicArn=topic_arn,
Protocol="sqs",
Endpoint=queue_arn,
ReturnSubscriptionArn=True,
)
sns.set_subscription_attributes(
SubscriptionArn=resp["SubscriptionArn"],
AttributeName="RawMessageDelivery",
AttributeValue="true",
)Replace the account ID and region in queue_arn with your own. The queue also needs the access policy from the earlier chapter.
Reading each format in a consumer
The two formats need different parsing code.
import json
def parse_enveloped(body):
envelope = json.loads(body)
return json.loads(envelope["Message"])
def parse_raw(body):
return json.loads(body)
enveloped = '{"Type": "Notification", "Message": "{\\"orderId\\": \\"1001\\"}"}'
raw = '{"orderId": "1001"}'
print(parse_enveloped(enveloped))
print(parse_raw(raw))Both calls print {'orderId': '1001'}.
Choosing and common errors
Use raw delivery when the queue serves one purpose and its consumers only care about the payload. This is also useful when the same consumer code reads messages from SQS producers and SNS-fed queues.
Keep the envelope when the consumer needs the topic ARN, the SNS message ID, or the timestamp, or when one queue receives from several topics and must tell them apart.
Common errors:
- Changing the setting on a live queue. Messages already in the queue keep their old format. A consumer that expects only one format will fail on the leftovers, so make the consumer accept both during the switch.
- Parsing the wrong shape. Calling
json.loadsonce on an envelope gives you a dictionary withTypeandMessagekeys, not your order. - Looking for attributes in the wrong place. With the envelope, attributes are inside the body. With raw delivery, read them from
MessageAttributeson the received SQS message, and request them withMessageAttributeNames=["All"]inreceive_message. - Assuming filters change. Filter policies work the same in both modes, because SNS applies them before delivery.
Exercise
A consumer reads from a queue subscribed to an SNS topic and fails with a KeyError on orderId after json.loads(body). The published message was {"orderId": "1001"}. What is the likely cause, and what are two ways to fix it?
Show answer
Raw message delivery is off, so the body is the SNS envelope and orderId sits inside the Message string. Fix it either by enabling RawMessageDelivery on the subscription, or by parsing twice with json.loads(json.loads(body)["Message"]).