Skip to content

Instantly share code, notes, and snippets.

@igor822
Created September 19, 2018 02:51
Show Gist options
  • Save igor822/0a7041219ea4f63051ad79380ec64f7d to your computer and use it in GitHub Desktop.
Save igor822/0a7041219ea4f63051ad79380ec64f7d to your computer and use it in GitHub Desktop.
OpenFaaS tutorial

Create Functions

Start docker swarm

$ docker swarm init --advertise-addr lo # {network}

FaaS

OpenFaaS is a framework for building Serveless functions with Docker

Clone it

git clone https://github.com/openfaas/faas

Installing FaaS CLI

url -sL https://cli.openfaas.com | sudo sh

And start faas by:

$ cd faas/ && ./deploy_stack.sh

After running stack, you have to log into faas

echo -n {password} | faas-cli login --username={username} --password-stdin

Templates

To create functions you need some templates, you can pull them by following commands

# PHP Template
faas-cli template pull https://github.com/itscaro/openfaas-template-php
# Other languages' templates
faas-cli template pull https://github.com/openfaas/templates

Creating your first function in PHP

Let's start by creating our first function using PHP.

faas-cli new hello-php --lang php

After you create a new function, faas will create a new directory containing a class Handler.php inside src/ folder, this is our proper function.

Let's build our function

faas-cli build -f ./hello-php.yml

This will build a docker container to execute the function. Now lets deploy our function

faas-cli deploy -f hello-php.yml

Now lets try it

curl -XPOST -d 'hello boy' http://127.0.0.1:8080/function/hello-php

If everything is ok, you should see the hello boy printed, which is the functions being executed. Now lets change our function:

<?php
# hello-php/src/Handler.php
namespace App;

class Handler
{
    public function handle(string $data): void
    {
        echo json_encode(['number' => mt_rand(10000, 999999)]);
    }
}

And then we have to re-build it

faas-cli build -f hello-php.yml

And re-deploy it

faas-cli deploy -f hello-php.yml

So, lets try again

curl -XPOST http://127.0.0.1:8080/function/hello-php

Creating our first function in python

Let's start by creating our first function using Python.

faas-cli new hello-python --lang python

After you create a new function, faas will create a file called handler.py inside hello-python/ folder, this is our proper function.

Let's build our function

faas-cli build -f ./hello-python.yml

This will build a docker container to execute the function. Now lets deploy our function

faas-cli deploy -f hello-python.yml

Now lets try it

curl -XPOST http://127.0.0.1:8080/function/hello-python -d "it's me here"

If everything is ok, you should see the hello boy printed, which is the functions being executed. Now lets change our function:

import random
import json

def handle(req):
    result = {"number": random.randint(1000, 9999)}
    print json.dumps(result)

And then we have to re-build it

faas-cli build -f hello-python.yml

And re-deploy it

faas-cli deploy -f hello-python.yml

So, lets try again

curl -XPOST http://127.0.0.1:8080/function/hello-python
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment