Wrapping a executable in a function

Easy function wrapping

Published August 30, 2021 #openfaas, #cli

Lets look at how to wrap a command line program into a function. We'll assume that you already have an OpenFaaS service running somewhere.

First create the template

faas new --lang dockerfile myfunction

Calling the function

I'm going to write a command in ruby so I'll include things from the ruby:3.0.1 base image.

The main thing here is that I'm copying the script into /usr/local/bin and I'm changing the fprocess ENV variable to be xargs gitinfo.rb which will pass in the standard input as command line arguments.

  FROM ghcr.io/openfaas/classic-watchdog:0.1.5 as watchdog

  FROM ruby:3.0.1

  RUN apt-get update && apt-get install -y cloc libsqlite3-dev

  COPY --from=watchdog /fwatchdog /usr/bin/fwatchdog
  RUN chmod +x /usr/bin/fwatchdog

  WORKDIR /home/app

  # Add non root user
  RUN useradd app
  RUN chown app /home/app

  COPY Gemfile* ./
  RUN bundle install
  COPY *rb /usr/local/bin/
  RUN chmod +x /usr/local/bin/*rb

  USER app

  # Populate example here - i.e. "cat", "sha512sum" or "node index.js"
  ENV fprocess="xargs gitinfo.rb"
  # Set to true to see request in function logs
  ENV write_debug="false"

  EXPOSE 8080

  HEALTHCHECK --interval=3s CMD [ -e /tmp/.lock ] || exit 1

  CMD ["fwatchdog"]

The script looks like this:

  #!/usr/bin/env ruby

  puts "This is the info file"

  ARGV.each do |v,i|
    puts "#{i}: #{v}"

Running without an argument

curl https://api.gitgratitude.com/function/gitinfo
This is the info file

Passing in arguments

Here we can pass in data to the function:

echo Hello world | curl -X POST --data @- https://api.gitgratitude.com/function/gitinfo
This is the info file
: Hello
: world

Async test

I'm going to use pipedream.com as a way to collect a response. Go there and sign up for it if you haven't, and then set the CALLBACK_URL to be resulting address.

In order to have a function run asyncronously, you need the change the url to start with /async-function/ and pass in an X-Callback-URL header where the result is posted.

  echo Hello world | curl -X POST \
                          -H "X-Callback-Url: ${CALLBACK_URL}" \
                          --data @- \


Very simple, very little was changed from the below posts below.


  1. https://blog.alexellis.io/cli-functions-with-openfaas/

  2. https://github.com/openfaas/workshop/blob/master/lab7.md

Read next

See also

Deploying OpenFaaS on Digital Ocean with Terraform

Everything functional

We are going to look at how to use Terraform to deploy a Kubernetes cluster on Digital Ocean, add a managed postgres database, and redis and OpenFaaS in kubernetes. This will show how to use Terraform to manage the configuration and how we can access both cloud and kubernetes managed services from OpenFaaS functions. We are going to use the digitalocean, kubernetes, and helm terraform providers. The plan Provision a digitalocean_kubernetes_cluster Provision a digitalocean_database_cluster Provision 2 kubernetes_namespace for openfaas and openfaas-fn Provision a helm_release for openfaas Provision a helm_release for redis Provision 2 kubernetes_secret to point to the databases Deploy an OpenFaaS function that reads those secrets and talks to the database.

Read more

Building static OpenFaas templates

Packaging up the packager

I've been playing with OpenFaaS recently and it's a very accessable way to starting building cloud first services. I wanted to see what I could cram in there, so I built a few templates that would let me host a static site. One that is just html, and another than can be built with something like create-react-app. Static Create the template directory: mkdir -p template/static Then add a template/static/template.

Read more