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:
- SNS topics
- SQS queues
- 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
Integrating with workloads
You can trigger a function or a batch job whenever a message is published to an SNS topic.
resources:myLambda:type: functionproperties:packaging:type: stacktape-lambda-buildpackproperties:entryfilePath: path/to/my-lambda.tsevents:- type: snsproperties:snsTopicName: mySnsTopicmySnsTopic: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.
Enables FIFO (First-In-First-Out) for this topic.
FIFO topics ensure that messages are processed in the exact order they are sent and that no duplicate messages are delivered. This is useful for applications where order and uniqueness are critical, such as financial transactions or inventory updates.
When fifoEnabled is true, either contentBasedDeduplication must be enabled, or every message must include a unique MessageDeduplicationId.
For more details, refer to the AWS documentation on FIFO topics.
resources:mainTopic:type: sns-topicproperties:fifoEnabled: true
Content-based deduplication
You can enable content-based deduplication to prevent duplicate messages from being sent.
Enables content-based deduplication. This helps prevent sending duplicate messages.
SNS will automatically generate a unique ID based on the message content and use it to detect and discard duplicate messages sent within a 5-minute interval.
This property can only be used when fifoEnabled is true.