Skip to main content

Deploy python app using OpenFaaS

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


modify ./elements-sum/requirements.txt if any dependancy module required write python code in ./elements-sum/

by default file will look like:

def handle(req):
    """handle a request to the function
        req (str): request body
    return req

this function only returns args passed

add code to get the sum of elements

def handle(req):
    for i in req:
    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 provide input as


will provide output as `150`


$ echo 10,20,30,40,50 | faas-cli invoke elements-sum

You can add other functions in ./elements-sum/

As well as python we can deploy many apps written in different languages