Serverless Groovy! Yeah!

David Delabassee
Sep 10, 2018 · 2 min read
Image for post
Image for post

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.

function.groovy
Dockerfile that will run the Groovy function

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.

Image for post
Image for post
Photo by Felix Dubois-Robert

Fn Project

Learn about the Fn Project, containers, and serverless…

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store