OW Skeleton Interface for Node-RED flows as functions
This flow implements the necessary interface needed by a Node-RED flow, in order to be executed as a function inside Openwhisk . Openwhisk can execute in a serverless manner any type of Docker container, as long as it implements two methods on port 8080.
- A
POST /init
, used in the typical OW runtime images (such as node.js etc) to retrieve the function code from the OW function DB. In the Node-RED function runtime this is not needed for functional purposes, since the code is already embedded in the flow, but it is needed in order to abide by the OW interface that expects this method. It needs to be stressed that in a warm execution (reuse of existing container from previous same function execution), the /init method is not re-executed. Hence any needed initialization per invocation should not be performed inside this method. - A
POST /run
method, which is called upon invocation of the function. Any parameters used are visible in Node-RED in themsg.payload.value.<param_name>
field
The flow includes also a hello world function example, getting the name for hello from the input parameters as an example.
In order to create a relevant image for execution, an example Dockerfile appears below. Starting from the official Node-RED image, we can install any extra needed Node-RED packaged nodes from npm
, used in an arbitrary flow. Then copy the local myflow.json
flow file into the /data directory of the container (default location used by the official Node-RED image for the workDir) and optionally copy any specific settings.js
or flows_cred.json
files needed. If we need to port credentials, the credentialSecret
should be set and used during credential creation in our Node-RED development server, and then move also the relevant settings.js
file in the newly built image. Finally, we shift the port used by Node-RED to 8080.
FROM nodered/node-red
RUN npm install <any needed node-red packaged node for your flows from npm>
#move arbitrary flow from local dir
COPY myflow.json /data/flows.json
#optional: add a specific settings.js file
COPY settings.js /data/settings.js
#optional: add a specific flows_cred.js file with credentials. If #used then the credentialSecret
#option should be set in the settings.js file so that credentials #are transferable to the new image
COPY flows_cred.json /data/flows_cred.json
ENV PORT 8080
EXPOSE 8080:8080
Then the image can be built (in the same dir):
docker build -t <docker_registry_account>/<image_name>
.
and pushed:
docker push <docker_registry_account>/<image_name>
Following the creation of the image and its pushing to a registry, it can be registered in Openwhisk as an action through the following command:
wsk action create <action_name> --docker <docker_registry_account>/<image_name>
and invoked through:
wsk action invoke <action_name> --param name george
A readymade image that can be used containing the hello world flow can be found here: