OpenFaaS: Classic and of-watchdog templates

OpenFaaS (Function as a Service) is a popular serverless framework. However, unlike other serverless function providers, OpenFaaS has very few restrictions on providing and receiving data. This guide will show you the primary differences between the Classic and of-watchdog templates.

You may also want to read: OpenFaaS: Getting Started on Kubernetes.
YouTube player

Let’s get started!

OpenFaaS provides two types of templates: Classic Watchdog and of-watchdog. 

Classic Watchdog templates:

  • Use STDIO to communicate with your function.
  • Provides compatibility with legacy code such as COBOL.

of-watchdog templates:

  • Use HTTP to communicate with your function.
  • Use connection pools. This in turns prevents overwhelming databases with too many connections.
  • Has a reduced latency vs Classic Watchdog.

You can read more about the differences here.

For both, you can provide data via the POST, PUT, DELETE, UPDATE method using a body. However, the GET method does not support a body.

Most OpenFaaS templates accept a string body input, but you can override that to a raw binary body. The raw binary option is useful when accepting a file as an input.

Add the following to your stack.yml file for the function to accept raw binary:

environment:
  RAW_BODY: True

Serverless template options

The following command will provide a list of templates from the faas-cli templates store:

faas-cli template store list

You will notice output like this:

NAME                     SOURCE             DESCRIPTION
csharp                   openfaas           Classic C# template
dockerfile               openfaas           Classic Dockerfile template
go                       openfaas           Classic Golang template
java8                    openfaas           Java 8 template
java11                   openfaas           Java 11 template
java11-vert-x            openfaas           Java 11 Vert.x template
node14                   openfaas           HTTP-based Node 14 template
node12                   openfaas           HTTP-based Node 12 template
node                     openfaas           Classic NodeJS 8 template
php7                     openfaas           Classic PHP 7 template
python                   openfaas           Classic Python 2.7 template
python3                  openfaas           Classic Python 3.6 template
python3-dlrs             intel              Deep Learning Reference Stack v0.4 for ML workloads
ruby                     openfaas           Classic Ruby 2.5 template
ruby-http                openfaas           Ruby 2.4 HTTP template
python27-flask           openfaas           Python 2.7 Flask template
python3-flask            openfaas           Python 3.7 Flask template
python3-flask-debian     openfaas           Python 3.7 Flask template based on Debian
python3-http             openfaas           Python 3.7 with Flask and HTTP
python3-http-debian      openfaas           Python 3.7 with Flask and HTTP based on Debian
golang-http              openfaas           Golang HTTP template
golang-middleware        openfaas           Golang Middleware template
python3-debian           openfaas           Python 3 Debian template
powershell-template      openfaas-incubator Powershell Core Ubuntu:16.04 template
powershell-http-template openfaas-incubator Powershell Core HTTP Ubuntu:16.04 template
rust                     booyaa             Rust template
crystal                  tpei               Crystal template
csharp-httprequest       distantcam         C# HTTP template
csharp-kestrel           burtonr            C# Kestrel HTTP template
vertx-native             pmlopes            Eclipse Vert.x native image template
swift                    affix              Swift 4.2 Template
lua53                    affix              Lua 5.3 Template
vala                     affix              Vala Template
vala-http                affix              Non-Forking Vala Template
quarkus-native           pmlopes            Quarkus.io native image template
perl-alpine              tmiklas            Perl language template based on Alpine image
crystal-http             koffeinfrei        Crystal HTTP template
rust-http                openfaas-incubator Rust HTTP template
bash-streaming           openfaas-incubator Bash Streaming template
cobol                    devries            COBOL Template

You can identify an of-watchdog template if the name has an HTTP suffix or a description referencing an HTTP server framework such as Flask (Python), Express (Node.js), or similar.

The following command will pull the template that you require:

faas-cli template store pull python3-http

The following command will set up your “python3-http” code scaffold:

faas-cli new my-python-http-function --lang python3-http

Using CURL with a Classic Watchdog function

The following examples use a basic Python 3 Classic Watchdog template.

You can create one using the following commands:

faas-cli template store pull python3
faas-cli new --lang python3 openfaas-classic-python3-basic-function
faas-cli up -f ./openfaas-classic-python3-basic-function.yml

The handler.py code will look like this:

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

    return req

You can perform a simple GET method using the following command:

curl http://localhost:8081/function/openfaas-classic-python3-basic-function

This command will result in no output as the function returns what we have provided. In this case, we provided no string body, so it returned nothing.

To perform a POST method, we can add the “-d” flag to CURL:

curl http://localhost:8081/function/openfaas-classic-python3-basic-function -d "Hi!"

The resulting output will be:

Hi!

Using CURL with an of-watchdog function

The following examples use a basic Python 3 HTTP-of-watchdog template. The significant difference with of-watchdog is the addition of an HTTP interface.

You can create one using the following commands:

faas-cli template store pull python3-flask-debian
faas-cli new --lang python3-flask-debian openfaas-http-python3-basic-function
faas-cli up -f ./openfaas-http-python3-basic-function.yml

The handler.py code will look like this:

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

    return req

Since this OpenFaaS template uses the Flask HTTP Framework; We can modify our handler.py file to the following code:

def handle(req):
    return (
      '{"status":"success" }',
      200,
      {"Content-Type": "application/json"},
)

Remember to run the following faas-cli command to update and deploy your code:

faas-cli up -f ./openfaas-http-python3-basic-function.yml

You can perform a simple GET method using the following command:

url http://localhost:8081/function/openfaas-http-python3-basic-function -i

The “-i” flag ads the response header from the serverless OpenFaaS function.

This flag will result in output like this:

HTTP/1.1 200 OK
Content-Length: 21
Content-Type: application/json
Date: Sat, 06 Nov 2021 05:02:03 GMT
Server: waitress
X-Call-Id: ce915ef1-cf0e-4f8e-99c9-031612f58af3
X-Duration-Seconds: 0.001937
X-Start-Time: 1636174923532191893

{"status":"success" }

In this case, we can see the JSON response we expected with the correct header and the 200 status code. Like the previous POST example, you can add the “-d” flag to CURL to POST.

Wrapping up

You now have a good understanding of the primary differences between OpenFaaS Classic and of-watchdog templates.

You may also be interested in



About the Authors

Anto's editorial team loves the cloud as much as you! Each member of Anto's editorial team is a Cloud expert in their own right. Anto Online takes great pride in helping fellow Cloud enthusiasts. Let us know if you have an excellent idea for the next topic! Contact Anto Online if you want to contribute.

Support the Cause

Support Anto Online and buy us a coffee. Anything is possible with coffee and code.

Buy me a coffee



About Anto Online

Having started his career in 1999 as a Desktop Support Engineer, Anto soon changed paths and became a developer. After several years of development experience, he transitioned into a consultant. As an enterprise application consultant for a leading SaaS software provider, Anto specializes in AWS's serverless technologies. By day, Anto focuses on helping customers leverage the power of serverless technologies. By night, he indulges his passion for cloud computing by playing with Python and trying out things that are currently beyond the scope of his work. Sometimes Anto needs help as there are not enough hours at night. So Anto relies on a team of fellow Cloud enthusiasts to help him out. Each one is a Cloud expert in their own right, and Anto takes great pride in helping them learn and grow.

View all posts by Anto Online →

One Comment on “OpenFaaS: Classic and of-watchdog templates”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.