Web701 11

April 2, 2019
Web701 VirtualBox Serverless OpenFaas Docker CLI Python

Web Development 701 ~ OpenFaas ~ Lab 4

In this blog we are going to go through lab 4 which goes into more depth around functions.

The first part of the lab covers configuration injection with environment variables. There are two ways of doing so; at deployment time and using query string and HTTP headers. Environment variables at deployment time are covered in lab 3 when working with write_debug, which is where the environment variable is added to the YAML file.

For using query string and HTTP headers the lab requires us to deploy a function that displays environment variables using a built-in BusyBox command. This is done via the following.

faas-cli deploy --name env --fprocess="env" --image="functions/alpine:latest" --network=func_functions

Now to invoke the function with a query string via the following.

echo "" | faas-cli invoke env --query lab=4 results in Http_Query=lab=4

The function can also be invoked via a curl command, an example is as follows.

curl -X GET http://127.0.0.1:8080/function/env/hello/world -d ""

results in Http_Path=/hello/world

Next to invoke the function with a header via the following.

curl http://127.0.0.1:8080/function/env --header "X-Output-Mode: json" -d ""

results in Http_X_Output_Mode=json


The next topic covered in this lab is making use of logging. The case when a function exits with a non-zero code and stderr is not empty, stderr is not printed to the logs. The lab prompts to change the code of handler.py to include the following.

sys.stderr.write("This is an error message.\n")

return json.dumps({"Hello": "from OpenFaas!"})

Now to build/deploy and invoke with the following.

faas-cli up -f hello-openfaas.yml

echo | faas-cli invoke hello-openfaas

The lab states that it is necessary to redirect the messages from stderr to the container’s logs via adding combine_output: false to the hello-openfaas.yaml file under the environment variable.


The next section of the lab covers workflows. The first being chaining functions on the client-side. To start we need to deploy the nodeinfo function via faas-cli deploy nodeinfo. Then to chain the functions we execute the following.

echo -n "" | faas-cli invoke nodeinfo | faas-cli invoke markdown

This prints the information of the node then pushes it through the markdown function which converts the print to markdown.

The second workflow is calling one function from another. This workflow require us to deploy the sentiment analysis function to start via the command faas-cli deploy sentiment analysis. To call the sentiment analysis function from our hello-openfaas function we add the requests module. With the requests module we pass ("http://127.0.0.1:8080/function/sentimentanalysis", data=req). The lab suggests to use an environment variable for the hostname (instead of 127.0.0.1) for two reasons; the name may change and Kubernetes sometimes requires a suffix.

Thats it for this lab

Web701 22

June 15, 2019
Web701 Serverless OpenFaas Docker CLI Python Digital Ocean

Web701 21

May 21, 2019
Web701 VirtualBox Serverless OpenFaas Docker CLI Python

Web701 20

May 20, 2019
Web701 Python Django Heroku Web Hosting
comments powered by Disqus