Skip to content

Polyglot OpenFaas Functions

This project is a set of Knative functions using OpenFaas runtimes

To deploy these functions:

When the CI/CD pipeline is complete, you can test the functions with GET requests:

  • You can retrieve the url of each functions by selecting the Operations/Serverless menu

How to re-create this project

Initialize the project

First, you have to install the OpenFaaS CLI. See here: https://docs.openfaas.com/cli/install/

You need to pull in the official OpenFaaS language templates with this command:

md polyglot-openfaas-functions
cd polyglot-openfaas-functions
faas-cli template pull

The OpenFaaS CLI will create a template directory with all the needed templates.

The, add a .gitignore file to your project with this content:

.template

Initialize the functions

I want to create a CSharp function, a GoLang function, a Java 11 function and a Ruby function. Then type the below commands:

cp -R template/csharp ./hello-csharp
cp -R template/go ./hello-go
cp -R template/java11 ./hello-java11
cp -R template/ruby ./hello-ruby

Update the functions

CSharp

Update hello-csharp/function/FunctionHandler.cs:

using System;
using System.Text;

namespace Function
{
    public class FunctionHandler
    {
        public string Handle(string input) {
            return "πŸ‘‹ Hello From CSharp πŸ˜‰\n";
        }
    }
}

GoLang

Update hello-go/function/handler.go:

package function

import (
 "fmt"
)

// Handle a serverless request
func Handle(req []byte) string {
 return fmt.Sprintf("πŸ–οΈ Hello from GoLang 😍")
}

Java 11

Update hello-java11/function/src/main/java/com/openfaas/function/Handler.java:

package com.openfaas.function;

import com.openfaas.model.IHandler;
import com.openfaas.model.IResponse;
import com.openfaas.model.IRequest;
import com.openfaas.model.Response;

public class Handler implements com.openfaas.model.IHandler {

    public IResponse Handle(IRequest req) {
        Response res = new Response();
        res.setBody("πŸ–– Hello World from Java 11 πŸ’₯");

        return res;
    }
}

Ruby

Update hello-ruby/function/handler.rb:

class Handler
  def run(req)
    return "Hello world from the ✨ Ruby Language πŸŽ‰"
  end
end

And now, time to build and deploy

Add a .gitlab-cy.yml file to your project:

include:
  - template: Serverless.gitlab-ci.yml 

functions:build:
  extends: .serverless:build:functions
  environment: production

deploy:function:
  extends: .serverless:deploy:functions
  environment: production

Run the pipeline, wait a little ⏳, that's all.