Skip to content

Queues and topics#

SQS keeps queues. SNS keeps topics that deliver to queues. A subscriber reads a queue. A publisher sends to a queue or publishes to a topic.

service.py
from faststream import FastStream

from faststream_sqs_sns import CapoSqsBroker, SqsQueue

broker = CapoSqsBroker(region="eu-central-1")
app = FastStream(broker)


@broker.subscriber("orders")
async def by_name(order: dict[str, int]) -> None: ...


@broker.subscriber("https://sqs.eu-central-1.amazonaws.com/123456789012/payments")
async def by_url(payment: dict[str, int]) -> None: ...


@broker.subscriber("arn:aws:sqs:eu-central-1:123456789012:invoices")
async def by_arn(invoice: dict[str, int]) -> None: ...


@broker.subscriber(SqsQueue("audit", wait_time=10, max_messages=5, visibility_timeout=60))
async def by_schema(event: dict[str, int]) -> None: ...

Queue names#

A queue is named by its name, its URL, its ARN, or an SqsQueue declaration:

  • A name is resolved with GetQueueUrl once, when the subscriber or publisher starts. The URL is cached on the endpoint.
  • A URL is used as it is. Use it for a queue of another account.
  • An ARN is resolved with the owner account the ARN names.

SqsQueue also sets how the subscriber reads the queue: wait_time (the long poll, 0 to 20 seconds, 20 by default), max_messages (messages of one receive, 1 to 10) and visibility_timeout. The values are checked when the object is built, with no network call. Declaring resources lists the settings that create a queue.

A name ending in .fifo names a FIFO queue; fifo=True or fifo=False must agree with the suffix. See FIFO queues.

Router prefixes apply to names only, never to a URL or an ARN.

Topics#

A publisher names an SnsTopic or a topic ARN. A subscriber names a topic with topic=: the queue receives what is published to the topic.

service.py
from faststream import FastStream

from faststream_sqs_sns import CapoSqsBroker, SnsTopic

# a bare topic name becomes an ARN with this account; without it the broker lists the topics
broker = CapoSqsBroker(region="eu-central-1", account_id="123456789012")
app = FastStream(broker)

events = broker.publisher(SnsTopic("events"))


@broker.subscriber("audit", topic="events")
async def audit(event: dict[str, str]) -> None: ...


async def publish_events() -> None:
    await broker.publish({"kind": "created"}, SnsTopic("events"))
    await broker.publish(
        {"kind": "deleted"}, SnsTopic("arn:aws:sns:eu-central-1:123456789012:events")
    )
    await events.publish({"kind": "updated"})

A bare topic name becomes an ARN in this order:

  1. With declare=True on the topic, CreateTopic returns the ARN.
  2. With account_id on the broker, the ARN is built from the partition, the region and the account. No call is made.
  3. Otherwise the broker lists the topics once, when the publisher starts, and takes the one whose name matches. A missing topic raises TopicNotFoundError. When the service denies sns:ListTopics, the error names account_id as the way around it.

What topic= does#

When the queue declares itself (declare=True), topic= subscribes the queue to the topic with raw message delivery, and creates the topic when it declares itself too. SNS then passes the body and the attributes to the queue unchanged, so a consumer cannot tell a topic publish from a queue publish.

When the queue does not declare itself, topic= documents where its messages come from. It is what the AsyncAPI document shows and what the test broker uses to fan a topic publish out to queues.

A queue subscribed to a topic without raw delivery receives the SNS JSON envelope. See Consuming.