Skip to main content

Topics

An SNS topic delivers a copy of every message to each queue subscribed to it, and each queue is then consumed like any other.

A publish to one SNS topic becomes a copy in each of two subscribed queues, each with a consumer of its ownPublisherSnsPublishTopic orders-eventsa copy for every subscriptionQueue billingQueue shippingBilling serviceShipping serviceWithout raw delivery, the queue receives the body wrappedin an SNS JSON envelope, and the attributes inside it
The diagram as text
  1. Publisher SnsPublish → Topic orders-events a copy for every subscription: Publish
  2. Topic orders-events a copy for every subscription → Queue billing: raw delivery
  3. Topic orders-events a copy for every subscription → Queue shipping
  4. Queue billing → Billing service: ReceiveMessage
  5. Queue shipping → Shipping service: ReceiveMessage
  6. Without raw delivery, the queue receives the body wrapped in an SNS JSON envelope, and the attributes inside it

A queue subscribes to a topic by its ARN. Every Publish to the topic becomes one message in each subscribed queue, so two services that read two queues both see every message. Within one queue, the consumers compete as on Queues.

With raw message delivery, the queue receives the body as it was published, and the message attributes become SQS message attributes. Without it, the queue receives a JSON envelope that holds the body and the attributes. A subscription made by subscribe_queue_to_topic turns raw delivery on, so a handler on the queue reads the same body and headers the publisher sent.

SNS sends to a queue only when the queue's access policy allows the SNS service sqs:SendMessage on behalf of the topic. subscribe_queue_to_topic adds a statement scoped to the topic through aws:SourceArn and keeps the statements already in the policy. A queue that an operator subscribes needs the same statement.

This crate publishes to SNS and does not consume from it: a topic is read through its queues. A topic named by name resolves through CreateTopic, which returns the ARN of an existing topic and creates a missing one. The ARN is cached after the first publish. A name that ends in .fifo makes a FIFO topic, which delivers only to FIFO queues. An SNS message is 256 KiB at most by default.

The behaviour is Amazon SNS's own (SQS as a subscriber, raw message delivery, CreateTopic, quotas).