Amazon SNS
object passing
cloud messaging
AWS tutorial
SNS object integration

how to pass an object to amazon SNS

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Amazon SNS does not accept an arbitrary in-memory object directly. The Message field of an SNS publish request is text, so the normal pattern is to serialize your object, usually as JSON, and let subscribers deserialize it on the other side.

Serialize the Object to JSON

If your application object is already dictionary-like, serialize it before calling Publish.

python
1import json
2import boto3
3
4sns = boto3.client("sns", region_name="us-east-1")
5
6payload = {
7    "event": "order.created",
8    "orderId": 1234,
9    "customerId": "cust-88",
10    "total": 49.99
11}
12
13response = sns.publish(
14    TopicArn="arn:aws:sns:us-east-1:123456789012:orders",
15    Message=json.dumps(payload),
16    Subject="Order event"
17)
18
19print(response["MessageId"])

From SNS's point of view, that object is now just a JSON string payload.

Understand What Subscribers Receive

The subscriber usually does not receive your object as a native object type either. It receives text and must parse it.

For example, a Python HTTP endpoint might do this:

python
1import json
2
3def handle_notification(request_body: str):
4    sns_envelope = json.loads(request_body)
5    message = json.loads(sns_envelope["Message"])
6
7    print(message["event"])
8    print(message["orderId"])

Notice the double parse:

  1. Parse the SNS envelope
  2. Parse your JSON object from the Message field

That pattern is common for HTTP and Lambda subscribers unless you configure raw message delivery where supported.

For SQS subscriptions, this distinction is especially important because many teams expect the queue message body to be their business object immediately, but by default it still arrives wrapped in the SNS notification structure.

Use Message Attributes for Metadata, Not Objects

SNS also supports MessageAttributes, but those are scalar values such as strings, numbers, or binary data. They are useful for routing and filtering, not for sending a nested object graph.

python
1response = sns.publish(
2    TopicArn="arn:aws:sns:us-east-1:123456789012:orders",
3    Message=json.dumps(payload),
4    MessageAttributes={
5        "eventType": {
6            "DataType": "String",
7            "StringValue": "order.created"
8        }
9    }
10)

This is a good pattern when subscribers need simple filterable metadata without opening the whole JSON body first.

If you use SNS subscription filter policies, message attributes become even more valuable because subscribers can opt in to only certain event types without parsing every message body.

Keep Size and Schema in Mind

SNS messages have a maximum size, so a huge object may not fit. In that case, publish a smaller event object and put the large payload somewhere else, such as S3, then include a pointer or identifier in the SNS message.

It also helps to keep the JSON schema stable:

  • Include an event name
  • Include a version if the payload may evolve
  • Keep field names predictable

Those habits make subscribers easier to maintain over time.

Another useful design choice is deciding whether subscribers should receive the SNS envelope or raw message delivery where the protocol supports it. That affects how many parsing layers the consumer needs.

If multiple services consume the topic, document the message contract in one place. SNS is only transport; schema discipline is still your application's job.

Common Pitfalls

  • Trying to pass a language-specific object directly fails because SNS transports text, not live runtime objects.
  • Forgetting to serialize the payload results in SDK type errors.
  • Treating MessageAttributes as a place for nested JSON objects does not work well because they are meant for simple scalar metadata.
  • Ignoring the SNS envelope causes subscribers to parse the wrong JSON layer.

Summary

  • To pass an object through SNS, serialize it, usually as JSON, and put that string into the Message field.
  • Subscribers receive text and must deserialize it back into an object.
  • Use MessageAttributes for small routing metadata, not for full object payloads.
  • For large payloads, publish a compact event and store the full object somewhere else.

Course illustration
Course illustration

All Rights Reserved.