Web Development 701 ~ OpenFaas Continued ~ Lab 3
In this blog we are going to explore the OpenFaas workshop. Since we already have OpenFaas deployed and running from the previous blog, we are going to skip to lab 3; introduction to functions. OpenFaas provides two ways of creating a new function; using a template, or using an existing binary. I dont have a binary to use, so I am going to be using a template.
To start we have to pull the templates from github via the following.
faas-cli template pull
Next to create a hello world function in Python via the following.
faas-cli new --lang python3 hello-openfaas --prefix="oglinuk"
The tutorial suggests to edit the handler.py
file and to replace the return value with something else, so I changed it to return Hello from OpenFaas!
.
Next we run the following command.
faas-cli up -f hello-openfaas.yml
We can invoke our function two ways.
- Going to
localhost:8080/function/hello-openfaas
- Executing the command
echo | faas-cli invoke hello-openfaas
The next step in the tutorial is to output the container logs which can be done via the following.
docker service logs -f hello-openfaas
The tutorial goes on to cover verbose output with write_debug, multiple functions, custom templates, and custom binaries as functions which I am not going to cover but have provided a link in case you would like to explore.