Useful faas-cli One-Liners
Tags: openfaas, faas, cli, shell
Published: 2020-07-10 13:20:21

[OpenFaaS](https://www.openfaas.com/) is a framework for building serverless functions with Docker and Kubernetes. Their goal, in their own words, is to make it easy for developers to deploy event-driven functions and microservices to Kubernetes without repetitive, boiler-plate coding.

[faas-cli](https://github.com/openfaas/faas-cli) is the official CLI for OpenFaaS. In other words it is a command-line tool to help manage, prepare, and invoke functions.

In this post we’re going to cover 3 tips on how to use `faas-cli` to improve (and automate) our OpenFaaS workflows

#### 1. Handle Multiple Functions from Multiple Files

When you start working with `faas-cli` it’s tempting to do this a lot:

`faas-cli new --lang= ...`

This will result in creating a new function (based on a template) but will also put it in it’s own YAML file. Now if we would like to leverage the `faas-cli up` command line option, we will need to find a way to run it on multiple YAML files. Here’s a one liner that will do it:

```
ls *.yml | awk '{ print $1 }' | xargs -I {} faas-cli up -f {}
```

This will build, push and deploy all of our functions in all of our YAML files. If we would like to remove all our functions from all our YAML files, just change `faas-cli up` to `faas-cli remove -f`:

```
ls *.yml | awk '{ print $1 }' | xargs -I {} faas-cli remove -f {}
```

Another way to remove all the functions (if you don’t have the YAML files) is this:

```
faas-cli list | tail -n +2 | awk '{ print $1 }' | xargs -I {} faas-cli remove {}
```

This will enumerate the output of `faas-cli list` instead of operating on the functions in the YAML files

#### 2. Merge Multiple Functions from Multiple Files to a Single YAML Stack File

Consolidating all your functions to one YAML file has many benefits and you can easily do it using the [yq](https://github.com/mikefarah/yq) utility:

```
yq merge *.yml > stack.yml
```

This will simplify your workflow, reduce your need for one liners and … it has more benefits! So keep reading the next tip :-)

#### 3. Use Multiple Cores to Build & Deploy your Single YAML Stack File

The `faas-cli` lets you parallelize the build, push and deploy process by specifying the `--parallel` command line option. The `--parallel` takes an integer that will define how many concurrent build actions should be performed. A good starting point will be to pass [nproc](https://www.gnu.org/software/coreutils/manual/html_node/nproc-invocation.html) output to it:

```
faas-cli up -f stack.yml --parallel `nproc`
```

The output of the `nproc` utility is the number of processing units available on the computer that you are running this command on. So ideally you can maximize your CPUs in the process. 

And of course, removing is now easy as:

```
faas-cli remove -f stack.yml
```

#### What’s next?

Go ahead and apply some of the tips above to your current OpenFaaS projects and workflows. If you are looking for more tips, check out this [post](https://www.openfaas.com/blog/five-cli-tips/) by Alex Ellis. Do you have a favorite `faas-cli` trick? Feel free to leave it in your comments below.

Big thanks to [Alex Ellis](https://dev.to/alexellis) for reviewing this post