AWS SNS (Simple Notification Service) Tutorial

user1272047user1272047
4 min read

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

  1. Creates a new SNS topic for messaging.

    • aws sns create-topic
  2. Requires a unique topic name (MySNSTopic).

    • --name MySNSTopic
  3. SNS topics allow multiple subscribers (email, SMS, Lambda, SQS).

    • create-topic
  4. Useful for event-driven architectures and notifications.

    • create-topic

Example 2: List All SNS Topics

aws sns list-topics

Explanation

  1. Retrieves all SNS topics in an AWS account.

    • aws sns list-topics
  2. Displays topic ARNs (Amazon Resource Names).

    • list-topics
  3. Useful for managing multiple SNS notification channels.

    • list-topics
  4. 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

  1. Retrieves metadata about a specific SNS topic.

    • aws sns get-topic-attributes
  2. Requires a valid SNS topic ARN.

    • --topic-arn arn:aws:sns:us-east-1:123456789012:MySNSTopic
  3. Shows policies, delivery status, and subscriptions.

    • get-topic-attributes
  4. 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

  1. Removes an SNS topic permanently.

    • aws sns delete-topic
  2. Requires a topic ARN to prevent accidental deletion.

    • --topic-arn arn:aws:sns:us-east-1:123456789012:MySNSTopic
  3. Deletes all associated subscriptions automatically.

    • delete-topic
  4. 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

  1. Adds an email address as an SNS subscriber.

    • aws sns subscribe
  2. Requires the SNS topic ARN for subscription.

    • --topic-arn arn:aws:sns:us-east-1:123456789012:MySNSTopic
  3. Uses email as the subscription protocol.

    • --protocol email
  4. Sends a confirmation request before enabling notifications.


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

  1. Links an SQS queue to receive SNS messages.

    • aws sns subscribe
  2. Requires an existing SNS topic ARN.

    • --topic-arn arn:aws:sns:us-east-1:123456789012:MySNSTopic
  3. Uses sqs as the protocol for queue-based messaging.

    • --protocol sqs
  4. 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

  1. Retrieves all SNS subscriptions across topics.

    • aws sns list-subscriptions
  2. Shows subscriber protocols (email, SQS, Lambda, HTTP).

    • list-subscriptions
  3. Lists pending and confirmed subscriptions.

    • list-subscriptions
  4. 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

  1. Removes a subscriber from an SNS topic.

    • aws sns unsubscribe
  2. Requires a valid subscription ARN.

    • --subscription-arn arn:aws:sns:us-east-1:123456789012:MySubscription
  3. Prevents further notifications to the subscriber.

    • unsubscribe
  4. 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

  1. Sends a message to all SNS subscribers.

    • aws sns publish
  2. Requires a valid SNS topic ARN.

    • --topic-arn arn:aws:sns:us-east-1:123456789012:MySNSTopic
  3. Message content must be specified.

    • --message "Hello from AWS SNS!"
  4. 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

  1. Sends a structured message with different formats.

    • aws sns publish
  2. JSON allows different messages per protocol (email, SQS, Lambda).

    • --message-structure json
  3. Customizes notifications based on the destination.

    • --message '{"default": "Hello!", "email": "Email message", "sqs": "Queue message"}'
  4. 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

  1. Sends an SMS notification directly to a phone.

    • aws sns publish
  2. Uses --phone-number instead of --topic-arn.

    • --phone-number "+1234567890"
  3. Message content must be kept short for SMS.

    • --message "Your verification code is 123456"
  4. 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

  1. Adds metadata to an SNS message.

    • aws sns publish
  2. message-attributes allows tagging messages with properties.

    • --message-attributes '{"Priority":{"DataType":"String","StringValue":"High"}}'
  3. Useful for filtering messages in subscribers.

    • "Priority":"High"
  4. Enables prioritization in event-driven workflows.

    • publish

0
Subscribe to my newsletter

Read articles from user1272047 directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

user1272047
user1272047