Tutorial for the OpenFaaS CLI with Node.js

    What is OpenFaas?

    OpenFaaS is a framework for packaging code, binaries or containers as Serverless functions on any platform - Windows or Linux. Visit website

    In the next few minutes we'll:

    • Create a function from a code template
    • Build the function as a Docker image
    • Push the image to a Docker registry
    • Deploy the function
    • Invoke the function

    ..and more

    Before starting you should setup OpenFaaS on your laptop or cluster using a deployment guide:

    If you're not sure which to pick - you can deploy and setup Docker Swarm in around 60 seconds.

    Get the CLI

    For the latest version of the CLI type in the following:

    Find the help page

    All the commands have a —help flag available which provides documentation on usage:

    1. $ faas-cli --help
    2.  
    3. Manage your OpenFaaS functions from the command line
    4.  
    5. Usage:
    6. faas-cli [flags]
    7. faas-cli [command]
    8.  
    9. Available Commands:
    10. build Builds OpenFaaS function containers
    11. deploy Deploy OpenFaaS functions
    12. help Help about any command
    13. push Push OpenFaaS functions to remote registry (Docker Hub)
    14. version Display the clients version information
    15.  
    16. Flags:
    17. -h, --help help for faas-cli
    18. -f, --yaml string Path to YAML file describing function(s)
    19.  
    20. Use "faas-cli [command] --help" for more information about a command.
    1. $ faas-cli new callme --lang node
    2. Folder: callme created.
    3. Function created in folder: callme
    4. Stack file written: callme.yml

    You'll see the following was created:

    1. $ find . | grep callme
    2. ./callme
    3. ./callme/handler.js
    4. ./callme/package.json
    5. ./callme.yml

    Here's the YAML file which was generated:

    The contents of callme.yml can now be used with the CLI to save on typing and build, push, deploy and invoke your function.

    If your cluster is remote or not running on port 8080 - then edit this in the YAML file before continuing.

    A handler.js file was generated for your function which looks like this:

    1. "use strict"
    2.  
    3. module.exports = (context, callback) => {
    4. callback(undefined, {status: "done"});
    5. }

    It will just send back a status of "done" when called.

    What about modules etc?

    You can edit the package.json file and your dependencies will be installed during the "build" step. The same works for the other language templates with a requirements.txt file for Python etc.

    Build the function

    The local Docker client is used to build your function into a Docker image.

    1. $ faas-cli build -f callme.yml
    2. Building: callme.
    3. Clearing temporary build folder: ./build/callme/
    4. Preparing ./callme/ ./build/callme/function
    5. Building: callme with node template. Please wait..
    6. docker build -t callme .
    7. Sending build context to Docker daemon 8.704kB
    8. Step 1/19 : FROM node:6.11.2-alpine
    9. ...
    10.  
    11. Step 19/19 : CMD fwatchdog
    12. ---> Running in 53d04c1631aa
    13. ---> f5e1266b0d32
    14. Removing intermediate container 53d04c1631aa
    15. Successfully built f5e1266b0d32
    16. Successfully tagged callme:latest
    17. Image: callme built.

    Push your function

    It is recommended to change the "tag" in the image field after each change, but by default OpenFaaS will attempt to pull your function from the Docker Hub or a remote container registry. This enables you to iterate on your functions quickly without changing their Docker "tag".

    Now edit the YAML file and set the "image" line to your username on the Docker Hub such as: alexellis/callme. Then build the function again.

    1. $ faas-cli push -f callme.yml
    2. Pushing: callme to remote repository.
    3. The push refers to a repository [docker.io/alexellis/callme]
    4. ...

    Once the image is pushed up to the Docker Hub or another remote Docker registry we can deploy and run the function.

    If an existing / old function was already deployed then it will be removed first.

    Invoke the function

    We can now invoke the function:

    1. $ faas-cli invoke -f callme.yml callme
    2. Reading from STDIN - hit (Control + D) to stop.
    3. This is my message
    4.  
    5. {"status":"done"}

    You can also pipe a command into the function.

    1. $ date | faas-cli invoke -f callme.yml callme
    2. {"status":"done"}

    Task: Can you edit the function so that it returns the input along with "status": "done"? Just edit the code, run "build", "push" and "deploy" - then you're good to invoke it again.

    List the functions

    You can list your functions and find out how many replicas they have and also the invocation count like this:

    1. $ faas-cli list
    2. Function Invocations Replicas
    3. inception 0 1
    4. callme 2 1
    5. func_hubstats 0 1
    6. tensorflow 0 1
    7. func_echoit 0 1
    8. func_nodeinfo 0 1

    Tip: If you pass —verbose then you'll also see the image name

    You can remove functions by using the CLI. If you specify a YAML file the CLI will remove all the deployed functions listed in the file.

    Type in faas-cli rm —help for more information.

    That concludes the coffee break - we just built and deployed our first Serverless Node.js OpenFaaS function - "Look Ma! No UI!" You can find help for any of the commands by passing in the parameter.