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

  1. Login to the Amazon SNS console.
    AWS SNS Topic - SNS Dashboard

    AWS SNS Topic – SNS Dashboard

     

  2. From the navigation panel, click on the option Subscriptions.
    AWS SNS Topic - Subscriptions

    AWS SNS Topic – Subscriptions

     

  3. From the page of Subscriptions, click on the option Create subscription.
    AWS SNS Topic - Create Subscription

    AWS SNS Topic – Create Subscription

     

  4. From the opened page of Create subscription, go over the below steps:
    1. 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

AWS SNS Topic - Topic

AWS SNS Topic – Topic

 

Keep in mind

For viewing the available topics in the AWS account you are currently in, click on the field named Topic ARN.

AWS SNS Topic - Protocol and Endpoint

AWS SNS Topic – Protocol and Endpoint

    1. In the section of Protocol, select a specific endpoint type, such as Email.
    2. 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.

    1. Click on the option Create subscription.

      AWS SNS Topic - Policy

      AWS SNS Topic – Policy

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 ARNEndpointTopicStatus (Pending confirmation) for this subscription, as well as the Protocol will be shown in the section called Details.

  1. From the email client, review the set email address then click on the option Confirm subscription which is found in the email from SNS.
  2. 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:

  1. Set your needed credentials.
  2. 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.");
  1. 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:

  1. Set your credentials.
  2. 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.");
  1. 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.

How to Manage AWS Budget


AUTHOR