Developing software in the Real World

Dependency Injection with OpenWhisk PHP

Any non-trivial PHP applications use various components to do its work, from PDO though to classes from Packagist. It's fairly common in a standard PHP application to use Dependency Injection to configure and load these classes when necessary. How do we do this in a serverless environment such as OpenWhisk? This question comes up because we do not have a single entry point into our application, instead we have one entry point per action. If… continue reading.

Using API Gateway with Serverless & OpenWhisk

As with all serverless offerings OpenWhisk offers an API Gateway to provide HTTP routing to your serverless actions. This provides a number of advantages over web actions, the most significant of which are routing based on HTTP method, authentication and custom domains (in IBM Cloud). Creating routes with the wsk CLI To route to an action using API Gateway, you first need to make your action a web action first:

(You can also use… continue reading.

Using Composer with Serverless & OpenWhisk

Every PHP project I write has dependencies on components from Packagist and my Serverless OpenWhisk PHP projects are no different. It turns out that adding Composer dependencies is trivial. Let's create a simple action that converts a number to it's string form. e.g. 123 becomes one hundred and twenty three. We'll start with our simple ow-php-hello project from my earlier article and add a new function to serverless.yml:

Our handler is n2w.main, so we… continue reading.

Using Serverless Framework with OpenWhisk PHP

Serverless Framework is a toolkit to help you mange and deploy a serverless application. (Personally, I'm not a fan of the name as the word "Serverless" already has a meaning in the same space!) It's a useful tool and supports all the major providers, though AWS Lambda seems to be first-among-equals. The OpenWhisk plugin for Serverless is maintained by the rather excellent James Thomas, so if you have any questions, ping him! As I build… continue reading.

Invoking many OpenWhisk actions from another one

I have a project where I need to store a number of items into a data store. I have an OpenWhisk action that stores the items so I wrote an action that takes advantage of the OpenWhisk JS client library to do invoke my store action once for each item in an array that this action receives. This is the JavaScript code that I used:

This is a NodeJS v8 action (–kind nodejs:8 when… continue reading.

Using async/await in OpenWhisk

I'm currently writing an OpenWhisk action in JavaScript that searches Twitter using their API. To do this, I need to get a bearer token from one API endpoint and then call the search endpoint. Disclaimer: I'm in no way a JavaScript expert, so I would love it if you could constructively suggest improvements in the comments! To do this in OpenWhisk, you need to use Promises like this:

Using Composer packages with OpenWhisk

When creating new OpenWhisk actions in PHP, It's likely that you'll want to take advantage of the rich ecosystem of Composer packages on The OpenWhisk PHP runtime has you covered with some pre-installed Composer packages and also the ability to upload your own using a zip file. Pre-installed Composer packages The PHP runtime ships with two Composer packages by default: Guzzle and ramsey/uuid. This is handy as if you need to make an API… continue reading.

CORS and OpenWhisk web actions

By default, OpenWhisk will handle the relevant responses for CORS. Specifically, it will respond to an OPTIONS request with these headers:

If you need to change what is sent or you don't want to send these headers at all, then you need to do set the annotation web-custom-options to true and handle the OPTIONS header yourself. Note that if you don't set this annotation, you must not set any of these headers yourself as… continue reading.

Getting started with Serverless PHP

I've been interested in Apache OpenWhisk for a little while now and recently submitted a new feature to add PHP support to the project. As OpenWhisk is a serverless environment, most users do not run their own copy and instead use a commercial provider with IBMs Bluemix available now along with Adobes I/O Runtime and RedHat coming soon. As a result, my contribution, isn't practically useful until it's in production with a provider. Fortunately, and… continue reading.

Logging in to Bluemix via wsk

To set up the authentication for the OpenWhisk cli tool wsk you do this:

The host and key are provided to from your OpenWhisk supplier. For Bluemix OpenWhisk, you can find it by logging in and then going to the Download OpenWhisk CLI page. To make my life easier, I use a bash function to swap OpenWhisk environments and I documented it in my Switching OpenWhisk Environments article. Log into Bluemix for API Gateway… continue reading.