1
votes

I want Amazon SNS to send SMS to cell phones. I checked the AWS document, it says

SMS notifications are currently supported for phone numbers in the     
United States. SMS messages can be sent only from topics created in the 
US East (N. Virginia) region. However, you can publish messages to 
topics that you create in the US East (N. Virginia) region from any 
other region.

But I don't know how to publish a message to the topics that I create in the US East region from US West which my aws nodes are located.

Googled a lot, but without luck. Does anybody here know how?

2

2 Answers

2
votes

It doesn't matter where your code is running... even somewhere not part of AWS. Most services, like SNS, have a regional endpoint That you need to access, in order to connect with resources defined in that region. That's what the AWS console is doing, behind the scenes.

If you are using an SDK or another library, rather than using your own code to directly communicate with the AWS services, then what you're looking for is how to change or set the "region" or "endpoint" with the request or (likely) in the constructor.

You can only send requests "from any other region" in same sense that you can send requests from the Internet, generally. You don't send them through the other region's SNS endpoint. You connect directly to the target region.

Note, though, that the SNS SMS solution is of somewhat limited utility -- useful for notifications to groups of individuals who are expecting your notifications ("The nightly global batch process has completed, but errors were encountered.") but not so much for customer contact ("Your order has been shipped.") since the topic subscribers are not individually addressable... send to one, send to all... all who responded positively to the subscription confirmation message, that is.

0
votes

Here's a sample function that uses the Node.js SDK to send an SMS message from an AWS lambda function:

module.exports.sendMessage = (event, context, cb) => {
  const AWS = require('aws-sdk');
  const sns = new AWS.SNS({ region: 'us-east-1' });
  const params = {
    Message: 'The text message body.',
    PhoneNumber: '+15555555555',
  };
  sns.publish(params, function (err, data) {
    if (err) {
      cb(null, { message: 'Error: ' + err, event });
    } else {
      // data has the following form:
      // var data = {
      //   ResponseMetadata: { RequestId: '12345678-1234-5678-1234-012345678901' },
      //   MessageId: '12345678-1234-5678-1234-012345678901'
      // };
      cb(null, { message: data.MessageId, event });
    }
  });
}

You can read more about it here or adapt it to use another SDK according to your architecture. Don't forget to setup the correct IAM permissions to allow for sending SNS messages for whatever will be running this.