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.
- FIFO (First-In-First-Out) topics have all the capabilities of the standard topics, but are designed to enhance messaging between applications when the order of operations and events is critical, or where duplicates can't be tolerated.
- For more information on FIFO topics refer to AWS docs
When using FIFO, each message should contain
MessageDeduplicationId
or contentBasedDeduplication must be enabled.
resources:mainTopic:type: sns-topicproperties:fifoEnabled: true
Content-based deduplication
You can enable content-based deduplication to prevent duplicate messages from being sent.
Deduplication explained:
- During the deduplication interval(five-minute), topic treats messages identifies duplicates and delivers only one copy of the message
- Duplicate messages are recognized based on
MessageDeduplicationId
:- You may provide a
MessageDeduplicationId
explicitly. - If you do not provide
MessageDeduplicationId
, content-based deduplication is used(if enabled) - this means Amazon SNS uses a SHA-256 hash to generate theMessageDeduplicationId
using the body of the message (but not the attributes of the message).FIFO must be enabled in order to use this feature
- You may provide a