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:
- $ faas-cli --help
- Manage your OpenFaaS functions from the command line
- Usage:
- faas-cli [flags]
- faas-cli [command]
- Available Commands:
- build Builds OpenFaaS function containers
- deploy Deploy OpenFaaS functions
- help Help about any command
- push Push OpenFaaS functions to remote registry (Docker Hub)
- version Display the clients version information
- Flags:
- -h, --help help for faas-cli
- -f, --yaml string Path to YAML file describing function(s)
- Use "faas-cli [command] --help" for more information about a command.
- $ faas-cli new callme --lang node
- Folder: callme created.
- Function created in folder: callme
- Stack file written: callme.yml
You'll see the following was created:
- $ find . | grep callme
- ./callme
- ./callme/handler.js
- ./callme/package.json
- ./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:
- "use strict"
- module.exports = (context, callback) => {
- callback(undefined, {status: "done"});
- }
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.
- $ faas-cli build -f callme.yml
- Building: callme.
- Clearing temporary build folder: ./build/callme/
- Preparing ./callme/ ./build/callme/function
- Building: callme with node template. Please wait..
- docker build -t callme .
- Sending build context to Docker daemon 8.704kB
- Step 1/19 : FROM node:6.11.2-alpine
- ...
- Step 19/19 : CMD fwatchdog
- ---> Running in 53d04c1631aa
- ---> f5e1266b0d32
- Removing intermediate container 53d04c1631aa
- Successfully built f5e1266b0d32
- Successfully tagged callme:latest
- 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.
- $ faas-cli push -f callme.yml
- Pushing: callme to remote repository.
- The push refers to a repository [docker.io/alexellis/callme]
- ...
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:
- $ faas-cli invoke -f callme.yml callme
- Reading from STDIN - hit (Control + D) to stop.
- This is my message
- {"status":"done"}
You can also pipe a command into the function.
- $ date | faas-cli invoke -f callme.yml callme
- {"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:
- $ faas-cli list
- Function Invocations Replicas
- inception 0 1
- callme 2 1
- func_hubstats 0 1
- tensorflow 0 1
- func_echoit 0 1
- 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.
- Acknowledgements: This post is based upon Morning coffee with the OpenFaaS CLI by Alex Ellis