Home » Nodejs » Getting delivery status of AWS SMS

Getting delivery status of AWS SMS

Posted by: admin November 30, 2017 Leave a comment

Questions:

I’m sending sms from AWS through the node SDK. SMS are going out well and I’m trying to get delivery informations. Apparently it’s not that easy and one has to setup SNS to send logs to Cloudwatch and to parse CloudWatch to get the delivery information looking up the MessageId: https://stackoverflow.com/a/40327061/2054629

If I send sms through SNS web interface, logs I see logs in cloudwatch, but not when I send them through the node SDK. I could not get information on how to setup things before sending them from node.

Ideally, I want to achieve something like:

const sendSMS = async (message, number) => {
    // send the SMS
    // wait to get delivery info
    // resolve with delivery info, or reject if failed
}

Currently I have:

import AWS from 'aws-sdk';

AWS.config.update({
  accessKeyId: accessKey,
  secretAccessKey: secretKey,
  region: 'us-east-1',
});

const sns = new AWS.SNS();

const sendSMS = async (message, number) => {
  return await new Promise((resolve, reject) => {
    sns.publish({
      Message: message,
      MessageStructure: 'string',
      PhoneNumber: number,
    }, (err, res) => {
      if (err) { return reject(err); }
      resolve(res);
    });  
  });
}

which only send a SMS request to AWS and resolves with something like

{
  ResponseMetadata: { RequestId: '7e0999a3-xxxx-xxxx-xxxx-xxxxxxxxxxxx' },
  MessageId: 'f7f21871-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
}

I’m not sure if one has to setup an SNS application to be able to get logs or not, and I’d rather not to keep things simple.

Answers:

Your code seems to work just fine with nodejs v6 by changing the import statement (lack of ES5/6 support). After enabling logging to cloudwatch, every SMS (both through the Web interface and this code) creates a log steam in CloudWatch logs. I think you should reinstall the AWS SDK or avoid using ES5/6 to make the SDK work correctly.

For the second question, if the message wasn’t delivered, you would get an error:

(err, res) => {
      if (err) { return reject(err); }
      resolve(res);
    }); 

if the message was successfully sent, you get a response like:

{ ResponseMetadata: { RequestId: 'e31feda6-669c-5b13-XXX-bc25b07877b5' },
  MessageId: '53555115-6acb-5684-XXXX-0096bc2f6a22' }

Questions:
Answers:

You might have already done this but to configure cloudwatch logs for SMS deliveries, you have to configure SMS preferences. For that you need to create an IAM role to allow cloudwatch logs access. It is very simple to do it through AWS console. The steps are given at http://docs.aws.amazon.com/sns/latest/dg/sms_preferences.html

You can even control what percentage of successful deliveries + failed SMSs are logged if you want. Once this is done, you should start seeing cloudwatch logs whichever way you sent the SMS.

I wanted to add this as a comment but I don’t have enough rep. I’ll delete this answer if it doesn’t work.