Fast API Prototyping with Webtask.io and Serverless.
Function as a Service (FaaS) commonly referred as serverless computing, feels like the answer to every engineer’s prayers. Simple functions, doing only one small thing and running in infrastructure that scales automatically for a small price, or even for free 😍!
I’m working with Serverless on my current project, and I’ve wanted to take Webtask for a spin for a while, so I decided this was the perfect time for it.
In a few minutes, I had a REST API, deployed, and running with the help of Express, RethinkDB, Webtask, and Serverless.
Getting started 🏁
Install the serverless framework and create a new project:
Go into the project’s folder and add the dependencies with NPM:
body-parserwill handle routing and JSON responses.
webtask-toolswraps the express app and binds it to the webtask handler.
rethinkdbdashis a RethinkDB driver; it has some cool features like connection pooling, an easier interface, and outstanding performance.
serverless.yml. Change the service name and add the IP of the RethinkDB server:
The only differences from the default config should be the service
name and the
The options tell the provider (webtasks in this case) to set
RETHINKDB_SERVER as an environment variable and to take its value from either the environment (
env), an option from the command line when deploying (
--rethinkdb-server) or, if neither is set, from the
The API will be running from Auth0’s FaaS infrastructure. RethinkDB needs to be reachable from the internet. I launched a Digital Ocean instance for that.
The final step is to set up a Webtask account:
Show me the code! 🤓
At this point, there is already a Function that can be deployed, run and tested. Give it a try:
It is important to run
npm update to install all packages before deploying a function. Dependencies will be pulled from
node_modules and uploaded with the function.
The service is running already and zero code. Wow! That is really something!
I realized that this was an excellent first approach to the FaaS paradigm. Definitely easier than setting up an account for AWS λ.
Now into the actual code…
Express + RethinkDB
Like most express apps, start by adding
webtask-tools. Create the app and add the database as a middleware to all routes:
The RethinkDB instance is now available as
db through every endpoint.
GET /: Retrieves all documents from RethinkDB.
GET /:id: Returns a particular document with the given
It is a simplistic version with no filtering and basic pagination.
The endpoint will get the response from RethinkDB and return it to the client. If anything goes wrong, it will send the error instead.
POST /: Creates a new document.
PUT /: Updates an existing document.
Creating and modifying data is just as simple.
The endpoint gets the JSON from the request and saves it into the
shares table. The code is almost the same for both functions.
RethinkDB has support for upserts. When the
conflict option is set to
update if the body of the request has an existing
id it will replace the document. The default behavior is to throw an error if the document with that
id exists, which is what the POSTs method is doing.
returnChanges, the query will return an Array with the resulting changes of the operation in a special object with two properties:
old_val. We use
new_val to return the upserted item in the response. In the case of a POST, the object will include the
id of the new document.
RethinkDB accepts an Array or an Item when inserting. Lines 6 and 11 normalize the response. If the request had an array, it would return an array with the changes. If it were just one item, it would return only one item.
Besides returning the actual data, it includes the
response from RethinkDB. It contains an object with helpful information that looks like this:
"name": "Tania 😍"
It can be helpful for the requester to know what happened to the data.
DELETE /:id: Removes the document with the given
idfrom the table.
The endpoint gets the
id from the request, and ask RethinkDB to remove that document. If anything goes wrong (i.e., the id doesn’t exist) it sends the error back to the client.
That’s all. A fully working —barebones — function as a service-based REST API. The code is straightforward. Small functions that are easy to read and understand.
Next steps will be adding Authentication to the requests and some business logic so the service is more helpful.
I will continue working on it and will elaborate about it in another post.
Webtask and Serverless make it easy to start “dipping your toes” into the Function as a Service world. It focuses around NodeJS with only two event sources HTTP Requests and Scheduled events. Compared to AWS Lambda it’s faster to get started and easier to approach.
Webtask’s free tier is limited to a request per second. But that should be enough for testing; maybe even for a basic service.
More resources 📚
- Webtask.io, the FaaS from the Auth0 guys. It’s easy and straightforward. Works with Node 8 out of the box and it takes less than a minute to get started. Super useful to get started with Slack hooks, Bots, and APIs. It has a fully-featured Web Editor that makes things even simpler. It has real-time logs, 500k of JSON storage, a cron-like scheduler, and Auth0 support is baked in.
- Serverless is a toolkit to work with serverless architectures from any provider (AWS Lambda, Google Functions, Azure Functions, OpenWhisk, Webtask, etc.). With the help of its Event Gateway, you can even combine different providers and make them all work together.
- RethinkDB is an Open Source NoSQL Database with a very nice querying language (ReQL) and advanced features like clustering, near-linear scaling, and real-time feeds. It’s my go-to NoSQL data store.