Pub/Sub Messaging with
Amazon Simple Notification
Service (SNS)
Task 1: Subscribe to an Amazon SNS Topic
Subscribing to an Amazon SNS (Simple Notification Service) topic allows an
endpoint (e.g., email, SMS, Lambda function) to receive notifications when
messages are published to the topic. Here’s how to do it:
Step 1: Prerequisites
1. Existing SNS Topic:
Ensure you have an SNS topic created. If not, create one:
bash
Copy
aws sns create-topic --name MyTopic
Note the TopicArn from the output.
2. Valid Endpoint:
Prepare the endpoint you want to subscribe (e.g., email address, phone
number, Lambda ARN, or HTTP/HTTPS URL).
Step 2: Create the Subscription
Use the AWS Management Console, CLI, or SDK to subscribe your endpoint to
the topic.
Using AWS CLI
Syntax:
bash
Copy
, aws sns subscribe \
--topic-arn <TOPIC_ARN> \
--protocol <PROTOCOL> \
--notification-endpoint <ENDPOINT>
Examples:
Email:
bash
Copy
aws sns subscribe \
--topic-arn arn:aws:sns:us-east-1:123456789012:MyTopic \
--protocol email \
--notification-endpoint
SMS:
bash
Copy
aws sns subscribe \
--topic-arn arn:aws:sns:us-east-1:123456789012:MyTopic \
--protocol sms \
--notification-endpoint +1234567890
Lambda Function:
bash
Copy
aws sns subscribe \
--topic-arn arn:aws:sns:us-east-1:123456789012:MyTopic \
--protocol lambda \
--notification-endpoint arn:aws:lambda:us-east-
1:123456789012:function:MyFunction
HTTP/HTTPS Endpoint: