Create your first serverless function with Red Hat OpenShift Serverless Functions

Create your first serverless function with Red Hat OpenShift Serverless Functions

Serverless is a powerful and popular paradigm where you don’t have to worry about managing and maintaining your application infrastructure. In the serverless context, a function is a single-purpose piece of code created by the developer but run and monitored by the managed infrastructure. A serverless function’s value is its simplicity and swiftness, which can entice even those who don’t consider themselves developers.

This article introduces you to Red Hat OpenShift Serverless Functions, a new developer preview feature in Red Hat OpenShift Serverless 1.11. I will provide an overview, then present two example applications demonstrating Serverless Functions with Node.js. Please check the OpenShift Serverless Functions Quick Start document for the example prerequisites.

OpenShift Serverless Functions

Red Hat OpenShift Serverless leverages the power of Knative to deliver serverless, event-driven applications that scale on demand. With the OpenShift Serverless 1.11 release, we have added the new Serverless Functions feature, currently available as a developer preview. Serverless Functions comes with pre-defined templates and runtimes and provides a local developer experience. Together, these features make it very easy to create serverless applications.

How to get Serverless Functions

Serverless Functions is bundled with the OpenShift Serverless command-line interface (CLI), kn. When you use an OpenShift Serverless Operator for installation, OpenShift Serverless is automatically deployed, and managed on OpenShift. You can access Serverless Functions with the following command:

$ kn func

Note: See the OpenShift Serverless documentation for installation instructions.

What’s included?

Serverless Functions comes with predefined runtimes for popular languages such as Quarkus, Node.js, and Go. These runtimes are based on Cloud Native Buildpacks. After you choose a runtime, Serverless Functions creates the appropriate project scaffolding so that you can focus on writing business logic. Serverless Functions also includes a local developer experience to support a quick inner loop of iterative development and testing.

Invoking Serverless Functions

You can invoke Serverless Functions using plain HTTP requests or CloudEvents with OpenShift Serverless eventing components. OpenShift Serverless Functions comes with out-of-the-box project templates to jumpstart your code for both the HTTP and CloudEvents trigger types.

Next, we’ll explore two examples. For the first example, we’ll configure Serverless Functions for HTTP requests. For the second example, we’ll use CloudEvents. Please use the Serverless Functions quick start document to ensure that you have the example prerequisites installed.

Example 1: Create a serverless function for HTTP requests

Once you have the prerequisites installed, create a new directory for your serverless function. Once you are in the directory, execute the following command to create and deploy a new serverless function:

$  kn func create 

By default, the function is initialized with a project template for plain HTTP requests. You can choose your programming language by entering Node.js, Quarkus, or Go as the value for the -l flag. If you do not provide a runtime with the -l flag, the default runtime is Node.js. We’ll use Node.js for both of our examples.

Note: You can use the -c flag to prompt the CLI to guide you in creating your first function through the interactive developer experience, which prompts you to add the language and event values. Type -help anytime for assistance.

The Node.js runtime

By default, entering the command $  kn func create creates the scaffolding for a function that is triggered by a plain HTTP request. The scaffolding for our default Node.js runtime includes index.js, package.json, and func.yaml files. We can extend the index.js base code to develop our serverless function.

As a start, let’s add a return message of Greeting <username> in the provided handleGet(context) method. Figure 1 shows the handleGet function in index.js.

The handleGet function accepts a context object and returns the word 'Greetings' with the name provided. If no name is provided, the word 'stranger' is inserted instead.

Figure 1: The handleGet(context) function.

Deploy the function

Next, we’ll deploy this function to our OpenShift cluster. Be sure that you are logged into an OpenShift cluster from your local environment, then type the following command with the project name or cluster namespace:

$ kn func deploy  -n <namespace>

Remember that you can use the -c flag for an interactive experience.

Serverless Functions will prompt you to provide a container registry where the resulting image is uploaded. DockerHub is the default registry, but you can use any public image registry.

Now, go to the Topology view in the OpenShift developer console. You will see your function deployed as a Knative service, as shown in Figure 2.

The Topology view lets you see the serverless function deployed on your OpenShift cluster. The highlighted routes make it easy to access the deployed function from your browser.

Figure 2: View the deployed serverless function on your OpenShift cluster.

Test the function

We can use the routes URL shown in Figure 2 to test our deployed serverless function. Enter the following command to delete the function from your cluster:

$ kn func delete

For a local developer experience, we can test serverless functions using standard language tooling or in a container running locally. Use the following command on the kn command-line to build the container image:

$ kn func build

To test the built image container in a local environment, enter:

$ kn func run 

Use the curl command to test your deployed image:

$ curl ‘https://localhost:8080/?name=Universe’

You may also use the browser to see the results, as shown in Figure 3.

Localhost on port 8080 is being accessed with the name 'Universe' as a data for the deployed serverless function's 'get' method. The browser displays 'Greetings Universe!'

Figure 3: The deployed function being called from the browser.

Example 2: Create a serverless function for CloudEvents

For our second example, we’ll create a serverless function that responds to CloudEvents rather than HTTP requests. Before you start, please check the quick start document to ensure that you have the prerequisites installed for this example.

Create a new serverless function project

We’ll use the same command that we used previously to create a new project. This time, however, we will provide an events value for the -t flag. Alternatively, we could use the -c flag for interactive prompts.

$  kn func create -l <node|quarkus> -t  events  

To receive CloudEvents, we will need Knative eventing components, so we’ll set that up next.

Log in to the OpenShift developer console and navigate to the Developer perspective. Click the Add section to see the Channel tile highlighted in Figure 4. This tile creates a default channel.

The Channel is highlighted in red.

Figure 4: Locate the Channel tile in the OpenShift developer console (notice the tile in red).

Now, we need an event source. For that, we will go back to the Add section and click on the Event Source tile shown in Figure 5.

The Event Source tile is highlighted in red.

Figure 5: Locate the Event Source tile in the OpenShift developer console.

Next, as shown in Figure 6, we will select and configure a ping source as the event source for our deployed function. Note that the Sink section displays the deployed function and the channel we’ve just created. For this example, we will choose the channel as the sink for our event source.

Sink options for the ping event source are the previously created channel and the deployed OpenShift serverless function.

Figure 6: Select Channel as the sink option for the ping event source.

After creating the event source, we can view all the components in the Topology view, as shown in Figure 7.

The topology view shows the serverless function, event source, and channel components.

Figure 7: The deployed serverless function, event source, and channel.

To add a trigger to the deployed function, hover over the channel, then click and drag the blue line to connect the channel to the function. Figure 8 shows the full deployment details in the Topology view.

The serverless function is connected to the event source via the channel. The serverless function pod is active and receiving events.

Figure 8: Connect the serverless function to the event source via the channel.

When the function starts receiving events, Knative spins up the function pod, and the logs show the call to the function. We have just created and deployed an OpenShift serverless function.

Looking forward

OpenShift Serverless Functions is available as a developer preview in OpenShift Serverless 1.11. It is available to all OpenShift users. We will release new features in the coming months, and your feedback is greatly appreciated.

This article is the first in a series introducing Serverless Functions. The next article in this series will introduce you to creating serverless functions with Quarkus, the supersonic, subatomic Java runtime. In the meantime, you can learn more about OpenShift Serverless Functions by reading the OpenShift Serverless 1.11 release announcement, the OpenShift Serverless documentation, and the OpenShift Serverless Functions documentation.