Thursday, February 15, 2018

Easy Database Setup the Serverless Way

Serverless Slackbot with Db2
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.



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. To initialize a Db2 Warehouse on Cloud service, the following steps were necessary:
  • 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.
  • Create an action, bind the credentials to the Db2 service, then invoke the actions with the right parameters. See the setup script on Github.
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.