Let's write a Python function with OpenFaaS that executes hello-world and then move onto something more. We'll start by deploying OpenFaaS on our machine and then we'll install the CLI and write the handler for our function.
OpenFaaS is democratising serverless functions - through Docker containers.
gateway- here we can specify a remote gateway if we need to, what the programming language is and where our handler is located within the filesystem.
functions - this block defines the functions in our stack
lang: python - even though Docker is used behind the scenes to package your function. You don't have to write your own Dockerfile unless you want to.
handler - this is the folder / path to your handler.py file and any other source code you need
image - this is the Docker image name. If you are going to push to the Docker Hub change the prefix from hello-python to include your Docker Hub account - i.e. alexellis/hello-python
You'll now see output from the Docker Engine as it builds your function into an image in your local Docker library. You'll see the image appear on docker images
For example you could run:
$ docker images | grep hello-python
hello-python latest e0344b26305f one minute ago
Testing on a single host
If you're using Kubernetes and minikube and don't want to push to a remote container registry, then see the helm chart for how to set the ImagePullPolicy. This will enable use of images from the local library on Kubernetes.
Remote host, or multi-node cluster
If you are using a remote server or a multi-node cluster then you can push your function's image to a registry or the Docker Hub. The Docker Hub is a free service provided by Docker Inc for sharing container images over the Internet.
If you are an advanced user, you can also run a container image registry within your own network. Link: Docker Registry docs
You'll also need to image the image: name to include your Hub account such as image: alexellis2/hello-python.
Here's how to upload the function to a remote registry (if needed):
$ faas-cli push -f ./hello-python.yml
Once you have multiple functions you can also use the --parallel argument to speed things up.
Let's deploy the function:
$ faas-cli deploy -f ./hello-python.yml
Deploying: hello-python.
No existing service to remove
Deployed.
200 OK
URL: http://127.0.0.1:8080/function/hello-python
And it's ready to be tested! Either open up the UI or use curl to call it:
$ curl 127.0.0.1:8080/function/hello-python -d "it's Alex here"
Hello! You said: its Alex here
You can even use the faas-cli to list and invoke your functions.
Try playing with these two commands including looking at the help page with the --help parameter:
So what if you need some dependencies to add behaviour beyond the standard Python libraries? Well you can use pip by providing a requirements.txt file along with your function handler.
Let's include the popular requests module which we can use to interact with webpages.
You will see a file called hello-python/requirements.txt. Add the following line:
requests
requirements.txt
Now we can update our Python code. Let's make it so it can accept JSON request of a URL and a string we want to test for:
We'll trigger it using a JSON request, which will take this format:
Please show support and Star the project on Github
Now that you've built your first function, why not checkout some of the examples built by the OpenFaaS community? You can share your first serverless OpenFaaS function with us on Twitter or Github.
Want to know more about the OpenFaaS Serverless framework for containers?