Write & Deploy Functions on OpenFaaS

Avishka Shamendra
2 min readOct 18, 2022

--

In this article we will see how you can write your own functions and deploy them on OpenFaaS.

Prerequisites

  1. docker installed and running on your machine
  2. openfaas, faas-cli installed and running on your machine. If you have have not installed these you can either follow Deploying OpenFaaS on Linux with Minikube or Deploying OpenFaaS on Linux with KinD article.
  3. A Docker Hub account.

Writing Functions

To understand how the whole process works lets consider writing a simple python function to capitalize a given string.

  1. To start with let’s create a folder to keep the functions
$ mkdir functions
$ cd functions

2. We can use openfaas templates as a starting point. Let’s pull the python3 template from the template store using faas-cli.

$ faas-cli template store pull python3

3. Now let’s use faas-cli and create the function capitalize from the pulled template

$ faas-cli new capitalize --lang python3

4. Now go to the function folder created and change the handler.py

def handle(req):
return str(req).capitalize()
# your code should be within the handle function

That’s it for the writing part. Now let’s deploy this function

Building & Deploying Functions

  1. Set the OPENFAAS_PREFIX variable.
$ export OPENFAAS_PREFIX=<your_docker_hub_username>

2. Let’s build & deploy the above capitalize function we wrote. The following command will,

  • build the function image
  • push the image to Docker Hub
  • deploy the function on OpenFaaS
$ faas-cli up -f capitalize.yml

That’s it. Now you should be able to invoke the function via the URL provided or you can use the OpenFaaS Portal to invoke the function.

Make sure you clap if you liked it and feel free to leave a comment.

--

--

Avishka Shamendra
Avishka Shamendra

Written by Avishka Shamendra

Computer Science and Engineering Graduate from University of Moratuwa, Sri Lanka

No responses yet