> ## Documentation Index
> Fetch the complete documentation index at: https://docs.fjall.io/llms.txt
> Use this file to discover all available pages before exploring further.

# SNS Topic

> Publish events to Lambda, SQS, and HTTP subscribers with an AWS SNS topic in Fjall for fan-out notifications.

## Overview

The `SNSTopic` construct creates an Amazon SNS topic for pub/sub messaging. Use it to fan out events to multiple subscribers (Lambda, SQS, HTTP endpoints) from a single publish call.

## Import

```typescript theme={null}
import { SNSTopic } from "@fjall/components-infrastructure/lib/resources/aws/messaging/sns";
```

## Basic Usage

```typescript theme={null}
const topic = new SNSTopic(scope, "Alerts", {});
```

## Using the Messaging Factory

The recommended way to add SNS to a Fjall application is through the `MessagingFactory`:

```typescript theme={null}
import { MessagingFactory } from "@fjall/components-infrastructure/lib/patterns/aws/messaging";

const alerts = app.addMessaging(
  MessagingFactory.build("Alerts", {
    type: "topic",
  }),
);
```

## Properties

| Property                    | Type                    | Default        | Description                                                                                |
| --------------------------- | ----------------------- | -------------- | ------------------------------------------------------------------------------------------ |
| `topicName`                 | `string`                | Auto-generated | Topic name                                                                                 |
| `displayName`               | `string`                | None           | Human-readable name for SMS subscriptions                                                  |
| `fifo`                      | `boolean`               | `false`        | Create a FIFO topic (strict ordering)                                                      |
| `contentBasedDeduplication` | `boolean`               | `true` (FIFO)  | Content-based deduplication. Defaults to `true` on FIFO topics, ignored on standard topics |
| `removalPolicy`             | `"DESTROY" \| "RETAIN"` | `"RETAIN"`     | What happens on stack deletion                                                             |

## Methods

| Method                    | Returns  | Description                                |
| ------------------------- | -------- | ------------------------------------------ |
| `getTopicArn()`           | `string` | Topic ARN                                  |
| `getTopicName()`          | `string` | Topic name                                 |
| `getTopic()`              | `ITopic` | CDK Topic construct                        |
| `grantPublish(grantee)`   | `Grant`  | Grant permission to publish messages       |
| `grantSubscribe(grantee)` | `Grant`  | Grant permission to subscribe to the topic |

## Examples

### Standard Topic

```typescript theme={null}
const notifications = app.addMessaging(
  MessagingFactory.build("Notifications", {
    type: "topic",
    displayName: "Order Notifications",
  }),
);

notifications.grantPublish(apiService);
```

### FIFO Topic

FIFO topics deliver messages in order and support deduplication:

```typescript theme={null}
const events = app.addMessaging(
  MessagingFactory.build("Events", {
    type: "topic",
    fifo: true,
    contentBasedDeduplication: true,
  }),
);
```

### Fan-Out to Multiple Queues

Combine SNS with SQS for fan-out patterns:

```typescript theme={null}
import { SqsSubscription } from "aws-cdk-lib/aws-sns-subscriptions";

const orderTopic = app.addMessaging(
  MessagingFactory.build("OrderEvents", {
    type: "topic",
  }),
);

const billingQueue = app.addMessaging(
  MessagingFactory.build("Billing", {
    type: "queue",
  }),
);

const shippingQueue = app.addMessaging(
  MessagingFactory.build("Shipping", {
    type: "queue",
  }),
);

// Subscribe queues to the topic
orderTopic
  .getTopic()
  .addSubscription(new SqsSubscription(billingQueue.getQueue()));
orderTopic
  .getTopic()
  .addSubscription(new SqsSubscription(shippingQueue.getQueue()));
```

## CloudFormation Outputs

| Output          | Description |
| --------------- | ----------- |
| `{id}TopicArn`  | Topic ARN   |
| `{id}TopicName` | Topic name  |

## Next Steps

<CardGroup cols={2}>
  <Card title="Messaging Factory" icon="industry" href="/patterns/messaging-factory">
    Create messaging resources via the factory pattern
  </Card>

  <Card title="SQS Queue" icon="layer-group" href="/resources/messaging/sqs-queue">
    Combine with SQS for fan-out patterns
  </Card>
</CardGroup>
