Cloud Functions: Easy Database Setup the Serverless Way

By Henrik Loeser

Cloud Functions: Easy Database Setup the Serverless Way

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) {<br>
 try {<br>
    var tabledef="create table events "+<br>
                 "(eid int not null generated always as identity (start with 1000, increment by 1),"+<br>
                  "shortname varchar(20) not null,"+<br>
                  "location varchar(60) not null,"+<br>
                  "begindate timestamp not null,"+<br>
                  "enddate timestamp not null,"+<br>
                  "contact varchar(255) not null);";<br>
    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','https://www.ibm.com/events/think/'),('IDUG2018','Philadelphia','2018-04-29 00:00:00','2018-05-03 23:59:00','http://www.idug.org/na2018');"<br>
    var tabledrop="drop table events;"<br>
    var conn=ibmdb.openSync(dsn);<br>
    if (mode=="setup")<br>
    {<br>
        var data=conn.querySync(tabledef);<br>
    } else if (mode=="sampledata")<br>
    {<br>
      var data=conn.querySync(sampledata);<br>
    } else if (mode=="cleanup")<br>
    {<br>
      var data=conn.querySync(tabledrop);<br>
    }<br>
    conn.closeSync();<br>
    return {result : data};<br>
 } catch (e) {<br>
     return { dberror : e }<br>
 }<br>
}<br>

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.

<br>
# create package for all the actions<br>
bx wsk package create slackdemo<p></p>
<p># create action for setup / cleanup using Node.js 8 environment<br>
bx wsk action create slackdemo/db2Setup db2-setup.js --kind nodejs:8</p>
<p># bind Db2 credentials to action<br>
bx wsk service bind dashDB slackdemo/db2Setup --instance eventDB</p>
<p># invoke action for database setup and initialization with sample data<br>
bx wsk action invoke slackdemo/db2Setup -p mode "[\"setup\"]" -r<br>
bx wsk action invoke slackdemo/db2Setup -p mode "[\"sampledata\"]" -r </p>

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.

Be the first to hear about news, product updates, and innovation from IBM Cloud