HiveBrain v1.2.0
Get Started
← Back to all entries
snippetMinor

How to send SMS through AWS?

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
howsmsawsthroughsend

Problem

Can we send SMS through AWS for the below case?

I have an application installed on Ec2 and that application runs few rules and may send SMS to end users if the rule conditions are met.

I wanted to integrate it with some SMSC and thought of giving a shot with AWS-SNS. However, I am not able to figure out how will i integrate my application with AWS-SNS.

Going through the documentation of AWS-SNS I am not sure if this can be achieved. It talks about creating topics and assigning E164 numbers to topics but does not tell us how to integrate this with our already existing application.

Solution

I believe you will need to do this programmatically using the AWS SDK as described in Sending a Message (AWS SDKs). Here is a quote a from it:


To send an SMS message by using one of AWS SDKs, use the action in
that SDK that corresponds to the Publish request in the Amazon SNS
API. With this request, you can send an SMS message directly to a
phone number


...


Sending a Message (AWS SDK for Java)


The following example uses the publish method of the AmazonSNSClient
class to send a message directly to a phone number:

public static void main(String[] args) {
        AmazonSNSClient snsClient = new AmazonSNSClient();
        String message = "My SMS message";
        String phoneNumber = "+1XXX5550100";
        Map smsAttributes = 
                new HashMap();
        //
        sendSMSMessage(snsClient, message, phoneNumber, smsAttributes);
}

public static void sendSMSMessage(AmazonSNSClient snsClient, String message, 
      String phoneNumber, Map smsAttributes) {
        PublishResult result = snsClient.publish(new PublishRequest()
                        .withMessage(message)
                        .withPhoneNumber(phoneNumber)
                        .withMessageAttributes(smsAttributes));
        System.out.println(result); // Prints the message ID.

Code Snippets

public static void main(String[] args) {
        AmazonSNSClient snsClient = new AmazonSNSClient();
        String message = "My SMS message";
        String phoneNumber = "+1XXX5550100";
        Map<String, MessageAttributeValue> smsAttributes = 
                new HashMap<String, MessageAttributeValue>();
        //<set SMS attributes>
        sendSMSMessage(snsClient, message, phoneNumber, smsAttributes);
}

public static void sendSMSMessage(AmazonSNSClient snsClient, String message, 
      String phoneNumber, Map<String, MessageAttributeValue> smsAttributes) {
        PublishResult result = snsClient.publish(new PublishRequest()
                        .withMessage(message)
                        .withPhoneNumber(phoneNumber)
                        .withMessageAttributes(smsAttributes));
        System.out.println(result); // Prints the message ID.

Context

StackExchange DevOps Q#1629, answer score: 8

Revisions (0)

No revisions yet.