Skip to main content

What Is Amazon SNS? Learn the Basics and How to Publish Messages to a Topic

Tags:

There are many situations where you need to deliver events or notifications between applications in real time. Amazon SNS is a fully managed notification service that makes it easy to implement the fan-out pattern — sending a single message to multiple destinations at once. This article covers the core concepts of SNS and walks you through creating a topic and publishing messages using the AWS CLI.

What Is Amazon SNS

Amazon SNS (Simple Notification Service) is a fully managed pub/sub messaging service provided by AWS. When a publisher sends a message to a topic, that message is delivered to all subscribers of that topic. There's no need to manage servers or poll a message queue, making it straightforward to build event-driven architectures at low cost.

Common use cases include sending alerts from applications, delivering events between microservices, and building serverless workflows with Lambda and SQS.

Topic Types

SNS topics come in two types: Standard and FIFO. Choose based on your use case.

TypeOrderingDeduplicationThroughput
StandardNone (best-effort)None (duplicate delivery possible)Nearly unlimited
FIFOYes (delivered in send order)Yes (controlled via deduplication ID)Up to 300 msg/sec (3,000 msg/sec with batching)

Use FIFO topics when ordering or deduplication matters. For high-throughput scenarios like log collection or alert notifications, Standard topics are the better fit.

Supported Subscription Protocols

SNS supports message delivery to a wide range of destinations. You can register multiple subscriptions with different protocols on a single topic, and all of them receive the message when it's published.

ProtocolDestinationCommon Use Cases
Email / Email-JSONEmail addressHuman notifications and alerts
HTTP / HTTPSWeb endpointWebhooks to external services
SQSSQS queueAsync processing and message buffering
LambdaLambda functionServerless event processing
SMSMobile phone numberSMS notifications
ApplicationMobile push notification endpointPush notifications to iOS / Android
FirehoseAmazon Data FirehoseLog and data streaming

Try SNS with the AWS CLI

Let's walk through creating a topic, registering an email address as a subscription, and publishing a message using the AWS CLI.

Create a Topic

Start by creating a topic, which is the destination for your messages. The name you pass to --name becomes the topic name.

❯ aws sns create-topic --name exrecord-topic
{
"TopicArn": "arn:aws:sns:us-east-1:123456789012:exrecord-topic"
}

Save the topic ARN returned here — you'll need it when registering subscriptions and publishing messages.

Register a Subscription

Next, register an email address as a subscriber to the topic. Pass email to --protocol and the recipient address to --notification-endpoint.

❯ aws sns subscribe \
--topic-arn arn:aws:sns:us-east-1:123456789012:exrecord-topic \
--protocol email \
--notification-endpoint you@example.com
{
"SubscriptionArn": "pending confirmation"
}

Right after registration, the status is pending confirmation. A confirmation email will be sent to the specified address — click the "Confirm subscription" link in that email to activate the subscription.

Once confirmed, you can check the subscription status with the following command.

❯ aws sns list-subscriptions-by-topic \
--topic-arn arn:aws:sns:us-east-1:123456789012:exrecord-topic
{
"Subscriptions": [
{
"SubscriptionArn": "arn:aws:sns:us-east-1:123456789012:exrecord-topic:0a1b2c3d-4e5f-6789-abcd-0a1b2c3d4e5f",
"Owner": "123456789012",
"Protocol": "email",
"Endpoint": "you@example.com",
"TopicArn": "arn:aws:sns:us-east-1:123456789012:exrecord-topic"
}
]
}

When the SubscriptionArn shows a full ARN instead of PendingConfirmation, the subscription is confirmed.

Publish a Message

Publishing a message to the topic delivers it to all registered subscribers. Pass the message body to --message.

❯ aws sns publish \
--topic-arn arn:aws:sns:us-east-1:123456789012:exrecord-topic \
--subject "Test Notification" \
--message "This is a test message from SNS."
{
"MessageId": "0a1b2c3d-4e5f-6789-abcd-0a1b2c3d4e5f"
}

A successful publish returns a MessageId. Wait a moment and the notification should arrive at the registered email address.

Delete a Subscription

You can delete a subscription when it's no longer needed. Pass the target subscription ARN to --subscription-arn.

❯ aws sns unsubscribe \
--subscription-arn arn:aws:sns:us-east-1:123456789012:exrecord-topic:0a1b2c3d-4e5f-6789-abcd-0a1b2c3d4e5f

Delete a Topic

Delete the topic when you're done with it. Deleting a topic also removes all of its subscriptions.

❯ aws sns delete-topic \
--topic-arn arn:aws:sns:us-east-1:123456789012:exrecord-topic

Summary

This article covered the core concepts of Amazon SNS and how to create a topic, register a subscription, and publish messages using the AWS CLI.

  • SNS is a fully managed pub/sub messaging service that makes it easy to implement the fan-out pattern — delivering a single message to multiple destinations at once
  • Topics come in two types — Standard (high throughput) and FIFO (ordered delivery with deduplication) — choose based on your use case
  • You can register diverse protocols like Email, SQS, Lambda, and SMS as subscriptions, and combine multiple protocols on a single topic
  • Publishing a message with the sns publish command delivers it to all registered subscribers in real time
  • Combining SNS with SQS enables message buffering and reprocessing, helping you build more resilient architectures