Skip to content

faststream-sqs-sns#

An Amazon SQS broker for FastStream, with SNS fan-out publishing, maintained by the community.

service.py
from faststream import FastStream
from pydantic import BaseModel

from faststream_sqs_sns import CapoSqsBroker, SnsTopic, SqsQueue


class Order(BaseModel):
    id: int
    amount: float


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

orders = SnsTopic("orders", declare=True)
shipments = broker.publisher("shipments")


@broker.subscriber(SqsQueue("orders-billing", declare=True), topic=orders)
@shipments
async def bill(order: Order) -> dict[str, int]:
    # the return value is sent to the `shipments` queue
    return {"order_id": order.id}


@app.after_startup
async def place_order() -> None:
    await broker.publish(Order(id=1, amount=9.5), orders)
faststream run service:app

The handler reads the orders-billing queue, which the broker subscribes to the orders topic. It decodes the JSON body into Order, deletes the message once the handler returns, and sends its result to the shipments queue.

Install#

Pick the client library the broker talks to AWS with. Both are extras:

pip install "faststream-sqs-sns[capo]"
pip install "faststream-sqs-sns[aiobotocore]"
uv add "faststream-sqs-sns[capo]"

The package needs Python 3.10 or newer and FastStream 0.7. CapoSqsBroker needs the capo extra and AiobotocoreSqsBroker the aiobotocore extra. The other extras are otel (OpenTelemetry), prometheus and cli (the faststream command):

pip install "faststream-sqs-sns[capo,cli,otel,prometheus]"

Choosing a backend compares the two.

What you get#

  • Subscribers long-poll a queue and keep each message invisible to other consumers until the handler settles it.
  • Publishers send to a queue or to an SNS topic, one message or a batch, with attributes, FIFO group and deduplication ids and delays.
  • Queues, topics, redrive policies and the SNS subscription of a queue are created at startup when you declare them.
  • Settlement follows the SQS model: ack deletes, nack makes the message visible again at once, and reject deletes it. Deletes and visibility changes travel in batches.
  • Flow control bounds the messages held at once, and the broker extends the visibility of messages in work.
  • TestSqsBroker runs the service in process, with the limits of the real service.
  • LocalStack, the default AWS credential chain, static keys and custom endpoints are supported.
  • FastAPI, OpenTelemetry, Prometheus and AsyncAPI integrations are included.

Where to go next#