Declaring resources#
A resource is created when its declaration says declare=True. The broker creates a queue, a
topic and the subscription between them at startup if they do not exist.
from faststream import FastStream
from faststream_sqs_sns import (
CapoSqsBroker,
RedrivePolicy,
SnsTopic,
SqsQueue,
)
broker = CapoSqsBroker(region="eu-central-1")
app = FastStream(broker)
events = SnsTopic("events", declare=True)
audit = SqsQueue(
"audit",
declare=True,
visibility_timeout=60,
redrive=RedrivePolicy("audit-dlq", max_receive_count=5),
attributes={"MessageRetentionPeriod": "86400"},
)
@broker.subscriber(audit, topic=events)
async def handle(event: dict[str, str]) -> None: ...
An existing queue keeps its own attributes: the declaration never overwrites them. When the
declared attributes differ from the existing queue's, SQS raises QueueNameExists (an
SqsServiceError) and the subscriber stops with an error that names the queue.
SqsQueue#
| Field | Meaning |
|---|---|
name |
A name, a URL or an ARN. |
declare |
Create the queue at startup when it does not exist. |
fifo |
Whether the queue is FIFO. The .fifo suffix sets it when omitted. |
visibility_timeout |
Seconds a received message stays invisible, 0 to 43200. The queue's own value when omitted. |
wait_time |
The long-poll seconds of a receive, 0 to 20. The default is 20. |
max_messages |
Messages of one receive, 1 to 10. The default is 10. |
redrive |
A RedrivePolicy. |
attributes |
Queue attributes in the service's spelling, applied on declaration only. The typed fields win over the same key. |
RedrivePolicy#
A message that is received too many times moves to the dead-letter queue.
| Field | Meaning |
|---|---|
dead_letter_queue |
The dead-letter queue: a name, a URL, an ARN or an SqsQueue. |
max_receive_count |
Receives after which the message moves, 1 to 1000. |
The dead-letter queue is created first, because SQS refuses a redrive policy whose target does
not exist. Every nack and every visibility timeout that runs out counts as a receive, so the
policy is what removes a message that keeps failing. With the default NACK_ON_ERROR
ack policy, a queue without a redrive policy returns a poison
message to the consumers as fast as the handler fails.
SnsTopic#
| Field | Meaning |
|---|---|
name |
A name or an ARN. |
declare |
Create the topic at startup when it does not exist. |
fifo |
Whether the topic is FIFO. The .fifo suffix sets it when omitted. |
attributes |
Topic attributes in the service's spelling, applied on declaration. |
A FIFO topic accepts only FIFO queues. The declaration raises SetupError before any call when
one side is FIFO and the other is not.
The queue policy#
SNS may write to a queue only when the queue's access policy allows it. When a declared queue
names a topic, the broker reads the queue's policy, adds one statement (its Sid is
faststream-sns-<topic name>) that lets sns.amazonaws.com send from that topic ARN, writes the
policy back, and then subscribes the queue to the topic. The policy is merged: other statements
stay, and an identical statement is not added twice.
Warning
The read and the write of the policy are separate calls. Two processes that declare different topics on one queue at the same moment can lose one statement. Declare a queue in one place, such as a single deployment step or one service.
Declaring by hand#
declare_queue and declare_topic create resources at a point you choose. Both leave an
existing resource alone. declare_queue returns the queue URL and declare_topic the topic ARN.
import logging
from faststream import FastStream
from faststream_sqs_sns import CapoSqsBroker, RedrivePolicy, SnsTopic, SqsQueue
broker = CapoSqsBroker(region="eu-central-1")
app = FastStream(broker)
logger = logging.getLogger(__name__)
@app.on_startup
async def declare() -> None:
await broker.connect()
queue_url = await broker.declare_queue(
SqsQueue("audit", redrive=RedrivePolicy("audit-dlq", max_receive_count=5))
)
topic_arn = await broker.declare_topic(SnsTopic("events"))
logger.info("%s %s", queue_url, topic_arn)
A publisher's destination declares itself the same way: declare=True on its SqsQueue or
SnsTopic creates the resource when the publisher starts.