How To Confirm the Subscription Request HTTP from Amazon SNS: A Step-by-step Guide

How To Confirm the Subscription Request HTTP from Amazon SNS: A Step-by-step Guide
As a data scientist or software engineer working with AWS, Amazon Simple Notification Service (SNS) is a powerful tool for managing notifications and messages across distributed systems. One of the most common initial tasks is confirming a subscription request, which is crucial for ensuring you’re ready to receive notifications. This article will explain how to confirm a subscription request HTTP from Amazon SNS.
What is Amazon SNS?
Amazon SNS is a fully managed publish/subscribe messaging service that manages the delivery of messages to subscribing endpoints or clients. The process starts when you create a topic and subscribe an endpoint to it. When a message is published to the topic, Amazon SNS sends an HTTP POST request with a confirmation message to the subscribed endpoint.
Note: Amazon SNS supports several protocols for subscription endpoints including HTTP/S, email, SMS, Lambda, and SQS.
How does the Subscription Confirmation Process Work?
When you subscribe an HTTP/HTTPS endpoint to a topic, Amazon SNS sends a subscription confirmation message to the endpoint. This message includes a URL that you must visit to confirm the subscription.
Here is a step-by-step guide to confirm the subscription request.
Step 1: Receive the Confirmation Message
The confirmation message from Amazon SNS will be a POST request with a JSON payload. The JSON will look something like this:
{
"Type" : "SubscriptionConfirmation",
"MessageId" : "165545c9-2a5c-472c-8df2-7ff2be2b3b1b",
"Token" : "2336412f37...",
"TopicArn" : "arn:aws:sns:us-west-2:123456789012:MyTopic",
"Message" : "You have chosen to subscribe to the topic arn:aws:sns:us-west-2:123456789012:MyTopic...\nTo confirm the subscription, visit the SubscribeURL included in this message.",
"SubscribeURL" : "https://sns.us-west-2.amazonaws.com/?Action=ConfirmSubscription&TopicArn=arn:aws:sns:us-west-2:123456789012:MyTopic&Token=2336412f37...",
"Timestamp" : "2012-04-26T20:45:04.751Z",
"SignatureVersion" : "1",
"Signature" : "EXAMPLEpH+DcEwjAPg8O9mY8dReBSwksfg2S7WKQcikcNKWLQjwu6A4VbeS0Qz4to=+",
"SigningCertURL" : "https://sns.us-west-2.amazonaws.com/SimpleNotificationService-0000000000000000000000.pem"
}
Step 2: Confirm the Subscription
To confirm the subscription, your application needs to GET the SubscribeURL
from the JSON payload. You can do this with any HTTP client library. Here is an example using Python’s requests
library:
import requests
def confirm_subscription(data):
if data['Type'] == 'SubscriptionConfirmation':
requests.get(data['SubscribeURL'])
Step 3: Validate the Message Signature
For security reasons, you should validate the message signature before confirming the subscription. The confirmation message includes the signature, the certificate, and the signed data. You can use these to verify the message came from Amazon SNS.
Here is an example of how to do this in Python:
import boto3
from botocore.exceptions import IncompleteSignatureError
sns_client = boto3.client('sns')
def validate_message(data):
try:
sns_client.verify_message(
Message=data,
MessageSignature=data['Signature'],
SigningCertURL=data['SigningCertURL']
)
return True
except IncompleteSignatureError:
return False
In conclusion, confirming subscription requests from Amazon SNS involves receiving the confirmation message, validating the message signature, and visiting the SubscribeURL
. By following these steps, you can ensure that your application is ready to receive notifications from Amazon SNS.
About Saturn Cloud
Saturn Cloud is your all-in-one solution for data science & ML development, deployment, and data pipelines in the cloud. Spin up a notebook with 4TB of RAM, add a GPU, connect to a distributed cluster of workers, and more. Join today and get 150 hours of free compute per month.