OpenFaaS provides function as a service. FaaS is a framework for building serverless functions on top of containers. FaaS can be run using Docker Swarm. With FaaS developers can focus on development more than servers and deploys. FaaS providers do not charge to their clients for an ideal condition, clients only pay for the computation taken by the function.
If you haven’t installed OpenFaaS, then Install OpenFaaS from here.
Lets start with Simple Python app
create a folder for our files:
$ mkdir myapp && cd myapp
Get templates from GitHub before proceeding
$ faas-cli template pull
we are using python3
for development you can use python2
also. a user following syntax to start with the app.
$ faas-cli new --lang <language> <app-name> --prefix="<your-docker-username>"
we will use python3 so our command will be:
$ faas-cli new --lang python3 elements-sum --prefix="<your-docker-username>"
now you will be having a following directory structure
./elements-sum.yml ./elements-sum ./elements-sum/handler.py ./elements-sum/requirements.txt
modify ./elements-sum/requirements.txt
if any dependancy module required write python code in ./elements-sum/handler.py
by default file will look like:
```python def handle(req): """handle a request to the function Args: req (str): request body """ return req ```
this function only returns args passed
add code to get the sum of elements
def handle(req): sum=0 req=req.split(",") for i in req: sum+=int(i) return str(sum)
if any dependancy needed add it into ./elements-sum/requirements.txt
build, push to docker hub and deploy function containers
$ faas-cli up -f elements-sum.yml
if you got an error then login to docker from CLI using $docker login
the app will be deployed to OpenFaaS http://127.0.0.1:8080/ui/ provide input as
`10,20,30,40,50` will provide output as `150`
or
$ echo 10,20,30,40,50 | faas-cli invoke elements-sum
You can add other functions in ./elements-sum/handler.py
As well as python we can deploy many apps written in different languages