How I Used Serverless Infrastructure to Build a Large-Scale Petition System

An example web app using OpenWhisk and Cloudant

Glynn Bird
Feb 27, 2017 · 6 min read

Let’s build a large-scale petition system

Modelling our system on the UK model, our petition system would have a public-facing website with a simple form detailing the issue being “signed”. The user supplies their name, location, email address, confirms that they are a resident of the country in question, and submits the form. Our app saves the data in a Cloudant database and sends a verification email to the signatory.

Serverless hosting

An obvious choice for “serverless” web hosting is GitHub Pages. Sign up for GitHub, create a repository, and put some HTML/JavaScript/CSS in a gh-pages branch. Your website will be served out at http://USERNAME.github.io/REPO/. You can even point your custom domain name to GitHub and it serves out your pages on that domain e.g. mypetition.com.

Serverless computing

Use IBM OpenWhisk to handle form submissions and email verification requests. OpenWhisk lets you write your code in JavaScript, Swift, or Python and have it run in response to incoming events — in this case the submission of a web form or the clicking of a link in an email.

Serverless transactional email

In order to send emails to signatories, you need a transactional email service. I set up an email template with placeholders for the content that changes between recipients. The email provider ensures that the emails are sent, avoiding each users’ spam filter. I used SendGrid because they let you send a templated email via a simple API call.

Serverless database storage

Ultimately we need to store our data somewhere. So someone is going to have to manage some servers and disks. By using the Cloudant database-as-a-service, we don’t have to have fixed servers dedicated to us; we can simply consume a database service on a shared cluster and pay only for the requests and storage that we use.

Designing our data flows

OpenWhisk computing tasks are built up from actions. You can combine many actions into sequences of actions and activate them with incoming data (such as an API call).

  • POST /petition/confirm - to confirm that the user has clicked on a link from the email. Another GitHub pages page renders on the browser, which makes an API call to OpenWhisk. The database record updates to mark the signature as "confirmed".

Sign up for the services

Going serverless means you don’t have to deal with equipment, virtual servers, operating systems or networking, but you still have to sign up for some accounts:

  • follow Getting started with OpenWhisk to get the wsk tool installed and authenticated
  • inside Bluemix, sign up for a Cloudant service. Make a note of the Cloudant URL, including username and password.
  • setup a SendGrid account, create an API key that has permissions to send transactional emails and create an email template
  • create a Github repository with your web site in it and branch to a gh-pages branch

Deploying OpenWhisk code

OpenWhisk has nice dashboard where you can paste your code, build sequences, and try your code. This is fine when taking your first few steps with OpenWhisk but very soon you’ll want to script the deployment of your code so that it can be automated and reproduced easily.

  • COUCH_DBNAME = the name of the Cloudant database e.g. petition
  • SENDGRIDBEARER = the SendGrid API key
  • SENDGRIDSENDER = the email address that emails will appear to come from
  • SENDGRIDTEMPLATEID = the SendGrid email template id to email

Try it yourself

You can sign our demo petition yourself here. The code that appears on that page is in this GitHub repository. The OpenWhisk actions and the deployment script can be found in the openwhisk directory.

Who’s paying the bill?

So I have a totally “serverless” system with Github serving the static content, OpenWhisk handling the form submissions, SendGrid sending emails, and Cloudant storing the data. But is serverless free?

Not free — it’s pay-as-you-go.

  • GitHub reserves the right to disable or throttle your site’s usage if your content is very popular
  • SendGrid has a range of plans that start at free for the first month but typically allow you to send tens of thousands of emails from $10 per month.
  • OpenWhisk has a free tier and then takes payment depending on the volume, memory, and execution time of your actions.
  • Cloudant’s default Lite Plan offers a limited API call limit for free, or increased capacity for additional dollars per month.

IBM CODAIT

Things we made with data at IBM’s Center for Open Source Data and AI Technologies.

Glynn Bird

Written by

Developer @ IBM. https://glynnbird.com

IBM CODAIT

Things we made with data at IBM’s Center for Open Source Data and AI Technologies.