Stateful Serverless stack with OpenFaaS & Minio

Would it be for trained AI models or simple files, sometimes you want serverless functions to be able to store data for reuse.

This post presents the deployment of a Stateful Serverless stack. Serverless functions will be deployed with OpenFaas. Static data will be stored on Minio.


Make sure Docker and faas-cli are installed on your server.

$ command -v docker && echo "Docker is installed." || echo "Docker is not installed."
Docker is installed.
$ command -v faas && echo "faas-cli is installed." || echo "faas-cli is not installed."
faas-cli is installed.

Run Minio database

By default, OpenFaaS functions are part of func_functions network. Minio database needs to be part of the same network in order to be accessible from OpenFaaS deployments. The --name option specifies the name and hostname of the service.

$ docker service create --name minio \
                        -p 9000:9000 \
                        --network=func_functions \
                        --env MINIO_ACCESS_KEY=minio \
                        --env MINIO_SECRET_KEY=minio123 \
                        minio/minio server /data

OpenFaaS functions can now access the database at minio:9000.

Serverless function


Let us create a simple function which creates a Minio bucket.

$ faas new --lang python3-debian make-bucket

The function uses Minio Python library. Add it to the dependencies.

$ printf "minio\n" >> make-bucket/requirements.txt

Write the handler in make-bucket/

import os, json

from minio import Minio

def handle(req):
    client = Minio('minio:9000', access_key=os.environ['MINIO_ACCESS_KEY'], secret_key=os.environ['MINIO_SECRET_KEY'], secure=False)

    err = None
    except Exception as e:
        err = e.message

    resp = { 'success': False, 'error': err } if err else { 'success': True }

Define Minio creds in make-bucket.yml.

  name: faas

    lang: python3-debian
    handler: ./make-bucket
    image: make-bucket
      MINIO_ACCESS_KEY: minio     # here
      MINIO_SECRET_KEY: minio123  # and here


$ faas build -f make-bucket.yml && faas deploy -f make-bucket.yml


Call it once.

$ curl localhost:8080/function/make-bucket
{"success": true}

The bucket is successfully created.

Call it twice.

$ curl localhost:8080/function/make-bucket
{"success": false, "error": "Your previous request to create the named bucket succeeded and you already own it."}

The bucket already exists.