OpenWhisk Action Running “npm install” for You
Problem Statement:
OpenWhisk has a limitation in creating Node.js6/8 actions which are dependent on external third party modules. We have to install such modules locally on the client before creating a new action and package them as part of the action source. For example, we want to create a helloworld
action in Node.js6 runtime. Our helloworld
action is using NPM module calledstring-format
which is not available in Node.js6 runtime container (created using openwhisk/nodejs6action
docker image). Our sample action source has two files:
$ cd nodejs/actions/helloworld
$ ls -1
index.js
package.json
index.js contains:
package.json contains:
Now, we have to install string-format
locally and package its source along with index.js
and package.json
in a zip file called helloworld.zip
:
$ cd nodejs/actions/helloworld
$ npm install --production
$ ls -1
index.js
node_modules/
package.json
$ zip -rq helloworld.zip *
Create an action using helloworld.zip
file:
$ wsk action create helloworld --kind nodejs:6 helloworld.zip
$ wsk action invoke helloworld -r --blocking --param name Amy
{
"message": "Hello, Amy!"
}
Solution:
Now, this is a bit lengthy process to create an action which has dependency on third party modules and that’s the reason, we decided to innovate and create an OpenWhisk action build/nodejs which can install and package external modules on server side for us before creating an intended action.
Let me first show you how to create helloworld
using build/nodejs
(assuming build/nodejs
is part of your OpenWhisk server):
$ cd nodejs/actions/helloworld
$ ls -1
index.js
package.json
$ zip -rq helloworld.zip index.js package.json
$ wsk action invoke build/nodejs --blocking --param action_name helloworld --param action_data `cat helloworld.zip | base64`
Here, helloworld
is using node module string-format
which was not available before but was installed by build/nodejs
as part of action creation. Test helloworld
with:
$ wsk action invoke helloworld -r --blocking --param name Amy
{
"message": "Hello, Amy!"
}
Pros:
- This solution simplifies action creation by installing third party modules and packaging them with an intended action on the server.
- The NPM modules which are installed once by
build/nodejs
are available for later use by any other Node.js actions. This reduces action creation time for the subsequent actions by a few seconds, as it doesn’t have to install the same package again. - This approach reduces the file size of a zip file as third party modules are not packaged with action files. This helps speeding up deployments and most importantly stays within the size limit of 48MB for action source code.
Installation:
This install script can help you deploy build/nodejs
on your own OpenWhisk server. Run:
./install.sh <authkey> <edgehost> <apihost>
Where, <authkey> is admin authentication key with access to whisk.system, <edgehost> and <apihost> are hostname/IP of OpenWhisk server where respective services are running.
You can experiment build/nodejs
yourself! Checkout this automation here in GitHub. Feel free to leave your comment below. Enjoy! 👍