how to pass an object to amazon SNS
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
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.
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:
Notice the double parse:
- Parse the SNS envelope
- Parse your JSON object from the
Messagefield
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.
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
MessageAttributesas 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
Messagefield. - Subscribers receive text and must deserialize it back into an object.
- Use
MessageAttributesfor small routing metadata, not for full object payloads. - For large payloads, publish a compact event and store the full object somewhere else.
Related reading
- How to pass input parameter to AWS Glue Map.apply function
- How to pass Kinesis Firehose data to dynamodb table?
- How to pg_dump an RDS Postgres database?
- How to place SQLite database outside of NFS Persistent Volume
- How to point ApiGateway to a specific Lambda alias
- How to prevent a DynamoDB item being overwritten if an entry already exists
- How to prevent a DynamoDB item being overwritten if an entry already exists
- How to prevent duplicate SQS Messages?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.