Serverless Groovy! Yeah!

Written on September 10, 2018

Groovy Logo

Right before my vacation, I had a short exchange with Guillaume Laforge, the Apache Groovy project PMC Chair, about running Groovy functions on Fn.

The bad news is that my vacations are behind me and that I didn’t have the time to prototype Groovy support thanks to my too-long-list-of-things-to-absolutely-do-before-going-away! Now I am back and the good news is that I was able, in a few hours, to add Groovy functions to Fn thanks to the new init-image approach!

As explained here, all we need is to provide a Docker image that will generate the required files for the function. For Groovy, it is simply a ‘hello world’ Groovy function, a bare-bone func.init.yaml and a Dockerfile that will run the given Groovy function using Docker, eg. using the official groovy:alpine image.

You can get all the files to create the Groovy init-image here. The first thing to do is to build the Docker Groovy init-image.

docker build -t groovy-init -f Dockerfile-groovy-init .

You can then create a Groovy function using that Groovy init-image.

fn init —init-image=groovy-init myfunc

and finally run the Groovy function…

echo "Dr. Evil" | fn run myfunc

In Fn, functions are running inside containers. To communicate with Fn, Groovy functions are simply using the container’s STDIN to read the request and its STDOUT to return the response. Given that Groovy also runs on top of the Java Virtual Machine it would probably makes more sense to leverage the Java FDK like the Kotlin image does instead of using the more limited STDIN/STDOUT approach. By doing so, Groovy functions would also get some of the Java FDK features for free! See here for more details on benefits the Java FDK can brings and here for an initial version of the Kotlin/Java FDK init-image.

Fn does not officially support Groovy but this minimalistic PoC demonstrates that it is trivial to add unsupported languages to Fn. All you need to know is how that language works and a little bit of Docker, nothing more! The init-image approach is a recent Fn enhancement, i.e. it might evolve, but it clearly provides several benefits like the ability to generate more dynamic boilerplates for functions, the ability to add custom images without having to hack the Fn CLI, etc.

Originaly posted on Medium.