Accessing AWS SNS from applications

AWS SNS helps you send e-mail, SMS, and mobile push notifications very efficiently. The subscribers of the messages receive the messages over one of the supported protocols such as HTTP/S, SMS, e-mail, and Amazon SQS.

If you are targeting an HTTP(S) endpoint from SNS, then it is highly recommended that your HTTP(S) application be highly available to avoid message drops. For reliable messaging, you can store the notification messages to Amazon SQS.

Mobile push notifications are typically used for application alerts, push e-mails and SMS, and mobile push notifications. A use case for mobile push notifications could be to prompt your inactive users back to using your application again. Targeting mobile platforms for mobile notifications requires integration with different libraries for different platforms. AWS SNS provides a single interface for all these platforms. You send a single push message to SNS, and then SNS sends this message to different platforms, such as the, iOS, Android, and Windows phones.

A topic is the communication channel between publishers and subscribers. Publishers send messages to a topic and subscribers of those messages subscribe to the same topic. Whenever the publisher publishes a message to a topic, AWS SNS delivers the message to all the subscribers of that specific topic.

How to do it…

  1. Installing AWS Java SDK.

    In your Maven dependency section, add the following dependency for AWS Java SDK Version 1.9.28.1:

    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk</artifactId>
        <version>1.9.28.1</version>
    </dependency>
  2. Create a topic.

    The following sample program creates a topic called emailalerts. Replace endpoint with your own value. Record topic ARN for further usage.

         // Create topic.
             public static void CreateTopic() {
    
            // Create BasicAWSCredentials with Access Key Id and Secret Access Key.
            BasicAWSCredentials credentials = new BasicAWSCredentials(
                    "Access Key Id",
                    "Secret Access Key");
    
            // Create SNS client.
            AmazonSNSClient snsClient = new AmazonSNSClient(credentials);
    
            // Set endpoint.
            snsClient.setEndpoint("sns.ap-southeast- 1.amazonaws.com");
    
            // Create topic.
            CreateTopicResult result = snsClient.createTopic("emailalerts");
    
            // Get the topic Arn.
            String topicArn = result.getTopicArn();
        }
  3. Subscribe to a topic.

    In the following sample program, we subscribe to the topic with name emailalerts using the e-mail protocol.

        // Subscribe to a topic.
             public static void Subscribe() {
    
            // Create BasicAWSCredentials with Access Key Id and Secret Access Key.
            BasicAWSCredentials credentials = new BasicAWSCredentials(
                    "Access Key Id",
                    "Secret Access Key");
    
            // Create SNS client.
            AmazonSNSClient snsClient = new AmazonSNSClient(credentials);
    
            // Set endpoint.
            snsClient.setEndpoint("sns.ap-southeast- 1.amazonaws.com");
    
            // Subscribe to a topic by email protocol.
            snsClient.subscribe(
                    "arn:aws:sns:ap-southeast- 1:968336292411:emailalerts", "email",
                    "[email protected] ");
        }
  4. Confirm the subscription.

    The user receives a confirmation e-mail from AWS and has to click on the link contained in the message.

  5. Publish to a topic.

    The following sample program publishes the message to the topic called emailalerts:

        // Publish to a topic.
             public static void Publish() {
    
            // Create BasicAWSCredentials with Access Key Id and Secret Access Key.
            BasicAWSCredentials credentials = new BasicAWSCredentials(
                    "Access Key Id",
                    "Secret Access Key");
    
            // Create SNS client.
            AmazonSNSClient snsClient = new AmazonSNSClient(credentials);
    
            // Set endpoint.
            snsClient.setEndpoint("sns.ap-southeast- 1.amazonaws.com");
    
            // Publish to a topic.
            snsClient.publish(
                    "arn:aws:sns:ap-southeast- 1:968336292411:emailalerts",
                    "This is test message.");
        }

How it works…

First, we install the AWS SDK, as the AWS Java SDK is required to access the AWS services from Java applications.

As AWS SNS topic acts as a communication point for publishers and subscribers to communicate with each other, we first create a topic called emailalerts. Copy the ARN topic for use in the subsequent steps.

In the next step, we subscribe an endpoint to our topic. We configure the subscription to send the topic messages to our e-mail address.

At this stage, we receive a confirmation e-mail from AWS. The following screenshot shows a sample confirmation e-mail from AWS:

How it works…

After the user clicks on the Confirm subscription link, he/she will see a subscription confirmed message.

How it works…

After the confirmation of the subscription, he/she will start getting e-mails from SNS when a new message is published to the topic.

Next, we publish a message to our topic. Here, we publish a message to our e-mail address as defined in a previous step. At this stage, you can access the message in your e-mail application.

There's more…

You can define access control policies to restrict access to specific producers and consumers for a given topic. SNS policies also support cross-account access.

Note

Each SNS policy must cover only a single topic or queue. Also, each policy must have a unique policy ID, and each statement within the policy must have a unique statement ID.

Amazon SNS also integrates with IAM, so you can specify which SNS-related actions a user is permitted to perform. Hence, you can use IAM policies and/or SNS policies to for setting up your user permissions.

Mobile push notifications are common use case for Amazon SNS. You can send push notifications to mobile devices using one of the supported push notification services. These services include Apple Push Notification Service (APNS) for iOS, Google Cloud Messaging (GCM) for Android, and Microsoft Push Notification Service (MPNS) for Windows Phone.

You can use Amazon SNS with Amazon SQS to store messages to a queue where it can be processed by other application components, asynchronously. You can subscribe a SQS queue to an SNS topic so that when your application publishes a message to the topic, SNS sends an SQS message to the queue.

You can use Amazon SNS to send and receive SMS notifications to mobile phones. A typical use case for this is to send SMS alerts. For example, you can associate a CloudWatch alarm to an SNS topic to send SMS notifications.

In the case of sending notifications to HTTP(S) endpoints, when you publish a notification to the topic, Amazon SNS sends an HTTP POST request to the subscribed endpoint. You have to ensure that your application is ready to handle these HTTP(S) POST requests. Your application should read the HTTP header for messages. There are two key message types that need to be handled—the subscription confirmation and the notifications. For the HTTPS endpoint, you must have a server certificate signed by a trusted Certificate Authority (CA) that Amazon SNS recognizes.

Note

Refer to SNS Developers Guide for a list of CAs recognized by Amazon SNS for HTTPS endpoints.

In addition, Amazon SNS is integrated with CloudWatch, so you can collect and analyze metrics for every SNS topic. You can also create alarms and send notifications if the threshold is exceeded for a specific SNS metric, for example, the number of messages failed notifications. API calls to SNS can be tracked via CloudTrail Logs.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset