OpenFaaS Lab 2

Deploying and call your first function

Posted by Krystian Wojcicki on Monday, July 29, 2019 Tags: Guide   4 minute read

Lab 2

  • Deploying and calling custom functions

Using your favorite language (will use python in this example) we are going to build a word count function. That will take in sentences and return a json object with keyvalue pairs that hold the word and occurrence

faas new --lang python3 wc

This will generate a folder called wc in your pwd and a wc.yml. Inside the wc folder you will see a requirements.txt + The contains a single method handle(req) which is your entrypoint.

One can directly call the handle method by appending the following to

if __name__ == '__main__':
    print(handle("hello world asd hello"))

Then call the function as follows

kwojcicki@ubuntu:~/workspace/openfaas-workshops$ python3 ./wc/ 
hello world asd hello

Now lets implement that actual code for counting the # of words

import json

def handle(req):
    """handle a request to the function
        req (str): request body

    words = req.split()

    freq = {}

    for word in words:
        if word in freq:
            freq[word] = freq[word] + 1
            freq[word] = 1

    return json.dumps(freq)

if __name__ == '__main__':
    print(handle("hello world asd hello"))

Now running our function locally

kwojcicki@ubuntu:~/workspace/openfaas-workshops$ python3 ./wc/ 
{"hello": 2, "world": 1, "asd": 1}

Great our function runs locally now lets deploy it using OpenFaas.

faas build -f wc.yml
kind load docker-image wc:latest
faas deploy -f wc.yml --gateway $ip:31112

Now you can either invoke your function as in lab1 using the UI or using the CLI/curl address.

kwojcicki@ubuntu:~/workspace/openfaas-workshops$ curl -XPOST $ip:31112/function/wc -d 'Hello I I am'
{"Hello": 1, "I": 2, "am": 1}
kwojcicki@ubuntu:~/workspace/openfaas-workshops$ echo "Hello I I am" | faas invoke wc --gateway $ip:31112
{"Hello": 1, "I": 2, "am": 1}

Great, but what if our function has an error and needs to log something? For the python3 template OpenFaas uses the Forking method of invoking functions as seen below


Normally OpenFaas returns the combination of stdout + stderr this can be turned off by adding

      write_debug: true
      combine_output: false

to your wc.yml as follows:

version: 1.0
  name: openfaas
  gateway: http://
    lang: python3
    handler: ./wc
    image: wc:latest
      write_debug: true
      combine_output: false

Now OpenFaas will only return the stdout in the response but stderr will still be logged.

Back in ./wc/ lets import sys and then add sys.stderr.write("Hello from handler!\n") somewhere in our handle function.

Now lets redeploy and invoke our function

faas build -f wc.yml
kind load docker-image wc:latest
faas deploy -f wc.yml --gateway $ip:31112
echo "Hello I I am" | faas invoke wc --gateway $ip:31112

Now using the faas logs command we can check out the logs our function is emitting

kwojcicki@ubuntu:~/workspace/openfaas-workshops$ faas logs wc --since 1m --gateway $ip:31112

2019-08-10 15:48:37.058889122 +0000 UTC wc (wc-c75969444-kgxcg) 2019/08/10 15:48:37 stderr: Hello from handler!

2019-08-10 15:48:37.059171373 +0000 UTC wc (wc-c75969444-kgxcg) 2019/08/10 15:48:37 Duration: 0.104399 seconds

2019-08-10 15:48:37.05917626 +0000 UTC wc (wc-c75969444-kgxcg) {"Hello": 1, "I": 2, "am": 1}