Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send SMS using Amazon SNS from a AWS lambda function

Amazon SNS provides a facility to send SMS globally.

I want to send SMS from a Lambda function were we provide the mobile number and text message and use SNS to deliver that message but I didn't find a helpful documentation or example code for NodeJS or java.

Can any one suggest a solution?

Code:

  var params = {
  Message: 'Hi this is message from AWS_SNS', /* required */
  MessageAttributes: {
    someKey: {
      DataType: 'String' ,
      StringValue: 'String'
    },
      },
  MessageStructure: 'String',
  PhoneNumber: '+91MyNUMBER',
  Subject: 'MYSubject',
  //TargetArn: 'arn:aws:sns:us-west-2:798298080689:SMS',
  //TopicArn: 'arn:aws:sqs:us-west-2:798298080689:SendSMS'
};
sns.publish(params, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);           // successful response
});
};
like image 427
Nag Avatar asked Jan 29 '26 14:01

Nag


2 Answers

So, you need to write Lambda function which is invoked somehow, let's say via HTTP request so you'll also need to setup API Gateway to route connections to your Lambda function.

Next, your Lambda function will push that data to "SNS Topic" while SMS Subscription will "poll" for any new data in this "Topic". As soon as any data gets into this topic, it will be consumed by subscription and SMS will be sent.

Few days ago I wrote a post about SNS & Lambda which might help you. Flow you wanted to achieve is pretty similar to one described in this article.

https://medium.com/@rafalwilinski/use-aws-lambda-sns-and-node-js-to-automatically-deploy-your-static-site-from-github-to-s3-9e0987a073ec#.3x6wbrz91

Documentation pages that might help:

  • Pushing to SNS: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SNS.html#publish-property
  • Subscribing to SNS: http://docs.aws.amazon.com/sns/latest/dg/SubscribeTopic.html
like image 63
Rafal Wiliński Avatar answered Jan 31 '26 07:01

Rafal Wiliński


Please try with setting the region explicitly to "us-east-1". I managed to send SMS to India by explicitly setting this region. I also tried with "ap-south-1", but was not successful.

like image 39
user5398398 Avatar answered Jan 31 '26 06:01

user5398398



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!