Easy Database Setup the Serverless Way

DZone 's Guide to

Easy Database Setup the Serverless Way

Interested in bringing the cloud to your database-backed solution? Let's see how you can bring the power of serverless computing to bear.

· Database Zone ·
Free Resource

A tutorial I wrote, featuring a database-backed Slack chatbot, is now live. It uses Db2 as the database system to store event data. The client accessing the database is written in Node.js and is implemented with IBM Cloud Functions in a serverless way. During the development of that tutorial, I faced the question of 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.Database-backed Slackbot

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 an 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 for the tutorial user as well as avoiding 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 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, CouchDB, and MongoDB. If that is not enough, you can use a Docker container as action.

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

Implement Your Function

First, you have to 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','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');"
        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, Bind, and Execute the 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
bx wsk package create slackdemo

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

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

# invoke actions to create table, then insert 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 setup to build a Slack chatbot to interface with Db2 is part of the IBM Cloud solution tutorials. It has all the steps and code needed 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.

database ,serverless ,ibm cloud functions ,db2 ,tutorial

Published at DZone with permission of Henrik Loeser . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}