Writing a Shell Function

On This Page

This guide uses practical examples to guide you through the process of writing serverless shell functions.

Overview

The shell runtime allows function developers to fork a process on every received event. Developers can choose to provide an executable script or run any executable binary in the Docker image. This guide walks you through both scenarios.

Handle events with a bash script

This example guides you through the steps for deploying a shell script that reverses the event’s body. To implement this, you call rev and pass stdin as input; (the event body will appear to shell functions as stdin).

Create a /tmp/nuclio-shell-script/reverser.sh file with the following code:

Handle events with any executable binary

Because the shell runtime simply forks a process, you can leverage it to run any executable binary in the Docker image. This means that you don’t need to provide any code to the shell runtime, only a function configuration. In this example, you install the ImageMagick utility and call its convert executable on each event. You then send the function images and use convert to reduce the image by 50% in the response. You do this by invoking the nuctl CLI as follows:

--handler convert — the handler must be set to the name or path of the executable. In this example, convert is in the environment PATH so there’s no need for a full path.

--runtime-attrs '{"arguments": "- -resize 50% fd:1"}' — through runtime specific attributes, you specify the arguments for the executable. In this example, - instructs the runtime to read from stdin, and the rest of the arguments specify how to convert the received image.

Overriding the arguments per request

The shell runtime allows events to override the default arguments through the use of a header. This means that you can supply x-nuclio-arguments as a header and provide any convert arguments that you wish, per event. Thus, you can create, for example, a smaller thumbnail file by using the following invocation command; replace the <function ip:port> placeholder with your function-URL information: