AWS SNS (Simple Notification Service) Tutorial

AWS SNS (Simple Notification Service) Tutorial
1. Creating and Managing an SNS Topic
Example 1: Create an SNS Topic
aws sns create-topic --name MySNSTopic
Explanation
Creates a new SNS topic for messaging.
aws sns create-topic
Requires a unique topic name (
MySNSTopic
).--name MySNSTopic
SNS topics allow multiple subscribers (email, SMS, Lambda, SQS).
create-topic
Useful for event-driven architectures and notifications.
create-topic
Example 2: List All SNS Topics
aws sns list-topics
Explanation
Retrieves all SNS topics in an AWS account.
aws sns list-topics
Displays topic ARNs (Amazon Resource Names).
list-topics
Useful for managing multiple SNS notification channels.
list-topics
Can be used to verify topic creation and access.
list-topics
Example 3: Get SNS Topic Attributes
aws sns get-topic-attributes --topic-arn arn:aws:sns:us-east-1:123456789012:MySNSTopic
Explanation
Retrieves metadata about a specific SNS topic.
aws sns get-topic-attributes
Requires a valid SNS topic ARN.
--topic-arn arn:aws:sns:us-east-1:123456789012:MySNSTopic
Shows policies, delivery status, and subscriptions.
get-topic-attributes
Useful for debugging and verifying configurations.
get-topic-attributes
Example 4: Delete an SNS Topic
aws sns delete-topic --topic-arn arn:aws:sns:us-east-1:123456789012:MySNSTopic
Explanation
Removes an SNS topic permanently.
aws sns delete-topic
Requires a topic ARN to prevent accidental deletion.
--topic-arn arn:aws:sns:us-east-1:123456789012:MySNSTopic
Deletes all associated subscriptions automatically.
delete-topic
Cannot be undone once deleted.
delete-topic
2. Subscribing to an SNS Topic
Example 1: Subscribe an Email Address
aws sns subscribe --topic-arn arn:aws:sns:us-east-1:123456789012:MySNSTopic --protocol email --notification-endpoint user@example.com
Explanation
Adds an email address as an SNS subscriber.
aws sns subscribe
Requires the SNS topic ARN for subscription.
--topic-arn arn:aws:sns:us-east-1:123456789012:MySNSTopic
Uses
email
as the subscription protocol.--protocol email
Sends a confirmation request before enabling notifications.
--notification-endpoint
user@example.com
Example 2: Subscribe an SQS Queue
aws sns subscribe --topic-arn arn:aws:sns:us-east-1:123456789012:MySNSTopic --protocol sqs --notification-endpoint arn:aws:sqs:us-east-1:123456789012:MyQueue
Explanation
Links an SQS queue to receive SNS messages.
aws sns subscribe
Requires an existing SNS topic ARN.
--topic-arn arn:aws:sns:us-east-1:123456789012:MySNSTopic
Uses
sqs
as the protocol for queue-based messaging.--protocol sqs
Messages are pushed automatically to the SQS queue.
--notification-endpoint arn:aws:sqs:us-east-1:123456789012:MyQueue
Example 3: List All Subscriptions
aws sns list-subscriptions
Explanation
Retrieves all SNS subscriptions across topics.
aws sns list-subscriptions
Shows subscriber protocols (email, SQS, Lambda, HTTP).
list-subscriptions
Lists pending and confirmed subscriptions.
list-subscriptions
Useful for auditing and managing notification services.
list-subscriptions
Example 4: Unsubscribe from an SNS Topic
aws sns unsubscribe --subscription-arn arn:aws:sns:us-east-1:123456789012:MySubscription
Explanation
Removes a subscriber from an SNS topic.
aws sns unsubscribe
Requires a valid subscription ARN.
--subscription-arn arn:aws:sns:us-east-1:123456789012:MySubscription
Prevents further notifications to the subscriber.
unsubscribe
Useful when deprecating or migrating notification systems.
unsubscribe
3. Publishing Messages to an SNS Topic
Example 1: Send a Simple Notification
aws sns publish --topic-arn arn:aws:sns:us-east-1:123456789012:MySNSTopic --message "Hello from AWS SNS!"
Explanation
Sends a message to all SNS subscribers.
aws sns publish
Requires a valid SNS topic ARN.
--topic-arn arn:aws:sns:us-east-1:123456789012:MySNSTopic
Message content must be specified.
--message "Hello from AWS SNS!"
All subscribers receive the message in their respective channels.
publish
Example 2: Send a JSON-Formatted Message
aws sns publish --topic-arn arn:aws:sns:us-east-1:123456789012:MySNSTopic --message '{"default": "Hello!", "email": "Email message", "sqs": "Queue message"}' --message-structure json
Explanation
Sends a structured message with different formats.
aws sns publish
JSON allows different messages per protocol (email, SQS, Lambda).
--message-structure json
Customizes notifications based on the destination.
--message '{"default": "Hello!", "email": "Email message", "sqs": "Queue message"}'
Useful for multi-channel messaging and alerts.
publish
Example 3: Send a Message to a Specific Phone Number
aws sns publish --phone-number "+1234567890" --message "Your verification code is 123456"
Explanation
Sends an SMS notification directly to a phone.
aws sns publish
Uses
--phone-number
instead of--topic-arn
.--phone-number "+1234567890"
Message content must be kept short for SMS.
--message "Your verification code is 123456"
Useful for OTPs, alerts, and customer notifications.
publish
Example 4: Publish a Message with Attributes
aws sns publish --topic-arn arn:aws:sns:us-east-1:123456789012:MySNSTopic --message "High priority alert!" --message-attributes '{"Priority":{"DataType":"String","StringValue":"High"}}'
Explanation
Adds metadata to an SNS message.
aws sns publish
message-attributes
allows tagging messages with properties.--message-attributes '{"Priority":{"DataType":"String","StringValue":"High"}}'
Useful for filtering messages in subscribers.
"Priority":"High"
Enables prioritization in event-driven workflows.
publish
Subscribe to my newsletter
Read articles from user1272047 directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
