Developing & Deploying Serverless Apps made easy using Nimbella & Python

Satya
6 min readNov 1, 2020

Recently I participated in a hackathon (#DeveloperIPL Game) organized by HackerEarth , Nimbella & Postman to build serverless apps for social good. Nimbella is a platform to deploy serverless web apps, api’s & functions. I am writing this blog post to walkthrough how to deploy simple web api’s with quick turnaround times (literally in minutes) using nimbella & python.

To follow along, It is required to sign up for a free developer account at nimbella. Also, you need to install nimbella cli on your machine. The CLI is available supports all three major operating systems windows, linux, mac & you can follow the instructions here for a quick setup.

Too lazy to set up an environment ? Just go to this repo, click on Open in gitpod button & follow on screen steps to get a ready made linux env with vs code in the browser.

So now what I am going to build & deploy ? — two api’s using python.

Using python was a choice & nimbella supports a wide range of languages to choose from. Also, deployment is language agnostic meaning you can build one api using python & other api using any languages like node or php.

So what are the api’s about ?

  • Fact check:

Misinformation pertaining to COVID-19 is Rising along with confirmed cases. While some misinformation is related to vaccines or symptoms, there is also other irrelevant info spread by people in social media channels like FB, Instagram & Youtube to increase their following. The idea is to combat all these wrong infos using Fact-Check API by validating the information to maintain peace of mind. It uses Google’s Fact check API in the backend (still in Alpha).

  • Urgency of Hospital Admission Check:

With Covid-19 cases increasing day by day, High population countries like India are facing shortages in available emergency beds. And with private hospitals coming into play, they started money bidding on the available beds — Who can pay more money gets the admission first regardless of emergency. I feel lack of money/not being rich should not be a discriminator in for availing treatment.

In order to resolve this, I built a prototype of an automated system with help of Random Forests ML Algorithm (using python & scikit-learn ) which rather classifies the urgency of admission based on their age, symptoms & gives them the preference.

* Caution Note: ML Algorithms are only as good as data. This is just a working prototype with a small sample of aggregated (crowdsourced) data, so don’t expect top-notch results.**

Let’s get started…

If you want to have look (visualize) at what we are building, experience it here.

I will only explain the backend code for APIs & deployment process needs to follow to get these both up & running on Nimbella. The frontend is pretty much static, built using Jquery & you can find those files here in the public folder of the git repo.

Nimbella CLI:

It is a command line tool which helps the developers in creating & deploying the serverless applications. Some useful commands

`nim auth login {key}` — to login to your registered account. You can get a key from nimbella account after signing or signing up if you haven’t yet.

`nim project create {name}` — automatically generates a project directory with given name & yaml configurations for deployment inside it.

`nim project deploy {name}` — builds & deployed the project into the nimbella cloud.

Nimbella Actions:

Each api will be treated as separate action by the nimbella. Going by that convention, We will build two actions as described earlier which will function same as rest-api’s you can call them using postman as well.

`nim action list` — To list all the actions currently deployed & running in your namespace.

`nim action invoke {action-name}` — invoke the action/api deployed.

Nimbella Namespace:

When you’re signed up, you will be automatically allotted a random namespace by nimbella.

`nim namespace get` — to get all your actions & their namespace

For example if you namespace is `random-namespace` then you will be able to access your api’s at https://random-namespace.nimbella.io/

It’s time to get started with our actual project, I will be going with the name `covid-tools`.

  1. Generate a project template directory, run the command below from cli to do the same. If you don’t specify a language argument, it goes with default i.e. javascript (js). This will create project directory with two folders `web` & `packages`

`nim project create covid-tools — language=py`

Inside packages/default, there is hello.py which is a hello world serverless function. Let’s deploy this action & invoke it.

`nim project deploy covid-tools`

`nim action invoke hello`

The action will typically have the name of folder i.e. default/hello

Here are few important points to note:

  1. There should be a main function for every action.
  2. If you are having multiple python files & imports, make the name of the entry file as __main__.py & define a main function in it.
  3. The return from the main function should be json with the key body.

FACT CHECK API

Now following the above points I changed the hello world program to fact-check api by making necessary changes.

  1. Rename the hello.py to __main__.py
  2. Write the code the necessary code to call the google fact check (refer here)
  3. For sake of simplicity, first hardcode the API_KEY variable (If you are following the above reference).
  4. To test this code, define the name constructor at end & call the functions you need to test. Later run the command `python __main__.py`
  5. Once you are sure your code is working fine, you can run the commands to deploy & invoke as shown in case of hello world action as above.

Once deployed, you can call the api/action using postman as well.

`nim action get {actionName} — url`

Run the above command to get the url to need to hit. Refer to this sample postman collection I created for fact-check & urcheck api’s.

This is all good for simple projects with a single actions,

But What if

  • Project is more complex having multiple actions written in multiple languages?

For more complex projects, you need to define a yaml config file (project.yml). You can also define different runtimes for different actions as well. refer this example.

  • There is sensitive information like API_KEY ?

You can define them as environmental variables & include a `.env` file. Later you can also map them in the project.yml file. refer this example.

  • The actions are having external dependencies ?

Incase of python, Define a requirements.txt & a build file with commands you need to run inside your action directory.

If you are on linux you need the `build.sh` file. else if on windows, you need the `build.cmd ` file. refer the examples here

URGENCY OF HOSPITAL ADMISSION CHECK API

  1. For this api, I trained a random forest model using the sklearn library using the covid-19 data from here.
  2. Save the model using Joblib
  3. Place the joblib file in the action directory (default/urcheck/models). Refer here.
  4. Load the file to make the predictions using the response received from the end user.
  5. Once you are sure that code is free from bugs, you can run the commands to deploy & invoke as discussed earlier.

Actual challenge here is installation of dependencies like numpy, pandas , sklearn which are very large components & also requires c, c++ to be installed. But from the Nimbella slack community channel I got to know that nimbella already has a separate pre-built python runtime for AI applications so I changed the runtime to python3:ai in the project yaml file. This runtime also has tensorflow, keras installed in case you are interested.

Initially, one might need some time to get familiar with nimbella cli & it is well documented as well, but after that it’s all matter of minutes to get the things running. It is as simple as that. In case your function needs to maintain the state, nimbella runtimes also come with a storage bucket & redis as well. You just need to add couple more lines of code to your action, for more details refer this python-sdk

For more examples of apps built using node.js & php on nimbella, refer to this repo.

--

--

Satya
0 Followers

Data Guy | Open Source Enthusiast