Using a Message Queue With SMS For Throughput Limitations
Published on May 5, 2021

In some countries, throughput limitation on SMS and slow request times (sometimes as low as one request per second) add challenges for bulk sending. One way to navigate these limitations is by using a message queue, such as AWS SQS.

For this example, you create a serverless microservice on AWS Lambda using this Python application available on Github that uses Flask, and Serverless. The application serves two purposes: It adds messages to AWS SQS and then facilitates the actual sending through Vonage SMS as requested.

Prerequisites

DT API Account

To complete this tutorial, you will need a DT API account. If you don’t have one already, you can sign up today and start building with free credit. Once you have an account, you can find your API Key and API Secret at the top of the DT API Dashboard.

Setup Instructions

Clone the nexmo-community/sms-aws-sqs-python-sender repo from GitHub, and navigate into the newly created directory to proceed.

Environment

Rename .env.default to .env and add values to VONAGE_API_KEY and VONAGE_API_SECRET provided on your Vonage API Dashboard.

AWS Setup

You need to create AWS credentials as indicated by Serverless. Update the .env file with these.

Also, create a new SQS FIFO queue using the default settings, and update .env with the SQS queue URL. FIFO ensures messages get sent in the order they were stored.

Usage

To start, create a virtualenv from within the project root to contain the project. Then, activate it as follows:

virtualenv venv --python=python3 source venv/bin/activate

Next, initialize npm and follow the prompts selecting the defaults, unless you desire to change any of them. Also, use npm to install needed dependencies for DEV to enable Serverless and Lambda to work with the Flask app.

npm init npm install --save-dev serverless-wsgi serverless-python-requirements

Now you can use pip to install the required Python dependencies. The dependencies are in the requirements.txt, so instruct pip to use it.

pip install -r requirements.txt

Running Local

Should you wish to run the app locally and test things out, before deploying to AWS Lambda, you can serve it with the following command:

sls wsgi serve

By default, this serves the app at http://localhost:5000. You hit Ctrl+c to close it down.

Deploy to Lambda

With all the above finished successfully, you can now use Serverless to deploy the app to AWS Lambda.

sls deploy

IMPORTANT: This application does not contain any authentication for use. Therefore, ensure you add authentication in front of it to prevent public usage. By leaving it open to the public, it could result in usage charges at AWS and Vonage.

Available Endpoints

There are 3 URL endpoints available with this client:

1 GET request to /

  • Doesn't perform any actions, but provides a quick way to test

2 POST request to /add

  • This action stores the message in SQS.

  • Pass a POST with a JSON body like the following. Substitute the placeholders, indicated with <> with your data.

  • The result contains the SQS MessageId.

{
    "from": "<your_name_or_number>",
    "message": "<sms_message_contents>",
    "to": "<recipients_number>"
}
</recipients_number></sms_message_contents></your_name_or_number>

3 GET request to /process

  • Kicks off the sending process.

  • Retrieves messages from the SQS FIFO queue.

  • Connects to Nexmo, and sends the SMS message.

location=None, media_mode=relayed, archive_mode=manual
Examples:

Go to the URL provided by the deploy process. Below are some examples of what sample requests may look like: (Your URL may vary.)

GET https://7ulasfasdasdfw4.execute-api.us-east-1.amazonaws.com/dev/

The / endpoint returns the generic message.

POST https://7ulasfasdasdfw4.execute-api.us-east-1.amazonaws.com/dev/add

The add endpoint returns the SQS MessageId.

GET https://7ulasfasdasdfw4.execute-api.us-east-1.amazonaws.com/dev/process

The process endpoint returns a message indicating a successful send.

Deactivating Virtualenv

To exit the virtualenv, you can deactivate it when desired.

deactivate

NOTE: Depending on OS, you may need to prepend virtualenv to the command above.

Next Steps

In this example, the application contains a /process endpoint callable with an HTTP GET request at scheduled intervals. Alternatively, the code in /process could be put in a separate Lambda function and called directly from CloudWatch scheduled events instead of an HTTP GET request.

If you have any questions or run into troubles, you can reach out to @VonageDev on Twitter or inquire in the Vonage Community Slack team. Good luck.

Adam CulpVonage Alumni

Adam is a developer and consultant who enjoys ultra-running, blogging/vlogging, and helping others tame technology to accomplish amazing things with an insatiable desire to mentor and help.

Ready to start building?

Experience seamless connectivity, real-time messaging, and crystal-clear voice and video calls-all at your fingertips.