• Home
  • >
  • Software Development
  • >
  • Building A Database-Driven Serverless Application on IBM Bluemix – InApps Technology 2022

Building A Database-Driven Serverless Application on IBM Bluemix – InApps Technology is an article under the topic Software Development Many of you are most interested in today !! Today, let’s InApps.net learn Building A Database-Driven Serverless Application on IBM Bluemix – InApps Technology in today’s post !

Read more about Building A Database-Driven Serverless Application on IBM Bluemix – InApps Technology at Wikipedia



You can find content about Building A Database-Driven Serverless Application on IBM Bluemix – InApps Technology from the Wikipedia website

IBM Bluemix and OpenWhisk are not new to InApps Technology readers. We covered various articles covering the overview and architecture of OpenWhisk.

This tutorial helps you configure a Node.js function that is automatically triggered whenever a new row is inserted into a database. We will take advantage of the the integration between Cloudant “DB as a Service” and OpenWhisk “Functions as a Service” in IBM Bluemix PaaS.

Let’s assume we have a database to capture sales orders. Each time a sales manager closes a deal, a new row is added to the table. Our goal is to send out an email alert if the order value is beyond a specific threshold.

Since we are building the notification engine as a serverless application, it is possible for us to extend the alert delivery as a SMS, mobile push notification, or even a Slack channel notification.

There are three steps to this tutorial: Configuring the environment, creating the Cloudant NoSQL database, and finally configuring and deploying an OpenWhisk function.

Configuring the Environment

If you haven’t signed up with IBM Bluemix, go ahead and create an account for yourself. The free tier quota is sufficient for completing this tutorial.

Once you have an active Bluemix account, download and configure the CLI with the following commands:

Test the configuration with bx regions command. It should show all the available Bluemix regions.

If Bluemix CLI is working as expected, configure and test the OpenWhisk CLI with the below commands:

Creating the Cloudant Database Instance

Let’s go ahead and create an instance of Cloudant database that acts as the event source for the OpenWhisk functions:

We also need to create a set of credentials for OpenWhisk to talk to Cloudant. Let’s call the credentials key as myapp:

Run the below command and make note of the URL with embedded username and password. We will use that to access the REST API of Cloudant:

Let’s fire cURL to test the connectivity to the database. If it works, we will initialize an environment variable that becomes handy for rest of the operations. Please download and install JQ, the command line tool to deal with JSON documents:

Let’s check the availability of databases in Cloudant:

The list is empty because we haven’t created the database yet. Run the below commands to create a collection called sales:

Verify that the document is created, and then delete it. Make sure you are capturing the document id and the revision id before attempting the delete operation:

Creating OpenWhisk Function to Send Email Notification

IBM Cloud Functions, the commercial implementation of OpenWhisk comes with pre-defined packages that are integrated with Bluemix PaaS services such as Cloudant, Weather data, Watson Translator, and more. Each package comes with a set of actions, triggers, and feeds to enable easier integration with Serverless environment.

Run the below command to see all the pre-defined packages:

This command shows the details of Cloudant-specific package:

Since we already created the instance of the Cloudant database in the previous step, we can access the package that’s specific to our database named salesdb:

We are all set to wire the triggers and actions associated with salesdb with our application. Let’s start by creating the trigger for the changes that occur to the database:

With the trigger in place, we can now create an action to send an email with the order details. Save the below contents to mail.js. We are using SendGrid for sending outbound emails. Refer to this tutorial for further details on using SendGrid with OpenWhisk.

Create an OpenWhisk Action based on the above function:

We can quickly test the action by passing dummy parameters. Running the below command should send an email to the address mentioned in the function:

In OpenWhisk, it is possible to chain multiple actions to create a sequence. This is similar to Unix Pipes where the output of one command is passed to the next. Since Cloudant already comes with an action to read the database, we will daisy-chain it to the high-value-order action that we created in the previous step:

It’s time for us to wire up the function with the database trigger, which is invoked when data is inserted. We do this by creating a rule that connects the action with the Cloudant trigger:

We are almost done with the required configuration. Let’s insert data with the order value greater than 10000. This should trigger the email notification:

Apart from waiting for the email to get delivered, you can also check the output of the invocation with the below commands. It should show the output as “Message Sent”:

You can easily extend this scenario to send a SMS by creating a new action based on Twilio.

Feature image via Pixabay.



Source: InApps.net

Rate this post
As a Senior Tech Enthusiast, I bring a decade of experience to the realm of tech writing, blending deep industry knowledge with a passion for storytelling. With expertise in software development to emerging tech trends like AI and IoT—my articles not only inform but also inspire. My journey in tech writing has been marked by a commitment to accuracy, clarity, and engaging storytelling, making me a trusted voice in the tech community.

Let’s create the next big thing together!

Coming together is a beginning. Keeping together is progress. Working together is success.

Let’s talk

Get a custom Proposal

Please fill in your information and your need to get a suitable solution.

    You need to enter your email to download

      [cf7sr-simple-recaptcha]

      Success. Downloading...