Over a million developers have joined DZone.

Using Composer Packages with OpenWhisk

DZone's Guide to

Using Composer Packages with OpenWhisk

We take a look at how to create and update OpenWhisk actions in PHP via ZIP file by leveraging the power of Composer.

· Cloud Zone ·
Free Resource

Discover a centralized approach to monitor your virtual infrastructure, on-premise IT environment, and cloud infrastructure – all on a single platform.

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 Packagist.org.

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 call from your action, then you have the full power to Guzzle at your fingertips. The ramsey/uuid library is also handy for those times when you need a UUID when PUT'ing to that API.

If you need anything else, then you have to do a bit more leg work.

Uploading Your Own Project Files

The PHP runtime also accepts a ZIP file when creating or updating an action. This means that you can create a PHP project for your action that includes a composer.json.

For example, let's say that you want to use NFNumberToWord in your action.

Create the PHP Project

Firstly, create the composer.json file:

This creates composer.json, composer.lock, and a vendor directory with the component and autoloader code in it.

We now need an action file. This must be called index.php:


The main function takes an associative array of arguments and must return an associative array which is converted to JSON for us. As with any other library, we import it using a use statement and can then instantiate the class and call toWords() on it.

Note that we don't need to include the Composer autoload file, as this is done automatically by the PHP runtime.

Upload to OpenWhisk

To upload to OpenWhisk, we first zip up the files and then use the wsk command line client to create the action:

The name of the ZIP file is immaterial, so I've used something simple. Now we upload it to OpenWhisk:

In this case, I've named the action n2w and passed in the ZIP file we have just created and specified the runtime to use with the --kind parameter. This is needed because we are supplying a ZIP file as our action code and OpenWhisk needs to know which runtime to use for it.

That's it. We're done.

Running Our Action

We can prove it worked using:

This outputs:


As you can see, it's really easy to take advantage of the vast library of Packagist components in your actions, but of course, if all you need is an HTTP client, then Guzzle is already available.

Learn how to auto-discover your containers and monitor their performance, capture Docker host and container metrics to allocate host resources, and provision containers.

composer ,php ,openwhisk actions ,tutorial ,cloud

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}