0
0
AWScloud~5 mins

SNS notification types (email, SMS, Lambda) in AWS - Commands & Configuration

Choose your learning style9 modes available
Introduction
SNS lets you send messages to different places automatically. You can send emails, text messages, or trigger small programs called Lambda functions when something happens.
When you want to send an alert email to your team if a server has a problem.
When you want to send a text message to your phone for important updates.
When you want to run a small program automatically after a new file is uploaded.
When you want to notify multiple systems at once about an event.
When you want to connect your app to other AWS services easily.
Config File - sns-subscription.json
sns-subscription.json
{
  "TopicArn": "arn:aws:sns:us-east-1:123456789012:my-topic",
  "Protocol": "email",
  "Endpoint": "example@example.com"
}

This file defines a subscription to an SNS topic.

TopicArn is the unique address of the SNS topic.

Protocol is the type of notification: email, sms, or lambda.

Endpoint is where the message goes: an email address, phone number, or Lambda function ARN.

Commands
Create a new SNS topic named 'my-topic' where messages will be sent.
Terminal
aws sns create-topic --name my-topic
Expected OutputExpected
{ "TopicArn": "arn:aws:sns:us-east-1:123456789012:my-topic" }
--name - Sets the name of the SNS topic
Subscribe an email address to the topic so it receives notifications.
Terminal
aws sns subscribe --topic-arn arn:aws:sns:us-east-1:123456789012:my-topic --protocol email --notification-endpoint example@example.com
Expected OutputExpected
{ "SubscriptionArn": "pending confirmation" }
--topic-arn - Specifies which topic to subscribe to
--protocol - Defines the notification type, here email
--notification-endpoint - The email address that will receive messages
Subscribe a phone number to receive SMS text messages from the topic.
Terminal
aws sns subscribe --topic-arn arn:aws:sns:us-east-1:123456789012:my-topic --protocol sms --notification-endpoint +12345678901
Expected OutputExpected
{ "SubscriptionArn": "arn:aws:sns:us-east-1:123456789012:my-topic:2bcfbf39-05c3-41de-beaa-fcfcc21c8f55" }
--protocol - Defines the notification type, here SMS
--notification-endpoint - The phone number to receive SMS messages
Subscribe a Lambda function to the topic so it runs when a message is sent.
Terminal
aws sns subscribe --topic-arn arn:aws:sns:us-east-1:123456789012:my-topic --protocol lambda --notification-endpoint arn:aws:lambda:us-east-1:123456789012:function:my-function
Expected OutputExpected
{ "SubscriptionArn": "arn:aws:sns:us-east-1:123456789012:my-topic:7c8f3e2a-1234-5678-9abc-def012345678" }
--protocol - Defines the notification type, here Lambda
--notification-endpoint - The ARN of the Lambda function to invoke
Send a test message to all subscribers of the topic.
Terminal
aws sns publish --topic-arn arn:aws:sns:us-east-1:123456789012:my-topic --message "Hello, this is a test notification!"
Expected OutputExpected
{ "MessageId": "95df01b4-ee2e-5fb6-9b6f-6f8f9f1a1234" }
--topic-arn - Specifies the topic to send the message to
--message - The content of the notification
Key Concept

If you remember nothing else from this pattern, remember: SNS lets you send the same message to different types of receivers like email, SMS, or Lambda functions automatically.

Common Mistakes
Using the wrong protocol name like 'text' instead of 'sms' when subscribing.
SNS will reject the subscription because the protocol is invalid.
Always use the exact protocol names: 'email', 'sms', or 'lambda'.
Not confirming the email subscription by clicking the link in the confirmation email.
The email address will not receive any messages until confirmed.
Check your email and confirm the subscription to activate it.
Using a phone number without the country code for SMS subscriptions.
SNS requires phone numbers in E.164 format with country code to send SMS.
Always include the country code, e.g., +12345678901.
Summary
Create an SNS topic to group notifications.
Subscribe endpoints like email, SMS, or Lambda functions to the topic.
Publish messages to the topic to notify all subscribers automatically.