Architektur

Developing FaaS with Oracle fn Project

fn-project
Carsten Wiesbaum
Carsten Wiesbaum

In my last post I wrote about general concepts behind Function as a Service (FaaS) and my view on this application development style. As an example I used the basic pipes and filters concept implemented by Unix Shells and many shell commands. Within this post I will use the same example and implement it using a Function as a Service platform. To recap the scenario, we had a simple JSON file containing booking records. From this file we identified certain records based on purchased products. Afterwards we extracted customer emails which could be used as a list for targeted marketing.

The FaaS platform I will be using is Oracle fn Project. It is an Open Source project which aims to develop an open and standard based FaaS Platform as alternative to mostly proprietary solutions existing in the market today. According to Oracle a managed service will be released in near future. Let us start and develop FaaS with Oracle fn Project!

Developing FaaS with Oracle fn Project

To begin with, the beauty of fn Project is that one is able to develop FaaS applications on any machine. This includes local development machines. From the decision to use fn to the first deployed function one needs less than nine shell commands. The mandatory „Hello World“ example can be found on fn Project’s GitHub repository. Just follow the instructions and come back to this post with a running fn server.

Now let us start to implement our FaaS example using fn Project. The first function should extract all booking records containing a specific product. Let us call this function searchProduct.

fn Project supports multiple languages. For our example we will be using Go. Why? Somehow everyone does and I wanted to test it myself!

So what did we get?

As one can see fn init generated some files in the searchProduct folder, namely:

  • Gopkg.toml – Go package management configuration
  • func.go – Go file containing function code
  • func.yaml – Metadata required by fn Project
  • test.json – Test definition file

In general, only func.go content is relevant for our example. Replace it with the following code:

Now that we got our searchProduct function implementation in place, we need to build and deploy it.

The fn deploy command created a new application named targetedMarketing and deployed the function on our local fn Project server. Furthermore, the function version got increased automatically within our development environment. As the last command shows it is now reachable under http://localhost:8080/r/targetedMarketing/searchProduct. So let us call the function using curl.

Congratulations, you just created your first function using Oracle fn Project and it does what we intended to do. Compared to the grep solution from my previous post the result actually improved as it produced valid JSON Output. Its structure is identical to the input JSON structure. So lets tackle our second function, extractEmail. Create a new project just like before and replace the generated code:

As one can see in the code, yet again the same BookingRecords structure is used for input. Now let us build and deploy this function as well.

We deployed the second function to the same application. As result we now have two routes within this application. If we now call our extractEmail function with the same data as searchProduct it will return a JSON Array containing all 1000 email addresses within this file. However, our requirement was to identify certain customers. In order to fulfil this requirement we need to chain our functions. As searchProduct uses the same input and output structure, this is no problem and we can reuse shell pipes in order to achieve our desired result.

The command line above uses curl to call our searchProduct function and get a list of identified customers. Afterwards the result is piped into another curl command and extractEmail is called, which returns two email addresses. We now implemented the same functionality using the same principles as in the previous post. However this time we used a FaaS platform to fulfil our requirements. As you can see pipes and filters can be easily applied to FaaS development and just feels right.

However, in our current solution we still do the piping using shell functionality. Of course this does not seem right when aiming for enterprise application development. So how is the piping requirement addressed within fn Project? This topic will be elaborated in my next post.

Further Reading

Blog Post 1: Function as a Service (FaaS)
fn project – Oracle’s open FaaS Platform
Source Code Examples on GitHub.com