Building a Serverless “Voter Ipsum” API as Quick as Voting

My Serverless #noServerNovember Submission

What an amazing day. If you haven’t voted yet: please stop reading this post, find your polling place, and cast your vote! If you have voted: you legend, great work! I wanted to share a quick hack I put together today to celebrate our rights and… participate in the Serverless #noServerNovember challenge. Serverless challenges are great for someone like me who is slowly learning how to say goodbye to servers and hello to that sweet sweet “Function as a Service” lifestyle. One of the options for this week’s challenge was to build a Serverless Ipsum generator, so here’s how I built “Voter Ipsum” on AWS in record time. 🔥

First, I made sure serverless was installed globally on my machine:

$ npm install serverless -g

Then, I followed the Quick Start instructions for AWS to make sure my AWS account was connected to the serverless CLI. As it turns out, since I was using the aws-sdk on my machine already — I was setup. 👍🏻

Next, I created my serverless project and navigated into the folder:

$ serverless create --template aws-nodejs --path voter-ipsum
$ cd voter-ipsum

From here, I made a slight adjustment to the default settings within serverless.yml so that serverless would expose an Amazon API Gateway endpoint for my ipsum service. Here’s the full configuration:

service: voteripsum
provider:
name: aws
runtime: nodejs8.10
functions:
hello:
handler: handler.vote
events:
- http: GET vote

Then, I found Nickolas’s excellent lorem-ipsum node module and used it to create the simplest serverless function ever. All my function does is initialize a loremIpsum function which exports one paragraph of text using the words “go,” “vote,” and “today.” This output is then served as a JSON response using the functions callback.

'use strict'
const loremIpsum = require('lorem-ipsum')
module.exports.vote = (event, context, callback) => {
const output = loremIpsum({
count: 1,
units: 'paragraph',
words: ['go', 'vote', 'today']
})
  const response = {
statusCode: 200,
body: JSON.stringify({
"voter-ipsum": output
})
}
  callback(null, response)
}

Finally, all I had to do was use the magic serverless cli to deploy it:

serverless deploy -v

You can check out the API live on AWS here and take a look at the full source code on my repo. You still have time to VOTE and submit to the #noServerNovember challenge. Good luck!