Cloud Functions: Easy Database Setup the Serverless Way

Share this post:

A tutorial I wrote, featuring a database-backed Slack chatbot, is now live. It uses Db2 as database system to store event data. The client accessing the database is written in Node.js and is implement with IBM Cloud Functions in a serverless way. During the development of that tutorial I faced the question on how to perform the database setup. Should I guide users through the user interface to create a table and insert data? Should they install a Db2 client and execute a script locally? I solved the problem in a serverless fashion. Here are the details.

Slack Chatbot Architecture

Slack Chatbot Architecture

Setup using a Cloud Functions action

Deploying an app to the cloud often involves setting up, initializing or cleaning up services. With an app server, there could be an extra routine executed or API to be called. However, in a serverless environment, e.g., using IBM Cloud Functions, this is not possible. Thus, why not stick to the theme and do it in a serverless fashion? It avoids extra steps to be performed by the tutorial user or handling extra software. Utilizing Cloud Functions has the extra benefit that it is quite simple to implement. Many runtime environments have several database drivers readily available.

To see which database-related packages or modules are already included in the Node.js, Python, PHP, or Swift environments, check out the IBM Cloud Functions system details. They include drivers for IBM Db2 and IBM Informix, MySQL, Cloudant and CouchDB, MongoDB. If that is not enough, you have the option to use a Docker container for an action.

To initialize a Db2 Warehouse on Cloud service as done in the tutorial, the following steps were necessary.

Setup Function

Implement a function, e.g., in Node.js or Python. The code for my Db2 setup action in Node.js is available on Github. The function (or “action”) has parameters that determine whether to create a table, to insert data or to drop that table. The Node.js driver for Db2 is part of the Node.js environment, importing the module “ibm_db” is all that needs to be done to use the driver.

function db2Setup(dsn, mode) {
try {
var tabledef="create table events "+
"(eid int not null generated always as identity (start with 1000, increment by 1),"+
"shortname varchar(20) not null,"+
"location varchar(60) not null,"+
"begindate timestamp not null,"+
"enddate timestamp not null,"+
"contact varchar(255) not null);";
var sampledata="insert into events(shortname,location,begindate,enddate,contact) values('Think 2018','Las Vegas','2018-03-19 00:00:00','2018-03-22 23:59:00',''),('IDUG2018','Philadelphia','2018-04-29 00:00:00','2018-05-03 23:59:00','');"
var tabledrop="drop table events;"
var conn=ibmdb.openSync(dsn);
if (mode=="setup")
var data=conn.querySync(tabledef);
} else if (mode=="sampledata")
var data=conn.querySync(sampledata);
} else if (mode=="cleanup")
var data=conn.querySync(tabledrop);
return {result : data};
} catch (e) {
return { dberror : e }

Create and Execute Action

Create an action, bind the credentials to the Db2 service, then invoke the actions with the right parameters. See the setup script on Github.

# create package for all the actions
bx wsk package create slackdemo

# create action for setup / cleanup using Node.js 8 environment
bx wsk action create slackdemo/db2Setup db2-setup.js --kind nodejs:8

# bind Db2 credentials to action
bx wsk service bind dashDB slackdemo/db2Setup --instance eventDB

# invoke action for database setup and initialization with sample data
bx wsk action invoke slackdemo/db2Setup -p mode "[\"setup\"]" -r
bx wsk action invoke slackdemo/db2Setup -p mode "[\"sampledata\"]" -r

Closing Remarks

The tutorial using the above for building a Slack chatbot to interface with Db2 is part of the IBM Cloud solution tutorials. It has all the steps and code to get your slackbot with Db2 quickly up and running.

If you have feedback, suggestions, or questions about this post, please reach out to me on Twitter (@data_henrik) or LinkedIn.

Technical Offering Manager / Developer Advocate

More How-tos stories
May 3, 2019

Kubernetes Tutorials: 5 Ways to Get You Building Fast

Ready to start working with Kubernetes? Want to build your Kubernetes skills? The five tutorials in this post will teach you everything you need to know about how to manage your containerized apps with Kubernetes.

Continue reading

May 3, 2019

Using Portworx to Deploy and Manage an HA MySQL Cluster on IBM Cloud Kubernetes Service

This tutorial is a walkthrough of the steps involved in deploying and managing a highly available MySQL cluster on IBM Cloud Kubernetes Service.

Continue reading

May 2, 2019

Kubernetes v1.14.1 Now Available in IBM Cloud Kubernetes Service

We are excited to announce the availability of Kubernetes v1.14.1 for your clusters that are running in IBM Cloud Kubernetes Service. IBM Cloud Kubernetes Service continues to be the first public managed Kubernetes service to support the latest upstream versions from the community.

Continue reading