Use Amazon SNS and Lambda to send SMS using NodeJS

41

In this post we are going to show how to combine two of the most amazing products of Amazon AWS (Lambda and SNS) with our (also amazing, of course) API and NodeJS SDK.

By using an SNS topic you will be able to quickly and easily send SMS from your application, but also combine it with other subscriptions in the same topic.

We have made the example code open source and available in our GitHub account, at https://github.com/PortaText/sns-lambda-example, we hope you enjoy it!

One of the great power of using SNS is that you can just “subscribe” your code as a Lambda function to a topic, and then from your own application publish messages to that topic.

Every subscription will get that message and act upon it, so sending an SMS could be just another thing that you can do when an interesting event happens (think about notifications!).

Also, this has the advantage that your systems should only need to integrate with SNS, and then you can just “plug in” more subscribers in SNS as you need them. So not only this is absolutely fun, it’s really useful too 🙂 Let’s see how it works!

Get the code and prepare it to use it from Lambda

Clone the code from https://github.com/PortaText/sns-lambda-example, download the dependencies, and create a .ZIP file with its contents:

$ git clone https://github.com/PortaText/sns-lambda-example.git
$ cd sns-lambda-example
$ npm install

Once this is done, you must edit the file index.js and change the line

var apiKey = 'your_api_key';

So it has your own API key. After that we’re done, and we only need to ZIP everything!

$ zip -r sns-lambda-example.zip *

Create a Lambda function to send SMS

In order to create your Lambda function, head over to your Lambda Console and:

  1. Click on “Create a Lambda Function”.
  2. Select NodeJS 4.3 as the runtime.
  3. Click Next. Don’t select a Trigger yet, we will create the SNS later on.
  4. Write the name and description of your function.
  5. Choose “Upload a .ZIP file” in “Code entry type”.
  6. Click “Upload” and select the .ZIP file created above.
  7. In Role choose “Create a Custom Role”, click on “Allow”.
  8. Click on Next, and then on “Create Function”.

Setup your SNS Topic

In your SNS console:

  1. Create a new topic in your SNS Console.
  2. Add a new subscription to it. Choose AWS Lambda as the Protocol, and select
    your new Lambda function as the Endpoint.
  3. Take not of your SNS ARN.

Create a user with Publish permissions for the topic

You should create a user and obtain the an access key and secret so you can publish
to your new topic. The policy should be something like this:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "Stmt1473859117000",
      "Effect": "Allow",
      "Action": [
         "sns:Publish"
      ],
      "Resource": [
         "the_arn_for_your_topic"
      ]
     }
  ]
}

Send your first SMS by publishing a payload to the SNS Topic

Test your topic by publishing a payload like this, either via the SNS console or via the Amazon AWS SNS API:

{
  "from": "one_of_your_dids",
  "to": "a_destination_number",
  "text": "hello world from Lambda"
}

And you should receive the SMS in the next few seconds 🙂

Conclusion

As you can see it’s really easy to integrate PortaText in the workflow of your cloud applications (in this case, using Amazon AWS). By using SNS you can extend your systems so they can react to different events, and in this case use your PortaText account to send SMS from the cloud with just a few lines of code.

If you have any questions, please let us know, we love challenges and are always glad to help to integrate 3rd party systems.

Until the next time 🙂

— The PortaText Team.