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?
OpenFaaS is a framework for packaging code, binaries or containers as Serverless functions on any platform - Windows or Linux. Visit website
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)
remove Remove deployed OpenFaaS functions
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.
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.
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 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.
$ 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.
$ 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.
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.