What an SNS topic fanning out to SQS queues actually requires, from queue policies and dead-letter queues to raw message delivery and IAM, what the Terraform looks like, and how to get the same setup in your own AWS account from a topic and subscriptions declared in code.

An SNS topic that fans out to SQS queues is the usual way to let several services react to the same event on AWS, such as an order being placed, with one queue per consumer.
Each queue also needs a subscription, a policy that lets SNS write to it, a dead-letter queue and IAM permissions on both ends. This guide lists those pieces, shows the Terraform and then sets up the same thing from code.
AWS documents the queue policy step in its guide to subscribing a queue to a topic, and it is the step most often missed, because the subscription is created successfully and messages simply never arrive. The dead-letter queue docs describe the redrive policy, and raw message delivery removes the JSON envelope SNS otherwise wraps around each message.
For one topic and one consumer, with a dead-letter queue and raw delivery, the Terraform is about forty lines, and each extra consumer repeats the queue, dead-letter queue, policy and subscription:
# fanout.tf
resource "aws_sns_topic" "order_placed" {
name = "order-placed"
}
resource "aws_sqs_queue" "send_email_dlq" {
name = "send-email-dlq"
}
resource "aws_sqs_queue" "send_email" {
name = "send-email"
redrive_policy = jsonencode({
deadLetterTargetArn = aws_sqs_queue.send_email_dlq.arn
maxReceiveCount = 5
})
}
resource "aws_sqs_queue_policy" "send_email" {
queue_url = aws_sqs_queue.send_email.id
policy = jsonencode({
Version = "2012-10-17"
Statement = [{
Effect = "Allow"
Principal = { Service = "sns.amazonaws.com" }
Action = "sqs:SendMessage"
Resource = aws_sqs_queue.send_email.arn
Condition = { ArnEquals = { "aws:SourceArn" = aws_sns_topic.order_placed.arn } }
}]
})
}
resource "aws_sns_topic_subscription" "send_email" {
topic_arn = aws_sns_topic.order_placed.arn
protocol = "sqs"
endpoint = aws_sqs_queue.send_email.arn
raw_message_delivery = true
}
That still leaves the IAM policies for the publishing service and the consumer, the consumer's polling loop with visibility timeouts and batch deletes, and a way to run all of it locally, which usually means LocalStack. The subscription resource docs list further options such as filter policies and delivery policies.
Clicking through the console works once and is hard to repeat for staging, an aws sns subscribe and aws sqs set-queue-attributes script has to be kept in step with the code by hand, and CloudFormation or CDK describe the same resources in another format, so none of the usual alternatives to Terraform change the shape of the work.
Encore brings infrastructure from code, so the topic and its consumers are declared in the services that publish and consume, and Encore provisions the AWS resources for them in your own account. Each topic becomes an SNS topic and each subscription an SQS queue subscribed to it, with a dead-letter queue configured automatically, as the AWS infrastructure docs describe.

Run in your terminal to get started locally.
The orders service publishes, and two other services each get their own subscription, which on AWS means their own SQS queue and dead-letter queue. Each folder also has an encore.service.ts that exports new Service(...) with its name:
// orders/orders.ts
import { api, APIError } from "encore.dev/api";
import { Topic } from "encore.dev/pubsub";
export interface OrderPlaced {
orderId: string;
customerEmail: string;
totalCents: number;
}
// An SNS topic on AWS; Pub/Sub on GCP; in-process under `encore run`.
export const orderPlaced = new Topic<OrderPlaced>("order-placed", {
deliveryGuarantee: "at-least-once",
});
// Only signed-in callers may place orders; in production, load the order from the database instead of trusting the body.
export const place = api(
{ expose: true, auth: true, method: "POST", path: "/orders" },
async (p: OrderPlaced): Promise<{ ok: boolean }> => {
if (!p.orderId || !p.customerEmail.includes("@") || p.totalCents <= 0) {
throw APIError.invalidArgument("orderId, customerEmail and a positive totalCents are required");
}
await orderPlaced.publish(p);
return { ok: true };
},
);
// notifications/notifications.ts
import { Subscription } from "encore.dev/pubsub";
import log from "encore.dev/log";
import { orderPlaced } from "../orders/orders";
// An SQS queue subscribed to the topic, with its own dead-letter queue.
const _ = new Subscription(orderPlaced, "send-confirmation", {
handler: async (event) => {
log.info("send order confirmation", { orderId: event.orderId, to: event.customerEmail });
},
retryPolicy: { minBackoff: "10s", maxBackoff: "5m", maxRetries: 5 },
});
// analytics/analytics.ts
import { Subscription } from "encore.dev/pubsub";
import log from "encore.dev/log";
import { orderPlaced } from "../orders/orders";
// A second queue on the same topic: slow analytics never delays confirmations.
const _ = new Subscription(orderPlaced, "record-revenue", {
handler: async (event) => {
log.info("record revenue", { orderId: event.orderId, cents: event.totalCents });
},
});
encore run runs the topic and both subscriptions locally, so a POST /orders shows both handlers firing in the local dashboard's traces without LocalStack. A handler that throws is retried with backoff and moved to its dead-letter queue after the configured attempts, and delivery is at least once, so handlers that write data should be idempotent, as the Pub/Sub docs explain. If the topic already exists in your account, the SNS import guide connects the Encore topic to it instead of creating a new one.
To deploy, connect your AWS account as described in Connect your cloud account and push. Encore creates the SNS topic, the two queues with their dead-letter queues and the subscriptions between them, along with an IAM role for each service.
Adding a third consumer is a new file with a Subscription, which is also what a coding agent writes when asked to react to orders somewhere else, and it can run the new handler locally in the same change.
Best fit: Teams that want SNS and SQS fan-out in their own AWS account, defined by the services that publish and consume, with the same topic running locally.
Use Terraform when a platform team already manages SNS and SQS for many applications and the queues outlive any one service. Use Encore when the topics belong to the application, and you want each new consumer to come with its queue, dead-letter queue and permissions in the same change.
A subscription from the topic to the queue, and a queue policy that allows the sns.amazonaws.com service principal to call sqs:SendMessage on the queue, ideally with an aws:SourceArn condition set to the topic ARN. Without the policy, SNS delivery fails silently from the publisher's point of view.
Create a second queue and set a redrive policy on the main queue with the dead-letter queue's ARN and a maxReceiveCount. After a message has been received that many times without being deleted, SQS moves it to the dead-letter queue.
Usually yes, if the consumer only needs the message body. Without raw message delivery, SNS wraps each message in a JSON envelope with metadata, and the consumer has to parse the envelope to get to the payload.
With Encore you declare a Topic and one Subscription per consumer in the service code. Deploying to your AWS account provisions an SNS topic, an SQS queue for each subscription with a dead-letter queue, and an IAM role for each service, and encore run runs the same topic locally.
Deploy to your cloud on AWS and Google Cloud
without managing infrastructure
Encore lets developers and agents define application infrastructure directly in code, then automatically provisions it from local development to production in your AWS or GCP account.