Whisk Deploy — API Gateway (Action to REST API)
This is a long overdue article🚶😅 . I guess its never too late …
Daniel Krook conducted a technical workshop on Serverless Computing at Index San Francisco earlier this year. His workshop exercises reminded me of Whisk Deploy where we had recently added support to create API Gateways and much more. We referred to a OpenWhisk bootcamp during the workshop which is full of examples and an easy to follow guide to create several serverless applications. Let’s directly jump into API Gateway.
Your first API
We ran through following steps at the workshop to expose an action generating Fibonacci numbers as a REST API using wsk
CLI (using fibonacci.js):
$ wsk action create fibonacci fibonacci.js --web true
ok: created action fibonacci$ wsk api create /fibonacci get fibonacci
ok: created API /fibonacci GET for action /_/fibonacci
https://service.us.apiconnect.ibmcloud.com/gws/apigateway/api/5b3edceff7c07077d71813c2b18d1a58c175337c37526b671dd37c0769c5c9a5/fibonacci$ curl --request GET https://service.us.apiconnect.ibmcloud.com/gws/apigateway/api/5b3edceff7c07077d71813c2b18d1a58c175337c37526b671dd37c0769c5c9a5/fibonacci?num=10
{
"body": "n: 10, value: 89, sequence: 1,1,2,3,5,8,13,21,34,55,89, invocations: 19"
}
I quickly wrote a manifest.yaml
to simplify the entire workflow:
Now, get the latest release of wskdeploy
from here and run it with fibonacci-manifest.yaml
:
./wskdeploy -m fibonacci-manifest.ymlInfo: Deploying package [fibonacci] ...
Info: package [fibonacci] has been successfully deployed.Info: Deploying action [fibonacci/fibonacci] ...
Info: action [fibonacci/fibonacci] has been successfully deployed.Info: Deploying api [fibonacci-api /fibonacci/sequence GET] ...
Info: api [fibonacci-api /fibonacci/sequence GET] has been successfully deployed.Success: Deployment completed successfully.
List the api just created:
$ wsk api list
ok: APIs
Action Verb API Name URL
/<namespace>/fibonacci/fibonac get fibonacci-api https://service.us.apiconnect.ibmcloud.com/gws/apigateway/api/5b3edceff7c07077d71813c2b18d1a58c175337c37526b671dd37c0769c5c9a5/fibonacci/sequence
Trying it out:
curl -X GET https://service.us.apiconnect.ibmcloud.com/gws/apigateway/api/5b3edceff7c07077d71813c2b18d1a58c175337c37526b671dd37c0769c5c9a5/fibonacci/sequence?num=10{
"body": "n: 10, value: 89, sequence: 1,1,2,3,5,8,13,21,34,55,89, invocations: 19"
}
Hurray 😃! With just a manifest YAML
file and an action source file, we could deploy a web action and an API. Well honestly, manifest YAML
didn’t oversimplify the whole workflow in this example. Let us explore one more example and find out how Whisk Deploy simplifies action/API creation.
Book Management Application
After creating an API for fibonacci series, we worked on a bit more complex application and created multiple actions/APIs. Here are the list of instructions we followed using wsk
CLI. The idea here is to expose a set of actions for managing books you have read for which we need to implement a couple of actions forming a serverless microservices backend for creating, reading, updating, and deleting books.
Step 1: Cloudant Setup and Configuration
- We are using a Cloudant Instance in this example application. Please create a cloudant instance named bookStore by following instructions in bootcamp.
- Now, create a database named books under bookStore by exporting CLOUDANT credentials and invoking /whisk.system/cloudant/create-database action:
$ export CLOUDANT_USERNAME=<username>
$ export CLOUDANT_PASSWORD=<password>
$ export CLOUDANT_DATABASE=books$ wsk action invoke --blocking --result /whisk.system/cloudant/create-database — param username $CLOUDANT_USERNAME — param password $CLOUDANT_PASSWORD — param host $CLOUDANT_USERNAME.cloudant.com — param dbname $CLOUDANT_DATABASE{
“ok”: true
}
- Deploy action/sequences/API using
wskdeploy
:
Here is the books-management-manifest.yaml:
$ wskdeploy -m books-management-manifest.yaml
Info: Deploying package [bookStore] ...
Info: package [bookStore] has been successfully deployed.Info: Deploying package binding [myBookStore] ...
Info: package binding [myBookStore] has been successfully deployed.Info: Deploying action [bookStore/proxy] ...
Info: action [bookStore/proxy] has been successfully deployed.Info: Deploying action [bookStore/endpoint_post] ...
Info: action [bookStore/endpoint_post] has been successfully deployed.Info: Deploying action [bookStore/endpoint_delete] ...
Info: action [bookStore/endpoint_delete] has been successfully deployed.Info: Deploying action [bookStore/endpoint_get] ...
Info: action [bookStore/endpoint_get] has been successfully deployed.Info: Deploying api [book-club /club/books GET] ...
...
This manifest has simplified the entire deployment process and made it easy to maintain. Now, we are mainly interested in three APIs we just created:
$ wsk api list
ok: APIs
Action Verb API Name URL
/<namespace>/bookStore/endpoin post book-club https://service.us.apiconnect.ibmcloud.com/gws/apigateway/api/5b3edceff7c07077d71813c2b18d1a58c175337c37526b671dd37c0769c5c9a5/club/books
/<namespace>/bookStore/endpoin get book-club https://service.us.apiconnect.ibmcloud.com/gws/apigateway/api/5b3edceff7c07077d71813c2b18d1a58c175337c37526b671dd37c0769c5c9a5/club/books
/<namespace>/bookStore/endpoin delete book-club https://service.us.apiconnect.ibmcloud.com/gws/apigateway/api/5b3edceff7c07077d71813c2b18d1a58c175337c37526b671dd37c0769c5c9a5/club/books
These APIs can be invoked through using curl
or any REST client like its explained in bootcamp.
Good Luck! 👍