Send SMS using Nexmo and Amazon Web Services’ Lambda

Developers have many tools to chose from when it comes to verifying users to mitigate fraud. In this specific example, I will explain how and why I used Nexmo to enable communications in my favorite cloud hosting environment, AWS (Amazon Web Services).

Previously, I used Nexmo’s SMS API to send SMS application alerts to users. I have also used Nexmo’s Verify API to enable two factor authentication (2FA) to allow my client’s customers to view their order status without having to log in to an eCommerce platform.

There are two key aspects to Nexmo’s Verification API:

  1. Generation of random number is handled by the API which relieves the app developer from generating and storing the number for verification later.

  2. Nexmo makes a voice call as a fallback if the number used for verification is either not a text enabled number or if the code is not entered by the customer within a few seconds.

Amazon recently launched AWS Lambda, which is an event-driven computation service that abstracts the infrastructure required to run an application behind a code. What this means is that as a developer, it’s sufficient to write a piece of code that takes action on an event without the need to setup an infrastructure to run the code. A typical example is an image resizing service that can take an image and resize on request. Some of the unique benefits include metered billing in increments of 100 milliseconds and scaled infrastructure to process the event automatically based on the throughput of input events.

In this article, I will demonstrate how to send SMS using Nexmo API from an AWS Lambda function when an event is published to a SNS topic.


Publish a message to the SNS Topic and process it through a lambda function which sends a SMS message using the Nexmo API.

The process involves 5 steps:

  • Setup Nexmo account
  • Setup IAM role
  • Create a Lambda deployment package
  • Configure Lambda function
  • ​Configure SNS and connect to lambda
steps for lambda function

Set Up Nexmo Account:

If you don’t have a Nexmo account already, set up an account here.

Buy a number to be used as a “from” number to send the SMS message.

Set Up an IAM Role:

Create a new user or get the arn of an existing user who should be able to subscribe and publish to the SNS queue.

Create a Deployment Package:

Create a nodejs project with package.json and index.js file. A sample project can be downloaded from this github project.

The signature of the function that will be invoked by Lambda is:

function(event, context)

The structure and content of event param depends on the service that invoked the lambda function. In this case, the invocation is from SNS the event object structure.


"Records": [
      "EventSource": “aws:sns”,
      "EventVersion": “1.0”,
      "EventSubscriptionArn": “arn:aws:sns:EXAMPLE”,
      "Sns": {
        "Type": “Notification”,
        "MessageId": “95df01b4-ee98-5cb9-9903-4c221d41eb5e”,
        "TopicArn": “arn:aws:sns:EXAMPLE”,
        "Subject": “TestInvoke”,
        "Message": “Hello from SNS!”,
        "Timestamp": “1970-01-01T00:00:00.000Z”,
        "SignatureVersion": “1”,
        "Signature": “EXAMPLE”,
        "SigningCertUrl": “EXAMPLE”,
        "UnsubscribeUrl": “EXAMPLE”,
        "MessageAttributes": {
          "Test": {
            "Type": “String”,
            "Value": “TestString”
          "TestBinary": {
            "Type": “Binary”,
            "Value": “TestBinary”


The message sent to SNS can be retrieved using this access key:


Create a function to parse the incoming message and call nexmo API using the easynexmo nodejs library as below:

nexmo.sendMessage(smsMessage,function(err,messageResponse) {
   //add custom logic to handle success or failed message delivery
   consolelog (err,messageResponse);

Once the functions are created and tested, zip the files from within the folder. Its important that the zip archive is from within the folder that contains the package.json, index.js and node_modules files. This archived file is the deployment package of the lambda function.

Configure Lamda Function:

Now that the lambda package is created, use the AWS console to create a new lambda function and upload the package. Below is the screenshot showing the values on how to configure the handle and file names.

function code

Configure SNS and Connect to Lambda:

The last step is to create the SNS topic to publish the message and add a lambda subscriber to process the message sent to SNS topic.

Either use AWS CLI or AWS console to create a new SNS Topic. Then add a new subscriber and make sure to choose the protocol as AWS Lambda.

The endpoint dropdown will contain the new created lambda function from the previous step.


There are two ways to test this setup.

AWS console provides a useful debugging function that can send a test message to the lambda function. Use the event JSON from above to test from the console.

Create a new publisher to send message to the SNS queue. For simplicity, I added a test function in the index.js file that can publish a message to the SNS topic. Here is the sample code to accomplish this:

function testSNSDelivery() {
var message = { from:NEXMO_FROM_NUMBER,
   text:‘testing from local to sns to lambda to nexmo’
var params = {
      Subject: 'TestSNSNexmo’
         if (err) {
            console.log('Error sending a message’, err);
         } else {
            console.log('Sent message:’, data.MessageId);
sns.publish(params, function(err,data){

The logs generated by lambda function can be viewed from cloud watch log viewer.

You may ask why I used a lambda function to send SMS. As an alternative, I can use the built-in SMS function of SNS to send SMS messages. However,  SNS doesn’t support sending SMS worldwide. Nexmo has a wide variety of communications API that can be used to do Two Factor Authentication, Number Verification, Number Insights, Make a Phone Call, Text To Speech and SIP endpoints. Above all, Nexmo supports sending SMS worldwide.

Example Use Cases:

    Send notification when:

  • A file is uploaded to S3 bucket

  • A job is completed in Hadoop cluster

  • An iot sensor detects a change like a light is turned on, a door is opened etc.

  • A new customer downloaded the app from appstore    


Nexmo provides a cost-effective way to sending SMS worldwide. AWS Lamda provides a cost effective way to process events without managing an infrastructure. By using Nexmo and AWS Lamda you can create an asynchronous  notification microservice component for your backend infrastructure without having to maintain an infrastructure.

Prabhu Velayutham Developer by passion, Architect by profession.