0
0
AWScloud~5 mins

SNS topics and subscriptions in AWS - Commands & Configuration

Choose your learning style9 modes available
Introduction
SNS helps send messages to many people or systems at once. It solves the problem of notifying multiple receivers quickly and reliably.
When you want to send alerts to multiple team members by email or SMS at the same time.
When your app needs to notify other apps or services about events instantly.
When you want to fan out messages to many subscribers without coding each connection.
When you want to decouple parts of your system so they communicate by messages.
When you want to send messages to mobile devices or HTTP endpoints automatically.
Config File - sns-topic-subscription.json
sns-topic-subscription.json
{
  "Resources": {
    "MySNSTopic": {
      "Type": "AWS::SNS::Topic",
      "Properties": {
        "TopicName": "example-topic"
      }
    },
    "MySubscription": {
      "Type": "AWS::SNS::Subscription",
      "Properties": {
        "Endpoint": "example@example.com",
        "Protocol": "email",
        "TopicArn": { "Ref": "MySNSTopic" }
      }
    }
  }
}

This JSON is an AWS CloudFormation template that creates an SNS topic named example-topic. It also creates a subscription that sends emails to example@example.com when messages are published to the topic.

MySNSTopic: Defines the SNS topic resource.

MySubscription: Defines the subscription resource linking the topic to an email endpoint.

Commands
This command creates a new SNS topic named 'example-topic' where messages can be published.
Terminal
aws sns create-topic --name example-topic
Expected OutputExpected
{"TopicArn":"arn:aws:sns:us-east-1:123456789012:example-topic"}
--name - Specifies the name of the SNS topic to create.
This command subscribes the email address 'example@example.com' to the SNS topic so it will receive messages.
Terminal
aws sns subscribe --topic-arn arn:aws:sns:us-east-1:123456789012:example-topic --protocol email --notification-endpoint example@example.com
Expected OutputExpected
{"SubscriptionArn":"pending confirmation"}
--topic-arn - Specifies the ARN of the SNS topic to subscribe to.
--protocol - Defines the communication protocol, here 'email'.
--notification-endpoint - The email address that will receive notifications.
This command lists all subscriptions for the given SNS topic to verify the subscription was created.
Terminal
aws sns list-subscriptions-by-topic --topic-arn arn:aws:sns:us-east-1:123456789012:example-topic
Expected OutputExpected
{"Subscriptions":[{"SubscriptionArn":"pending confirmation","Owner":"123456789012","Protocol":"email","Endpoint":"example@example.com","TopicArn":"arn:aws:sns:us-east-1:123456789012:example-topic"}]}
--topic-arn - Specifies the SNS topic to list subscriptions for.
Key Concept

If you remember nothing else from this pattern, remember: SNS topics let you send messages to many subscribers easily by creating a topic and adding subscriptions.

Common Mistakes
Not confirming the subscription email after subscribing.
The subscription stays in 'pending confirmation' and no messages are received.
Check your email and click the confirmation link to activate the subscription.
Using the wrong ARN or topic name when subscribing.
The subscription command fails or subscribes to the wrong topic.
Copy the exact TopicArn from the create-topic output and use it in the subscribe command.
Trying to subscribe with an unsupported protocol or invalid endpoint.
Subscription creation fails or messages are not delivered.
Use supported protocols like email, sms, http, https, and valid endpoints.
Summary
Create an SNS topic to group messages for multiple receivers.
Subscribe endpoints like email addresses to the topic to receive messages.
Verify subscriptions and confirm them to start receiving notifications.