Tutorial for the OpenFaaS CLI with Node.js

We'll explore how to create, build, deploy and invoke a brand new function with one of the supported language templates. We'll use the CLI for every part of the workflow.

What is OpenFaas?

CLI with Node.js - 图1

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

Pre-requisites

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

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:

  1. $ curl -sL https://cli.openfaas.com | sudo sh

This is also available via brew install faas-cli on MacOS.

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. remove Remove deployed OpenFaaS functions
  15. version Display the clients version information
  16.  
  17. Flags:
  18. -h, --help help for faas-cli
  19. -f, --yaml string Path to YAML file describing function(s)
  20.  
  21. Use "faas-cli [command] --help" for more information about a command.

Create a new Node.js function

We'll create a brand new Node.js template and a YAML file at the same time which is used by the CLI to save you on typing.

  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:

  1. $ cat callme.yml
  2. provider:
  3. name: faas
  4. gateway: http://localhost:8080
  5.  
  6. functions:
  7. callme:
  8. lang: node
  9. handler: ./callme
  10. image: callme

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.

Tip: You can also create your handler.js file manually

What about npm 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. ---> 16566b7ed19e
  10. ...
  11.  
  12. Step 19/19 : CMD fwatchdog
  13. ---> Running in 53d04c1631aa
  14. ---> f5e1266b0d32
  15. Removing intermediate container 53d04c1631aa
  16. Successfully built f5e1266b0d32
  17. Successfully tagged callme:latest
  18. 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".

If you are using a single-node Docker Swarm cluster on your laptop then you can skip this step. If you are using minikube or a single-node Kubernetes cluster then you can set the ImagePullPolicy to "IfNotPresent" in the helm chart for the Kubernetes controller, this will allow you to work with images within your library.

Now edit the callme.yml 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.

Deploy the function

  1. $ faas-cli deploy -f callme.yml
  2. Deploying: callme.
  3. No existing service to remove
  4. Deployed.
  5. 200 OK
  6. URL: http://localhost:8080/function/callme

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

If you get an error at this point then please make sure you followed the pre-requisites.

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

Remove the function

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.

  1. $ faas-cli rm -f callme.yml
  2. Deleting: callme.
  3. Removing old service.

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

Wrapping up

Did you know we also have a UI and Prometheus metrics built-into the stack? Head over to the GitHub repo to read more and to Star the repository.

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 —help parameter.