Automate SMS sending with Serverless, AWS EventBridge, AWS Lambda (node.js), and AWS SNS

Nermin Imamovic
4 min readMay 14, 2022

Mobile phones are a part of us. From the ’90s SMS represents a powerful communication tool. During the development of different internet messaging applications, SMS still remains one of the most trustful and popular communication way.

There are a lot of SMS subscriptions scheduled on the different events, such as constant time range (weekly, monthly subscriptions), cron job definition, or different events. In this article, we will present a way of creating a scheduled SMS sender using Serverless, AWS Lambda (node.js), and AWS SNS.

Introduction

A serverless framework is a black box way of using different AWS services together achieving the maximum of the software. We don’t need to set up things and trigger actions manually. Everything is done in the code and services are handled on the payed-per-use approach.

AWS Lambda represents a single executable code unit-specific only to just one functionality. It is run without thinking about servers and paid only for the compute time that the Lambda function consumes.

AWS SNS (Simple Notification Service) is a publish/subscribe system used to notify end-users by the different actions on the different protocols (send SMS, Email, HTTP(S) calls, mobile push notifications, another lambda subscribed to the topic, SQS event, etc..). The task for which it was designed is to send all the messages it receives to each of its many subscribers. SNS follows the “push” principle, where it’s the responsibility of the system to make sure all subscribers are notified of the new message. No polling on the part of subscribers is required.

Amazon EventBridge is a serverless event bus service used to connect applications with data from various sources. EventBridge receives an event, an indicator of a change in environment, then trigger an event to run our lambda function.

In this article, we will make a simple solution for sending SMS without over-engineering and without introducing unnecessary complexity in it. Later we will suggest potential improvements.

Before we start we should be sure that we have our AWS credentials exported on our machine. Also, at the moment it is possible to send SMS only on region us-east-1 so we need to set it as the default.

In this article we are using serverless version 1.8.3 so we should install exactly that one:

npm install -g serverless@1.8.3

Development

First, let’s create a new serverless project using the next command:

serverless create --template aws-nodejs --name message-sender --path message-sender

We have created a new repository message-sender with two main files handler.js and serverless.yml. handler.js contains lambda function that should be executed after the calling. serverless.yml file contains configuration about our services, such as environment variables, roles, events to run lambda, and external services used.

First part provider shows information related to runtime, environment variables, and role possibilities. We enabled every action on the SNS service.

functions part contains information about lambda functions and events how they are triggered. Our function is named as sendMessage and it is triggered using eventBridge service every second day.

For using the SNS, we will need to install AWS-SDK in our handler.js file using:

npm install aws-sdk

Let’s update handler.js file to look like:

The three most important attributes related to sending SMS messages are Message , PhoneNumber and StringValue in MessageAtributes attribute AWS.SNS.SMS.SenderID .

  • PhoneNumber — Number that should receive the message.
  • Message — Message content.
  • StringValue — Sender whose name will be shown in the SMS.

You could update it with your own values.

Then we need to deploy our lambda with the next command:

serverless deploy

or

sls deploy

After it is deployed we can see how our lambda function looks on the AWS console.

Now we could expect to have SMS on the wanted telephone number.

If we want to stop SMS sending we could mark the flag enabled to false and redeploy it. To remove our lambda we are using the next command:

sls remove --stage dev --region us-east-1

Full code could be found in the repository.

Benefits:

  • Serverless provides us the benefit of black box work without worrying about AWS configuration and spending time to set up it.

Drawbacks:

  • At the moment it is only possible to send SMS using us-east-1 the region, so if we want to integrate SMS sending the whole system should be in that region.

Possible improvements:

There are a lot of different SMS services such as Twillio, Foster, etc. So we could try some of them also. Due to it, we could reorganize our code to use services that send SMS in a different way, we could have a lot of benefits such as Dependency Injection, easier unit testing, etc.

Also as SNS provides other different types of message subscriptions, we could try to have Email, Push notifications on our service, etc.

Related to business needs this idea could be used as a marketing tool for subscriptions or notifications.

References

If you’ve found any of my articles helpful or useful then please consider throwing a coffee my way to help support my work 😊, by using

Ko-fi.com

Thanks,
Nermin

--

--

Nermin Imamovic

Software Architect/Developer from Sarajevo. Double Masters Degree in Computer Science and Informatics. Simplicity reduces complexity.