OpenFaaS - deploying serverless functions to Docker Swarm via a CLI
Tags: docker, faas, serverless, openfaas
Published: 2017-05-18 21:52:02

---
title: OpenFaaS - deploying serverless functions to Docker Swarm via a CLI
published: true
description: OpenFaaS is an interesting new framework for deploying serverless functions to the cloud, built on top of Docker. In this post, I will walk through building a function, shipping it to a remote swarm and auto scaling it.
cover_image: https://thepracticaldev.s3.amazonaws.com/i/xthsiqnu2i1vuto738nn.jpg
tags: Docker,FaaS,serverless,OpenFaaS
---

# OpenFaaS

[Functions as a Service](https://openfaas.com) or OpenFaaS (lead by [Alex Ellis](https://twitter.com/alexellisuk)) is a really neat way of implementing serverless functions with Docker. You can build out functions in any programming language and then deploy them to your existing Docker Swarm.

In this post we'll look at a CLI for making this process even easier.

![](https://asciinema.org/a/141284.png)

[Demo](https://asciinema.org/a/141284)

## OpenFaaS highlights
- The only serverless framework for both Docker Swarm and Kubernetes
- Easy to use - deploy in 60 seconds, UI and CLI built-in
- Supports code or binaries in any language on Windows or Linux

# Getting up and running

To run functions with OpenFaaS, you'll first need to deploy an OpenFaaS cluster to a Docker Swarm. This is really simple, just follow the steps over on the [deployment guide](https://github.com/openfaas/faas/blob/master/guide/deployment_swarm.md).

# How it works

The diagram below gives an overview of how the OpenFaaS function packages, the Docker image, and the `faas-cli deploy` command fit together.

![The OpenFaaS CLI makes it easy to deploy your functions!](https://finnian.io/blog/content/images/2017/05/Screen-Shot-2017-05-02-at-15.12.45.png)

To deploy a function onto an OpenFaaS stack, you firstly must write the function itself. This is really easy and you can do it in any language which runs inside Docker (i.e. all of them). At the current time, the OpenFaaS CLI supports Python and Node, so for this example we'll use Python.

My very simple fib function is written in Python:

```python
def fib(n):
    if n <= 1: return n
    else: return fib(n-1) + fib(n-2)

def handle(st):
    n = int(st)

    output = []
    for i in range(n):
        output.append(str(fib(i)))
    print(', '.join(output))
```

This code is saved in `sample/fib/handler.py`. It's important to create a `requirements.txt` file in the same directory (even if your Python code has no requirements from pip) because otherwise `pip install` will throw an error.

## Building the function package

![OpenFaaS deploy overview](https://finnian.io/blog/content/images/2017/05/FaaS-function-diagrams.001.jpeg)

The way FaaS runs the function is by running this inside Python:

```python
from function import handler
handler.handle(stdin)
```

When you build a function with the OpenFaaS CLI, what it's doing is packaging up your function (`handler.py`) and building out the parts for Docker to run the container.

The last step is to write the `stack.yml` file which FaaS will use to build and deploy the function.

```yaml
provider:
  name: faas
  gateway: http://localhost:8080 # your faas stack url

functions:
  fib:
    lang: python
    handler: ./sample/fib
    image: developius/func_python-fib
```

To build the package, simply run:

```
$ faas-cli  build -f ./stack.yml
```

Here is an example package for our `func_python-fib` function, located inside `faas-cli`'s `build` directory:

```shell
$ tree build/fib/
build/fib/
â”œâ”€â”€ Dockerfile
â”œâ”€â”€ function
â”‚Â Â  â”œâ”€â”€ handler.py         <-- called by index.py
â”‚Â Â  â””â”€â”€ requirements.txt
â”œâ”€â”€ index.py               <-- Docker runs this
â””â”€â”€ requirements.txt

1 directory, 5 files
```

The package includes everything to allow Docker to run our function - `Dockerfile`, `index.py` and `handler.py`

## Deploying the function

Now that our function is built, we need to deploy it. Before we do this however, we must push our function to the hub so that all the nodes in the swarm can access it.

```
$ docker push developius/func_python-fib
```

Now for the deploy! This can be done with one command:

```shell
$ faas-cli deploy -f ./stack.yml
```

When you run this command, the function will be deployed to the remote Docker Swarm. How cool is that?

## Testing

The final thing to do is to test your function deployed successfully. You can do this via the OpenFaaS UI or via `curl`.

![Testing via the FaaS UI](https://finnian.io/blog/content/images/2017/05/Screen-Shot-2017-05-01-at-21.23.26.png)

Or, via curl:

```
$ curl -d "10" http://localhost:8080/function/fib
0, 1, 1, 2, 3, 5, 8, 13, 21, 34
```

## High Availability via autoscaling

FaaS supports Docker Swarm's replication feature, which allows auto scaling of functions directly inside Docker. OpenFaaS makes use of this internally to make sure enough function replicas are available to deal with requests. It works by exporting metrics for [Prometheus](https://prometheus.io/), which then generates alerts when thresholds are reached. The FaaS gateway will then scale up the functions when it receives these alerts.

This can be seen perfectly in the screenshot of my Grafana dashboard below.

![Autoscaling within OpenFaaS](https://thepracticaldev.s3.amazonaws.com/i/2qnqpir8d6jc8dwe65zh.png)

## Recap

In this post we've seen how to package and deploy a function using the FaaS framework. We've looked at how to write a function, how to build it into a function package and deploy it to a remote Docker Swarm. Pretty neat stuff.

I've built a [number of other functions](https://github.com/alexellis/faas/blob/master/community.md) and hope to expand this to include more soon. I wrote a function to shorten URLs, using [a service I built](https://finnian.io/blog/a-url-shortener-written-in-ten-minutes/), which received a PR from [Richard Gee](https://twitter.com/rgee0) to merge in an armhf Dockerfile for use on Raspberry Pies. This was a big milestone for me as I've never accepted a PR for one of my projects before!

You can find out more about the projects I've been doing with Docker on my blog at [finnian.io/blog](https://finnian.io/blog) - including [my DockerCon 2017 writeup](https://finnian.io/blog/dockercon-2017-highlights-and-experiences/)! Feel free to get in touch on Twitter [@developius](https://twitter.com/developius) and I'd love to hear your thoughts below.

Props to [Alex Ellis](https://twitter.com/alexellisuk) for creating such an awesome tool, I look forward to participating in the development of FaaS in the future. It's worth noting that FaaS is currently considered experimental but you can expect more language templates amongst some other neat features. First... learn Go. ðŸ™ˆ