The OpenFaaS CLI (faas-cli) has a template engine built-in which can create new functions in a given programming language. The way this works is by reading a list of templates from the ./template location in your current working folder.
In the past, a default templates repository contained all templates for OpenFaaS, but now they're distributed across a number of different Git repositories.
If the language you want to use is listed in the template store (faas-cli template store list), then it will be fetched automatically whenever you run faas-cli new or faas-cli build.
To create a Python function called hello-python using the python3-http template, you would run:
$faas-clinew--langpython3-httphello-python
Since this function is listed in the Templates Store, it will be downloaded automatically for you.
If you need to get a custom, or modified template from a different Git repository, you can use the faas-cli template pull command first.
The below is an example of a fictitious Rust Tokio template being pulled from GitHub and then used to create a new function:
Version pinning is supported for templates as of faas-cli version 0.18.0.
You can specify the version of a template in three ways:
Latest - no pinning, always get the latest version golang-middleware - this always pulls from the default branch, using its latest commit
Git tag or branch - specify a tag after an @ symbol golang-middleware@0.20.0 or golang-middleware@debian
Commit SHA - specify a full commit SHA after an @ symbol golang-middleware@sha-4d0f102baedd9f4d618dac34ff419292e2f91578 (short SHAs are also supported i.e. golang-middleware@sha-4d0f102)
These versions can be specified when running faas-cli new, or directly in stack.yaml if the function already exists.
Here's how you'd pin an existing function to a specific version in stack.yaml:
The template pull command uses your local Git client, so if you have proper authentication set up with a Git credential helper, then you'll also be able to run it against private repositories.
The Classic Templates are held in the openfaas/templates repository and are based upon the Classic Watchdog which uses STDIO to communicate with your function. The of-watchdog uses HTTP to communicate with functions and most of its templates are available in the openfaas organisation in their own separate repositories on GitHub and in the store.
How to pick:
Use the Classic Watchdog if you're starting out or following tutorials or guides
Use the of-watchdog if you need more performance or if you need full control of the HTTP response
You can create your own template store by following the convention found in the official templates store repository, then provide the --url flag when pulling or listing templates. If you're using GitHub, you'll need to reference the raw file rather than the view in a webbrowser.
[{"template":"golang-middleware","platform":"x86_64","language":"Go","source":"openfaas","description":"HTTP middleware interface in Go","repo":"https://github.com/openfaas/golang-http-template"}]
As an alternative to repeating the --url flag on each command, you can also set the OPENFAAS_TEMPLATE_STORE_URL environment variable in your .zshrc or .bashrc file.
For instance, you could add these lines to override installations to use your company or team repository:
Two Java templates are provided java11 and java11-vertx, both of which use Gradle as the build system. Please note that the java8 template is deprecated, and should not be used.
If you need a different version, then please fork the templates repository, or contact sales@openfaas.com to access additional templates via your OpenFaaS Premium Subscription.
Support is made available for external code repositories via the build.gradle file where you specify dependencies to fetch from repositories or JAR files to be added via the build.
Edit the ruby-function.yml and point your image to your dockerhub, for example
${your_user}/ruby-function
$ faas-cli up -f ruby-function.yml
...
Using bundler 1.16.4
Fetching multi_xml 0.6.0
Installing multi_xml 0.6.0
Fetching httparty 0.16.2
Installing httparty 0.16.2
Bundle complete! 1 Gemfile dependency, 3 gems now installed.
Bundled gems are installed into `/usr/local/bundle`
Post-install message from httparty:
When you HTTParty, you must party hard!
...
It is recommended that you use the official templates as they are provided and if there is a short-coming that you raise a GitHub issue so we can improve the templates for everyone.
All templates are driven by a Dockerfile and can be customised by editing the files found in the ./template folder.
There are several reasons why you may want to update your Dockerfile, just edit ./template/<language_name>/Dockerfile.
New base image - some companies prefer to use their own base images for Docker images for compliance, support or licensing reasons
Add native package - sometimes you may want to add a native package from the Alpine Linux repository or the Debian package repository - just add a step into the Dockerfile
Try a new version of a base-image - it may be that the project is showing support for Node.js LTS, but you want the cutting-edge version, you can do that too
Note: In order to access the template store you need 0.8.1 version of the CLI or higher
Check what templates are available in the template store with the CLI by typing:
faas-clitemplatestorelist
Pull the desired template by specifying NAME attribute only:
faas-clitemplatestorepullgo
or pull the template by mixing the REPOSITORY and NAME attributes the following way:
faas-clitemplatestorepullopenfaas/go
To get more information on specific store use the describe verb like:
faas-clitemplatestoredescribeopenfaas/go
or if there is no collision between names use only the name field:
faas-clitemplatestoredescribego
If you have your own store with templates, you can set that as your default official store by setting the environmental variable OPENFAAS_TEMPLATE_STORE_URL the following way: