Go serverless, Functions as a Service.

Leonidas Boutsikaris
3 min readNov 11, 2020

Serverless?

The serverless architecture provides services on demand. Well, servers are still used but a company is charged from the cloud vendor only on its usage and not for a fixed amount of servers. This happens by triggering them to work only when needed. These services are auto-scalling and cut a lot of daily costs and deployment time.

Nowadays we use modular separate components to build an application. This is known as the microservice architecture.

microservices

Function as a Service?

Function as a service is a serverless way to execute modular pieces of code. This enables developers to write small pieces of code that only executes in response of a user action.

A lot of cloud companies provide serverless solutions such as Google Cloud Functions or Azure functions but limit your choices.

OpenFaaS

Moving to open source the OpenFaaS project provides a stack and ensures that any process can become a function through a docker container. So the code can be run from whatever language you are currently writing and not tied to the cloud vendor product.

A quick example

For starters we need docker installed and running.

Watch out, if you are using a mac with a small disk space you definitely want to go to docker settings and make the docker raw image smaller, something like 16GB would be enough. This file takes an absurd amount of “logical space”, even if it’s not actually that big it fools the OS.

Also let’s have the OpenFaas cli installed

$ curl -sSL https://cli.openfaas.com | sudo sh

Download the git repo

$ git clone https://github.com/openfaas/faas

We now have to initialize a Docker Swarm. A Docker Swarm is a group of either physical or virtual machines that are running the Docker application and that have been configured to join together in a cluster. Docker swarm is a container orchestration tool, meaning that it allows the user to manage multiple containers deployed across multiple host machines.

$ docker swarm init

Swarm init will give you a password so you can access the localhost. You can save it with

$ faas-cli login -p “password”

We can now deploy the FaaS stack and go to http://localhost:8080.

$ cd faas
$ ./deploy_stack.sh

Create a function

Create a new folder and cd inside it.

At first we can download the FaaS templates

$ faas-cli template pull

And check which we can use with

$ faas-cli new --list

Let’s create a python function

$ faas-cli new new_function --lang python3

This will create 3 files. The handler.py the requirements.txt and the new_function.yml

The requirements.txt file is used to install dependencies for your code. For example numpy, tensorflow or anything else you need.

Build and deploy.

$ faas-cli build -f ./new_function.yml && faas-cli deploy -f ./new_function.yml

You can test your function either by curl or with the OpenFaaS GUI from localhost.

--

--

Leonidas Boutsikaris

Software Engineer, writing code and blogging about it. Found something interesting? Buy me a coffee! https://ko-fi.com/leobouts