Skip to main content

What Is Amazon SNS? Learn to Publish Messages to Topics

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.

Amazon SQS is a similar service, but it plays a different role. SNS is a pub/sub service that delivers a single message to multiple subscribers at once, while SQS is a queue service that temporarily stores messages. SNS has no built-in way to give each destination its own queue, so when you need to deliver a message to several consumers, a common pattern is to attach multiple SQS queues underneath an SNS topic — a fan-out architecture.

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

Message Filtering

Even when multiple subscribers are registered on a single topic, they don't all necessarily need the same messages. SNS provides subscription filter policies, which let you narrow down delivery based on message attributes.

For example, suppose a topic distributes order events and one subscriber should only receive messages where the order amount is 10,000 yen or more. You'd set a filter policy on that subscription. Messages that don't match the filter condition aren't delivered to that subscriber, and delivery to other subscribers isn't affected. Since each destination only receives the messages it actually needs, you can cut down on message-filtering logic on the receiving side.

Access Control (Topic Policies)

You can attach a JSON topic policy to a topic to control who can publish and subscribe to it. By default, only the topic's creator has access, but you can allow publishing from another AWS account, or restrict publishing to a specific AWS service.

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "s3.amazonaws.com"
},
"Action": "sns:Publish",
"Resource": "arn:aws:sns:us-east-1:123456789012:exrecord-topic"
}
]
}

For example, if you want S3 to send event notifications to an SNS topic, you need a topic policy that allows publishing from the S3 service. IAM policies alone can't control access from other AWS services, so don't forget to configure a topic policy when integrating with other services.

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.

You can retrieve a list of the topics already created in your account with the following command.

❯ aws sns list-topics
{
"Topics": [
{
"TopicArn": "arn:aws:sns:us-east-1:123456789012:exrecord-topic"
}
]
}

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.

Check Topic Attributes

Use get-topic-attributes to check a topic's current settings. It gives you a combined view of things like the attached topic policy and the number of confirmed subscriptions.

❯ aws sns get-topic-attributes --topic-arn arn:aws:sns:us-east-1:123456789012:exrecord-topic
{
"Attributes": {
"TopicArn": "arn:aws:sns:us-east-1:123456789012:exrecord-topic",
"SubscriptionsConfirmed": "1",
"SubscriptionsPending": "0",
"Policy": "{\"Version\":\"2008-10-17\", ...}"
}
}

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 completes with a single command, delivering it to all registered subscribers in real time
  • Filter policies let you deliver only the messages each destination actually needs, based on message attributes
  • Topic policies let you control publishing and subscribing from other AWS accounts or services
  • Combining SNS with SQS enables message buffering and reprocessing, helping you build more resilient architectures