Database connections are easy with serverless platforms like OpenWhisk. All it takes is packaging up your favorite language-API combo. In this article, we'll provide a simple example using Python and Compose Redis.
One of the big trends in web development in 2017 has definitely been the rise of serverless technology. Adoption is predicted to grow a lot over the next year or two, and plenty of organisations will be solving the same problems around naming, architecture, deployment—and of course, databases. Databases are our specialist subject around here, and they're an important consideration when you're thinking of moving to serverless. Today's post will dispel your worries and includes examples of working with data stores from your IBM Cloud Functions (our serverless application, actually a hosted version of the Apache OpenWhisk project) application.
We can think about accessing external storage in three different ways:
- Calling an API or microservice to get the data we are interested in
- Accessing our existing SQL/NoSQL databases and other datastores
- Having specific packages that integrate with the datastore in question (e.g., the Cloudant package)
All three of these approaches work well with serverless technology. If you're working with an API or a database with an HTTP interface like CouchDB, then you can just use any HTTP client in your chosen programming language (e.g., Requests for Python or Guzzle for PHP). Traditional-style access to existing databases is the most common use case; I'll drill into some examples of that in a moment. Also worth a mention for serverless platforms is that some data sources, such as a file upload, a message on a Kafka queue, or a database change in Cloudant/CouchDB can be used as a trigger to cause the cloud function to run. This can add a new dimension to our applications since they can react to external changes.
Connecting to a Database
Serverless technology is pretty shiny, but not so shiny that we can't do the things we'd expect to be able to do, like connect to a database. For this, we'll just use the library we usually use to connect to our data store. In this example, it's a Python function that fetches some data from a Redis store (if you want to see the whole project, which also doubles as an Alexa skill, the code is on Github).
First, some preamble. We'll create a "package", which is a way of grouping actions and parameters in OpenWhisk. This one is called
pydemo and also needs a parameter
redis_url so that the actions will know where to connect to. We'll achieve this with the IBM Cloud command-line tool with the Cloud Functions plugin enabled—check out their excellent documentation on getting started if any of this is unfamiliar. Here's the command to create the package with the parameter:
bx wsk package create pydemo -p redis_url redis://admin:WXDWEBVJZVKHEUEJ@sl-us-south-1-portal.16.dblayer.com:30206
With the package set up, now the interesting bit: the CODE! This is a function that checks a Redis data store for some airport code information and returns it back to us in a sentence.
from urllib.parse import urlparse import redis def main(args): redis_url = args.get("redis_url", '') parsed = urlparse(redis_url) redis_client = redis.StrictRedis( host=parsed.hostname, port=parsed.port, password=parsed.password, decode_responses=True) code = args['code'] airport1 = redis_client.hgetall("code:" + code) airport_info = "Airport code " + code + " is for " + airport1['name'] + " in " + airport1['country'] output = airport_info return output
The patterns here are repeated regardless of the programming language used; write the code you usually would, but wrap it in a function that the serverless platform will then call. Some libraries are provided by default (this differs between platforms and languages), but the function code along with any dependencies can be packaged into a zip file, and that zip file is deployed as our function source code. For Python, this usually means using a virtualenv. For the code above, my package and deploy steps look like this:
zip -r airport.zip __main__.py virtualenv/lib/python3.6/site-packages/ virtualenv/bin/activate_this.py bx wsk action update pydemo/airport --kind python:3 --web true airport.zip
zip command probably looks familiar but there's that
bx wsk ... command again, so let's look at that in more detail. The command there consists of a few pieces but basically says "now update my python3 action named pydemo/airport with the following code, and web enable it". Perhaps it even says "please".
Compose and Serverless
It's reassuring to know we can keep working with existing, trusted database platforms even as the applications that access them change. Serverless is a rising tide—not just a shiny new buzzword but genuinely a tool that is here to stay in toolboxes across the industry. Being able to easily access our data enables us to experiment and to add some more cloud features to the solid hosted database options we're already using.
attribution Dan Price