In this post I’ll share 5 top tips for boosting productivity with the OpenFaaS CLI. The CLI is used by developers to interact with OpenFaaS from the terminal is the most popular part of the project for new contributors to cut their teeth on. Since 2017 the contributors been incrementally fine-tuning the developer-experience through user-feedback, new features and productivity-boosters.
Part of this work has also involved expanding the set of language templates that are pre-packaged with OpenFaaS to include languages such as Java. A recent developer-survey by DigitalOcean showed a high-level of overlap between the languages being used in containers in the responses and the list provided by the project.
From @digitalocean report "For those using containers, what languages are you most frequently using?" vs. "faas-cli new --list" in OpenFaaS - what else would you like to see as a template? pic.twitter.com/1S49KYMj42— Alex Ellis (@alexellisuk) July 4, 2018
Note: any binary or container can be made into a function with OpenFaaS meaning you can run any code, anywhere you like at any scale.
Before we get started, make sure you have the OpenFaaS CLI installed via the docs.
Tip 1 & 2: use
The OpenFaaS YAML definition allows developers to group together all their functions in one place. When you get half a dozen functions and are only changing one you can use the
--filter flag to speed things up.
Here’s an example:
Create four functions appending them all to the same YAML file:
faas-cli new --lang node --name filter1 faas-cli new --lang node --name filter2 --append filter1.yml faas-cli new --lang node --name filter3 --append filter1.yml faas-cli new --lang node --name filter4 --append filter1.yml
Now if you were to run a
deploy you’d carry that step out for all four functions. Fortunately Docker’s caching would speed this up, but we knew we could make this experience better, that’s when we introduced the
--filter option to all three verbs.
You can simply type in
faas-cli build --filter=filter2 for instance.
Now if you do happen to edit most of your functions most of the time such as with OpenFaaS Cloud, then you can use the
--parallel flag and your images will be built concurrently. If you have 16 functions in one YAML file and use
--parallel=4 they will build in batches of 4 saving time overall. This speed-boosting feature isn’t available when working with Docker outside of OpenFaaS.
You can also split your functions into separate folders, repos or YAML files.
For a full YAML reference for the “stack file” see the docs: YAML format reference
Tip 3: use
OPENFAAS_URL for your gateway address
All commands that reach out to the OpenFaaS gateway can take a flag for a remote or local address.
You can log into your remote gateway using
faas-cli login for example.
Override with a flag or a short-flag:
faas-cli login --gateway http://127.0.0.1:31112 faas-cli login -g http://127.0.0.1:31112
Use an environmental variable:
export OPENFAAS_URL=https://gw.my-openfaas.com faas-cli login
Some other verbs or commands that can take a
gateway URL are:
invoke and more. For a full list type in
Tip 4: Use static and build dynamic tags for versioning
There are several ways you can version your functions and the simplest way is to change the tag of the Docker image between commits.
functions: test: lang: go handler: ./test image: alexellis2/test:latest
To statically change the version you can alter the word
test:0.1 and so-forth. This will result in a number of different Docker images being pushed and maintained in your registry allowing you to go back to a known version easily using a rolling-update and without down-time.
You can also generate a dynamic tag based upon
git meta-data in the repository such as SHA ID and branch.
mkdir -p tester cd tester git init faas-cli new --lang go test1 --prefix=alexellis2 git add . git commit -s -m "Initial commit" faas-cli build --yaml test1.yml --tag=sha
In this example the short-format Git SHA ID was appended to the tag in the YAML file:
Successfully tagged alexellis2/test1:latest-b6d5661
If we change the tag in the YAML file from
0.1 then the image tag would have been:
test1:0.1-b6d5661. This is useful for CI/CD jobs and is supported by the verbs:
You can also omit the
--yaml/-f flag if you rename your YAML file to
Tip 5: Use
faas-cli up and save on typing
From early on contributors and users requested an alias or new CLI command to prevent having to type in something like the following after each change:
faas-cli build && \ faas-cli push && \ faas-cli deploy
It might not look bad, but if you decide to add some flags it could become repetitive:
faas-cli build --filter=filter1 && \ faas-cli push --filter=filter1 && \ faas-cli deploy --filter=filter1 --gateway https://alt-gw.my-openfaas.com
Spurred by great developer experiences found in Docker Compose and other similar tooling I raised an issue and looked for a contributor to help build the new feature. After a couple of weeks John Mccabe volunteered to take a look. His work was merged in the 0.7.0 version of the CLI.
The above examples become:
faas-cli up --filter=filter1 --gateway https://alt-gw.my-openfaas.com
You can also skip the push step with
--skip-push if you’re working locally.
Here’s the feature in action in a handy gif made by John for his PR:
I hope you’ve enjoyed my list of top 5 productivity boosts for the CLI. If you want to learn more and get started then the community has produced a set of hands-on labs for you in the OpenFaaS Workshop.
Curious about who is using OpenFaaS in production? Check out the list of companies that have let us know so far over at https://docs.openfaas.com/.