SNS Topics



An SNS topic provides high-throughput, push-based, many-to-many messaging. It's a managed service that allows you to send messages to a large number of subscribers, including distributed systems, microservices, and serverless applications. You can use SNS for both application-to-application (A2A) and application-to-person (A2P) communication.

Under the hood, Stacktape uses Amazon Simple Notification Service (SNS).

When to use them

Stacktape offers three managed resources for messaging and communication:

  1. SNS topics
  2. SQS queues
  3. Event buses

Use an SNS topic when:

  • You need to publish messages to many different subscribers with a single action.
  • You require high throughput and reliability.

Use an SQS queue when:

  • You need reliable, one-to-one, asynchronous communication to decouple your applications.
  • You want to control the rate at which messages are consumed.

Use an event bus when:

  • You want to publish messages to many subscribers and use the event data itself to filter which messages are delivered to which subscribers.
  • You need to integrate with third-party SaaS providers like Shopify, Datadog, or PagerDuty.

Basic usage

You don't need to specify any properties to create a basic SNS topic.

resources:
  mainTopic:
    type: sns-topic
SnsTopicAPI reference
typeRequired
No description

Type: string "sns-topic"

properties.smsDisplayName

Sender name shown on SMS messages sent to subscribers (e.g., "MyApp"). Max 11 characters.

Type: string

properties.fifoEnabled

Guarantees message order and exactly-once delivery. Use for financial transactions, sequential workflows.

Type: boolean

FIFO topics can only deliver to FIFO SQS queues (not email, SMS, or HTTP). Requires either contentBasedDeduplication: true or a unique MessageDeduplicationId per message.

properties.contentBasedDeduplication

Automatically deduplicates messages based on content (SHA-256 hash) within a 5-minute window.

Type: boolean

Saves you from generating a unique deduplication ID for each message. Requires fifoEnabled: true.

overrides

Escape hatch to modify the underlying CloudFormation resources Stacktape creates.

Type: Object

Use dot-notation paths to override specific properties on any child resource. Find resource logical IDs with stacktape stack-info --detailed.

overrides:
  MyDbInstance:
    Properties.StorageEncrypted: true

Integrating with workloads

You can trigger a function or a batch job whenever a message is published to an SNS topic.

resources:
  myLambda:
    type: function
    properties:
      packaging:
        type: stacktape-lambda-buildpack
        properties:
          entryfilePath: path/to/my-lambda.ts
      events:
        - type: sns
          properties:
            snsTopicName: mySnsTopic

  mySnsTopic:
    type: sns-topic

A Lambda function with an SNS topic integration.

Publishing messages

You can use the AWS SDK to publish messages to a topic.

import { PublishCommand, SNSClient } from "@aws-sdk/client-sns";

const snsClient = new SNSClient({});

snsClient.send(
  new PublishCommand({
    Message: "Hello from the other side.",
    TopicArn: process.env.STP_MY_TOPIC_ARN,
  })
);

FIFO

You can enable FIFO (First-In, First-Out) to ensure that messages are processed in the exact order that they are sent.

FIFO topics can only deliver to FIFO SQS queues (not email, SMS, or HTTP). Requires either contentBasedDeduplication: true or a unique MessageDeduplicationId per message.

resources:
  mainTopic:
    type: sns-topic
    properties:
      fifoEnabled: true

Content-based deduplication

You can enable content-based deduplication to prevent duplicate messages from being sent.

Saves you from generating a unique deduplication ID for each message. Requires fifoEnabled: true.