How to Subscribe to an AWS SNS Topic?
For the sake of receiving messages that were published to a topic, you will need to first subscribe to an AWS SNS Topic. Upon subscribing an endpoint to a topic then confirming the subscription, the endpoint will start getting the messages which get published to its related topic.
AWS Management Console: Subscribing an endpoint to an AWS SNS Topic
- Login to the Amazon SNS console.
- From the navigation panel, click on the option Subscriptions.
- From the page of Subscriptions, click on the option Create subscription.
- From the opened page of Create subscription, go over the below steps:
- Fill in the Topic ARN of your previously created AWS SNS Topic, which should be similar to the following example:
arn:aws:sns:us-east-2:123456789012:MyFirstTopic
Keep in mind
For viewing the available topics in the AWS account you are currently in, click on the field named Topic ARN.
-
- In the section of Protocol, select a specific endpoint type, such as Email.
- In the section of Endpoint, fill in an email address for getting all the notifications, such as the below example:
myname@example.com
Keep in mind
Upon creating the subscription, you will have to now confirm it. Confirmation is just needed for email addresses, HTTP/S endpoints, and different account AWS resources. However, Lambda functions and SQS queues which are located in the exact AWS account and mobile endpoints, will not need any confirmation.
-
- Click on the option Create subscription.
Now you will get a subscription created and the page for this Subscription which is: 1234a567-bc89-012d-3e45-6fg7h890123i will get shown.
You will see that the ARN, Endpoint, Topic, Status (Pending confirmation) for this subscription, as well as the Protocol will be shown in the section called Details.
- From the email client, review the set email address then click on the option Confirm subscription which is found in the email from SNS.
- You will find a subscription confirmation on your web browser having your subscription ID.
AWS SDK for Java: Subscribing an endpoint to an AWS SNS topic
For the sake of subscribing an endpoint to an AWS SNS Topic through the SDK for Java, you will need to do the below:
- Set your needed credentials.
- Type your code.
Below is a code excerpt which will create a subscription for an email endpoint. After that, it will print the request ID of SubscribeRequest.
// Subscribing an email endpoint to an AWS SNS topic.
final SubscribeRequest subscribeRequest = new SubscribeRequest(topicArn, "email", "myname@example.com");
snsClient.subscribe(subscribeRequest);
// Printing the request ID for SubscribeRequest action.
System.out.println("SubscribeRequest: " + snsClient.getCachedResponseMetadata(subscribeRequest));
System.out.println("If you’d like to confirm your subscription, you will have to review your specified email.");
- Compile your code then run it.
You will find that the subscription has been created and the SubscribeRequest request ID gets printed, such as follows:
SubscribeRequest: {AWS_REQUEST_ID=1234a567-bc89-012d-3e45-6fg7h890123i}
If you’d like to confirm your subscription, you will have to review your specified email.
For subscribing an endpoint to an SNS topic through the SDK for .NET, go over the below steps:
- Set your credentials.
- Start writing your code.
Below you will find a code excerpt which is going to create a subscription for an email endpoint. Then it will print the SubscribeRequest request ID.
// Subscribing an email endpoint to an AWS SNS topic.
SubscribeRequest subscribeRequest = new SubscribeRequest(topicArn, "email", "myname@example.com");
SubscribeResponse subscribeResponse = snsClient.Subscribe(subscribeRequest);
// Printing the request ID for the SubscribeRequest action.
Console.WriteLine("SubscribeRequest: " + subscribeResponse.ResponseMetadata.RequestId);
Console.WriteLine("If you’d like to confirm your subscription, you will have to review your specified email.");
- Now, you should compile then run your code.
Finally, the subscription will be created and the SubscribeRequest request ID will get printed, as such:
SubscribeRequest: 1234a567-bc89-012d-3e45-6fg7h890123i
If you’d like to confirm your subscription, you will have to review your specified email.