Serverless workshop: Build an app with Apache OpenWhisk and CouchDB

Upkar Lidder
Lisa Jung



> ulidder@us.ibm.com
> @lidderupk
> upkar.dev

Prerequisites

@lidderupk
IBM Developer

1. Create IBM Cloud Account using THIS URL

3. If you already have an account, use the above URL to sign into your IBM Cloud account.

2. Check your email and activate your account. Once activated, log back into your IBM Cloud account using the link above.

4. OPTIONAL - Download Postman to test the API - https://www.getpostman.com/

Workshop - Goals

@lidderupk
IBM Developer
Create an API using OpenWhisk, and CouchDB.
Optionally, add authentication with API Gateway.
@lidderupk
IBM Developer

What is Serverless

Serverless computing refers to the concept of building and running applications that do not require server management.

 

It describes a finer-grained deployment model where applications, bundled as one or more functions, are uploaded to a platform and then executed, scaled, and billed in response to the exact demand needed at the moment.

 

It refers to the idea that consumers of serverless computing no longer need to spend time and resources on server provisioning, maintenance, updates, scaling, and capacity planning. Instead, all of these tasks and capabilities are handled by a serverless platform and are completely abstracted away from the developers​.

Cloud Native Computing Foundation

https://github.com/cncf/wg-serverless/tree/master/whitepapers/serverless-overview

@lidderupk
IBM Developer

Use Cases

  • Secure and scalable APIs
    • API Gateway Function
    • HTTP level scaling

 

  • Backend services / micro services
    • function/service level scaling
    • polyglot functions

 

  • Event Driven Programming
    • Changes in database
    • Social events
@lidderupk
IBM Developer

Serverless Landscape

Hosted

Installable

https://github.com/cncf/landscape#serverless

@lidderupk
IBM Developer

Apache OpenWhisk

Apache OpenWhisk is an open source, distributed Serverless platform that supports a programming model in which developers write functional logic (called Actions), in any supported programming language, that can be dynamically scheduled and run (with Rules) in response to associated events (via Triggers) from external sources ( Feeds) or from HTTP requests.

@lidderupk
IBM Developer

Apache OpenWhisk

Packages
@lidderupk
IBM Developer

Community and Help

  • Slack
  • http://openwhisk-team.slack.com/

 

 

  • Github and OpenWhisk docs

 

 

 

  • Youtube/Medium/Twitter

 

 

  • Bi-weekly interexchange meetings
@lidderupk
IBM Developer

Some things to 🤔

  • Functions are stateless. Need some sort of persistence between runs.
  • Are you able to test and develop locally ? Does provider have CLI ?
  • Can you easily version your functions ? Source control ?
  • Can you easily monitor your functions ?
  • Security and API gateway
  • Avoid long-running loops / mini-monoliths ?
  • Latency (cold, warm and hot loads)
  • How do you track dependencies ?
@lidderupk
IBM Developer
@lidderupk
IBM Developer

💕

Document Database

@lidderupk
IBM Developer

Apache CouchDB is a JSON document database

@lidderupk
IBM Developer
@lidderupk
IBM Developer

What is this NoSQL - it's not SQL for sure 🤣

@lidderupk
IBM Developer

Person table in RDBMS

@lidderupk
IBM Developer

Person in RDBMS 👉🏽 Person in Cloudant

Apache CouchDB - What is a document ?

@lidderupk
IBM Developer

Apache CouchDB - what is a database ?

@lidderupk
IBM Developer
@lidderupk
IBM Developer

http://nosql-database.org/

> 255 NoSQL databases

https://en.wikipedia.org/wiki/NoSQL

Are there any others ? - oh yea ! LOTS 🖖🏽

 👀

Workshop - Goals

@lidderupk
IBM Developer
Create an API using OpenWhisk, and CouchDB.
Optionally, add authentication with API Gateway.
@lidderupk
IBM Developer

Steps

@lidderupk
IBM Developer
  1. Sign up / Log into IBM Cloud - http://bit.ly/openwhisk-ibm
  2. Create a Cloudant Service and a cats database
  3. Create a an action for each of
    • ​CREATE
    • READ
    • UPDATE
    • DELETE
  4. Create an API Gateway and expose each action
  5. Test the API using POSTMAN or CURL or API Gateway

Step 1 - sign up/ log into IBM Cloud

@lidderupk
IBM Developer

Create Data Service

@lidderupk
IBM Developer

Step 2.1 - create Cloudant service

@lidderupk
IBM Developer

Step 2.2 - create Cloudant service

@lidderupk
IBM Developer

Step 2.3 - launch Cloudant service from Resource List

@lidderupk
IBM Developer

Step 3.1 - create new credentials

@lidderupk
IBM Developer

Step 3.2 - create new credentials

@lidderupk
IBM Developer

Step 3.3 - note down credentials for later

@lidderupk
IBM Developer

Step 4 - launch Cloudant dashboard

@lidderupk
IBM Developer

Step 5 - create new database

@lidderupk
IBM Developer

Create Functions

@lidderupk
IBM Developer

Step 6.1 - create functions back on the first tab!

@lidderupk
IBM Developer

Step 6.2 - create functions!

@lidderupk
IBM Developer

Step 7.1 - create POST CAT action

@lidderupk
IBM Developer

Step 7.2 - create POST CAT action

@lidderupk
IBM Developer

Step 7.3 - create and save POST CAT action

@lidderupk
IBM Developer

https://github.com/lidderupk/ibm-cloud-functions-serverless-apis/blob/master/actions/cat-post-action/index.js

Step 7.4 - add parameters to POST CAT action

@lidderupk
IBM Developer

Step 7.5 - test POST CAT action

@lidderupk
IBM Developer

Step 7.6 - test POST CAT action

@lidderupk
IBM Developer

Step 7.7 - see new cat created in Cloudant

@lidderupk
IBM Developer

Step 8 - go back to actions home page

@lidderupk
IBM Developer

Step 9.1 - create GET CAT action

@lidderupk
IBM Developer

Step 9.2 - create GET CAT action

@lidderupk
IBM Developer

Step 9.3 - create GET CAT action

@lidderupk
IBM Developer

Step 9.4 - add parameters to GET CAT action

@lidderupk
IBM Developer

Step 9.5 - create and save GET CAT action

@lidderupk
IBM Developer

https://github.com/lidderupk/ibm-cloud-functions-serverless-apis/blob/master/actions/cat-get-action/index.js

Step 9.6 - test GET CAT action

@lidderupk
IBM Developer

Step 10.1 - create DELETE CAT action

@lidderupk
IBM Developer

Step 10.2 - create DELETE CAT action

@lidderupk
IBM Developer

Step 10.3 - create DELETE CAT action

@lidderupk
IBM Developer

Step 10.4 - create and save DELETE CAT action

@lidderupk
IBM Developer

https://github.com/lidderupk/ibm-cloud-functions-serverless-apis/blob/master/actions/cat-delete-action/index.js

Step 10.5 - add parameters to DELETE CAT action

@lidderupk
IBM Developer

Step 10.6 - test DELETE CAT action WITHOUT id

@lidderupk
IBM Developer

Step 10.7 - test DELETE CAT action WITH id

@lidderupk
IBM Developer

Step 10.8 - test DELETE CAT action WITH id

@lidderupk
IBM Developer

Step 10.9 - test DELETE CAT action WITH id

@lidderupk
IBM Developer

Step 10.10 - test DELETE CAT action WITH id

@lidderupk
IBM Developer

Step 11.1 - create PUT CAT action

@lidderupk
IBM Developer

Step 11.2 - create PUT CAT action

@lidderupk
IBM Developer

Step 11.3 - create PUT CAT action

@lidderupk
IBM Developer

Step 11.4 - add parameters to PUT CAT action

@lidderupk
IBM Developer

Step 11.5 - create and save PUT CAT action

@lidderupk
IBM Developer

https://github.com/lidderupk/ibm-cloud-functions-serverless-apis/blob/master/actions/cat-put-action/index.js

Step 11.6 - find an id to test PUT CAT action

@lidderupk
IBM Developer

Step 11.7 - test PUT CAT action with new name and color

@lidderupk
IBM Developer

Step 11.8 - test PUT CAT action with new name and color

@lidderupk
IBM Developer

Step 11.9 - check if change was made in cloudant

@lidderupk
IBM Developer

We are done with Actions!

@lidderupk
IBM Developer

APIs

@lidderupk
IBM Developer

Step 12 - let's work with the API now!

@lidderupk
IBM Developer

Step 12.1 - create a new API

@lidderupk
IBM Developer

Step 12.2 - create cats-api with base path /cats-api

@lidderupk
IBM Developer

Step 12.3 - map cat-get action to GET /cat

@lidderupk
IBM Developer

Step 12.4 - map cat-post action to POST /cat

@lidderupk
IBM Developer

Step 12.5 - map cat-delete action to DELETE /cat

@lidderupk
IBM Developer

Step 12.6 - map cat-put action to PUT /cat

@lidderupk
IBM Developer

All operations are done!

@lidderupk
IBM Developer

Step 12.7 - save your API

@lidderupk
IBM Developer

Step 13.1 - test GET /cat API

@lidderupk
IBM Developer

Step 13.2 - test POST /cat API

@lidderupk
IBM Developer

Step 13.3 - see new cat in Cloudant!

@lidderupk
IBM Developer

Step 13.4 - your turn. Test the DELETE and PUT ACTIONS

@lidderupk
IBM Developer

Step 14.1 - add authentication. Remember to SAVE!

@lidderupk
IBM Developer

Step 14.2 - generate a new key for "sharing outside org"

@lidderupk
IBM Developer

Step 14.3 - test using postman or CURL

@lidderupk
IBM Developer
[9:02:07] ulidder:serverless git:(master*) $ curl --request GET \
  --url https://bd17e6ce.us-south.apiconnect.appdomain.cloud/cats-api/cat \
  --header 'accept: application/json' \
  --header 'x-ibm-client-id: Client ID'
{"status":401,"message":"Error: Unauthorized"}
[10:35:13] ulidder:serverless git:(master*) $ curl --request GET \
  --url https://bd17e6ce.us-south.apiconnect.appdomain.cloud/cats-api/cat \
  --header 'accept: application/json' \
  --header 'x-ibm-client-id: 819c401d-5d86-4c79-996c-e76ea229cefa'


{
  "body": {
    "_id": "c4d98db9ebd692116472ed00fc007847",
    "_rev": "1-14f0009b0af8d840b9d57ac17dc1e394",
    "color": "black",
    "name": "kitty"
  },
  "headers": {
    "Content-Type": "application/json"
  },
  "statusCode": 200
}

Without Authentication

With Authentication

[9:02:07] ulidder:serverless git:(master*) $ curl --request GET \
  --url https://bd17e6ce.us-south.apiconnect.appdomain.cloud/cats-api/cat \
  --header 'accept: application/json' \
  --header 'x-ibm-client-id: Client ID'



{"status":401,"message":"Error: Unauthorized"}
@lidderupk
IBM Developer

So why use serverless and API Gateway?

What if there is a surge of new cats from Australia ?

CAT-POST is scaled as needed !

@lidderupk
IBM Developer
@lidderupk
IBM Developer

I want to refactor GET and DELETE  to Python to use a cool pip package !

Refactor one function/action at a time !

@lidderupk
IBM Developer

I want to switch out my email service provider !

Plug and Play model. Only change send_email action !

@lidderupk
IBM Developer

I want to onboard more clients to my API

Use API Gateway to set up sepearate keys or use your own authentication !

IBM Code Patterns - serverless

@lidderupk
IBM Developer

Full workshop - serverless with couchDB

@lidderupk
IBM Developer

Thank you

 

Let's chat !

@lidderupk
IBM Developer

Upkar Lidder, IBM

 

@lidderupk

https://github.com/lidderupk/

ulidder@us.ibm.com

@lidderupk
IBM Developer

Apache OpenWhisk and Apache CouchDB

By Upkar Lidder

Apache OpenWhisk and Apache CouchDB

  • 580