Sunday, January 1, 2017

IBM Bluemix, OpenWhisk, Docker, Python, and Twilio app to start with serverless computing on IBM Cloud

Once Forbes starts to cover serverless computing[1] you know that it is time to begin paying attention. Today, there are many frameworks that can help you get started with serverless computing, for example OpenWhisk[2], AWS Lambda, and Google Cloud Functions.
This post will help you build a simple but useful serverless computing application with OpenWhisk on IBM Cloud. The app is implemented using Python with Flask and can help you send text messages via a Twilio SMS API[3].
If you would like to skip the introductions and geek out with the code, you can access it from the following github repository: Otherwise, read on.
So why OpenWhisk? One reason is that it stands out based on its elegant, Docker-based architecture that enables a lot more flexibility than competing frameworks from AWS and Google. For example, AWS Lambda forces developers to choose between Python, Java, or JavaScript[4] for the implementation of the serverless computing functions. Google Cloud Functions are JavaScript only and must be packaged as Node.js modules[5].
OpenWhisk's use of Docker means that any server side programming language supported by Docker can be used for serverless computing. This is particularly important for organizations that target hybrid clouds, environments where legacy, on-premise code needs to be integrated with code running in the cloud. Also, since Docker is a de facto standard for containerizing applications, serverless computing developers don't need to learn yet another packaging mechanism to build applications on IBM Cloud.
You can use the sample app described in this post to figure out whether OpenWhisk works for you.
The post will walk you through the steps to clone existing Python code and package it as a Docker image. Once the image is in Docker Hub, you will create an OpenWhisk action[6] that knows how to launch a Docker container with your code. To send a text message, you will use OpenWhisk's command line interface to pass it the text message contents. In response, OpenWhisk instantiates the Docker container holding the Python app which connects to Twilio's text messaging service and sends an SMS.

Before you start

OpenWhisk serverless computing environment is hosted on IBM Bluemix. To sign up for a 30 day trial Bluemix account register here:
This app uses Twilio for text messaging capabilities. To sign up for a Twilio account visit: Make sure that once you have a Twilio account, you also obtain the account SID, authentication token, and register a phone number with an SMS capability.
OpenWhisk uses Docker Hub to execute Docker based actions. You will need a Docker Hub account; to sign up for one use:
NOTE: To make it easier to use the instructions, export your various account settings as environment variables:
  • your Docker Hub username as DOCKER_USER
  • your Twilio Account SID as TWILIO_SID
  • your Twilio Auth Token as TWILIO_TOKEN
  • your Twilio SMS capable phone number as TWILIO_NUMBER
export DOCKER_USER=''
export TWILIO_SID=''
export TWILIO_TOKEN=''

Clone the OpenWhisk action implementation

The OpenWhisk action is implemented as a Python Flask application which is packaged as a Docker image and published to Docker Hub. You can clone the code for the action from github by running the following from your command line

git clone
This will create an openwhisk-python-twilio folder in your current working directory.
All of the code for the OpenWhisk action is in the py/ file. There are two functions, called init and run that correspond to Flask app routes /init and /run. The init function is called on an HTTP POST request and returns an HTTP 200 status code as expected by the OpenWhisk platform. The run function verifies that an incoming HTTP POST request is a JSON document containing Twilio configuration parameters and the content of the text message. After configuring a Twilio client and sending the text message, the function returns back an HTTP 200 status code and a JSON document with a success status message.

Build and package the action implementation in a Docker image

If you don't have Docker installed, it is available per the instructions provided in the link below. Note that if you are using Windows or OSX, you will want to install Docker Toolbox from:
Make sure that your Docker Hub account is working correctly by trying to login using
docker login -u $DOCKER_USER
You will be prompted to enter your Docker Hub password.
Run the following commands to build the Docker image with the OpenWhisk action implementation and to push the image to Docker Hub.

cd openwhisk-python-twilio
docker build -t $DOCKER_USER/openwhisk .
docker push $DOCKER_USER/openwhisk
Use your browser to login to after the docker push command is done. You should be able to see the openwhisk image in the list of your Docker Hub images.

Create a stateless, Docker-based OpenWhisk action

To get started with OpenWhisk, download and install a command line interface using the instructions from the following link:
The following commands need to be executed to configure your OpenWhisk action instance:

wsk action create --docker textAction $DOCKER_USER/openwhisk
wsk action update textAction --param account_sid "$TWILIO_SID" --param auth_token "$TWILIO_TOKEN"
The first command sets up a Docker-based OpenWhisk action called textAction that is implemented using the $DOCKER_USER/openwhisk image from Docker Hub. The second command configures the textAction with the Twilio account SID and authentication token so that they don't need to be passed to the action execution environment on every action invocation.

Test the serverless computing action

Open a dedicated console window and execute

wsk activation poll
to monitor the result of running the OpenWhisk action.
In a separate console, execute the following command, replacing the to value to specify the phone number and the msg value to specify the text message contents:

wsk action invoke --blocking --result -p from "$TWILIO_NUMBER" -p to "867-5309" -p msg "Jenny I got your number" textAction
Upon successful action execution your to phone number should receive the text message and you should be able to see an output similar to the following:
  "status": [
      "success": "true"
      "message_sid": "SM5ecc4ee8c73b4ec29e79c0f1ede5a4c8"