-
Notifications
You must be signed in to change notification settings - Fork 9
First serverless Python function with OpenFaaS
This wiki describes how to create your first serverless python function with OpenFaaS. OpenFaas is a serverless framework for Docker and Kubernetes.
The instructions are directly copied from the blog by Alex Ellis below, with minor comments added.
https://blog.alexellis.io/first-faas-python-function/
On Raspberry Pi, install Docker
curl -sSL https://get.docker.com | sh
sudo usermod -aG docker pi
and reboot. The Docker installation on Mac can be done through Homebrew:
brew install ruby
brew cask install docker
Initialize Swarm mode on your Docker daemon.
docker swarm init
You now have a single-node Docker cluster. That is all we need to deploy the FaaS stack and sample functions.
git clone https://github.com/openfaas/faas
cd faas
./deploy_stack.sh
The default stack contains sample functions, you can play with them via the UI at http://localhost:8080.
Install CLI on Raspberry Pi
curl -sSL https://cli.openfaas.com | sudo sh
or on Mac
brew install faas-cli
Create a new Python function using the CLI.
mkdir -p ~/functions && cd ~/functions
faas-cli new --lang python hello-python
This creates three files for you:
hello-python/handler.py
hello-python/requirements.txt
hello-python.yml
Edit the handler.py
file in the following way:
def handle(req):
print("Hello! You said: " + req)
Build the function:
faas-cli build -f ./hello-python.yml
See the image:
docker images | grep hello-python
Deploy the function:
faas-cli deploy -f ./hello-python.yml
List the available functions:
faas-cli list
Invoke function in one of the following ways:
curl localhost:8080/function/hello-python -d "it's David here"
echo "it's David here" | faas-cli invoke hello-python
You can also invoke the function from a different machine on your local network:
curl 192.168.2.2:8080/function/hello-python -d "it's David here"