Write & Deploy Functions on OpenFaaS
In this article we will see how you can write your own functions and deploy them on OpenFaaS.
Prerequisites
docker
installed and running on your machineopenfaas
,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.- 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.
- 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
- 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.