Twilio Voice to Pagerduty alert using Python Flask, Zappa, AWS Lambda & AWS API Gateway

My SaaS product DevOps team at Quest Software uses several monitoring services to notice problems (hopefully before end users see them), and raises alerts for our team using PagerDuty. We also frequently need to integrate with existing company and partner products, for example our internal helpdesk and customer-facing technical-support processes. In this case, the helpdesk team wanted to have a phone number they could call to raise an alert to our team. The first suggestion was to simply put my name down as the 24×7 on-call contact, and make it my problem to alert the right people. I scoffed. We already had PagerDuty in place – why couldn’t we use that too? Simply because we didn’t have a phone number hooked up to PagerDuty. So, lets fix that.

A few searches quickly turned up a PagerDuty blog where David Hayes had done exactly this. Excellent! However, it was written to use Google App Engine, and my team has their processes predominately in Azure and AWS. I didn’t want to introduce yet another set of cloud services, for something conceptually so simple.

Twilio’s quickstarts do a nice job of showing how to use their API, and these use Flask for the web framework. How can I use Flask apps in AWS Lambda? Here enters Zappa, a tool for deploying Flask and Django apps into AWS Lambda & AWS API Gateway. Slick! Now I have all the pieces I need.

You can find the code on github. I’ve extended the quickstarts slightly, to have the phone response first prompt for the application that is experiencing issues, and recording that in a session cookie to be retrieved later. Then it prompts the user to leave a message. With these two pieces of information, we have enough to create the PagerDuty incident for the proper application, including information about the caller gathered from Caller ID (in case the recording is garbled), and a link to the recorded message. Not too shabby for ~125 lines of “my” code, at a cost of ~$1/month to Twilio for the phone number, almost $0.00 for AWS, and a couple pennies if anyone actually calls to raise an alert.