Working with Serverless Functions

Objectives

After completing this lesson, you will be able to:
  • Describe the Serverless Functions
  • Identify the appropriate scenarios for using Serverless Functions
  • Use different methods to invoke a Serverless Function
  • Create a Serverless Function

Serverless Functions Overview

What Are Serverless Functions?

A Function is a workload type that Kyma provides. "Serverless" implies no server or infrastructure is involved. But that's wrong. It simply means that a third party manages the server, and you do not have to worry about it. You can focus on the code of your Function.

Technically, Functions in Kyma rely on multiple Kubernetes resources, such as Deployments and Services.

So, with Serverless Functions in Kyma, you can run code snippets without providing and maintaining the container image or the infrastructure. Like Deployments based on containers, Functions can also be scaled, exposed, and subscribed to events.

Pros and Cons of Serverless Functions

Pros
  • Serverless Functions are helpful when you want to reduce your application's implementation and operation effort. You only care about the code and the dependencies of your Function. Kyma manages the rest. This ensures quick deployments of your code.
  • Developers can deploy lightweight Serverless Functions without needing to set up complete applications, which enables rapid prototyping. Functions support seamless integration with SAP BTP services through service bindings, making it easy to consume capabilities such as SAP HANA, Event Mesh, or Object Store. This accelerates innovation by letting teams experiment and iterate faster within the SAP BTP ecosystem.
  • Serverless Functions also support the asynchronous programming model and offer loose coupling of event producers and consumers.
Cons
  • However, Serverless Functions are limited to the supported runtime environments of Kyma. With self-managed containers, you can use any language and environment you want.
  • Functions are also vendor-specific. In other words, you must develop your Functions specifically for a particular provider. As a result, Functions are less portable compared to containers.

Invoking Serverless Functions

The following table shows two different ways to invoke a Function:

Invoking a Function

Invocation MethodDescription
HTTPAn HTTP request invokes the Function. The endpoint must be exposed first.
EventThe Function is invoked by an event.

The following image illustrates how an Event invokes a Function:

An event-driven architecture within a Kubernetes cluster using Kyma. It shows an Event Publisher sending events to a Proxy, which forwards them to an Event Controller that triggers a Serverless Function.

Creating Serverless Functions

You can create a Function by declaring a Function manifest in a YAML file or using Kyma dashboard's visual editor. The visual editor generates the Function manifest for you.

Describing a Function Manifest

With Kyma, you can create Functions in Node.js and Python. Kyma extends Kubernetes with a custom resource (CR) for this workload type.

See a Function manifest (version v1alhpa2) in YAML for a simple hello-kyma-function example:

Code Snippet
1234567891011121314151617181920212223242526272829
apiVersion: serverless.kyma-project.io/v1alpha2 kind: Function metadata: name: hello-kyma-function labels: app: hello-kyma-function spec: runtime: nodejs22 source: inline: dependencies: | { "name": "hello-kyma-function", "version": "1.0.0", "dependencies": {} } source: |- module.exports = { main: async function (event, context) { const message = `Hello ` + process.env.NAME + ` from the Kyma Function ${context["function-name"]}` + ` running on ${context.runtime}!`; console.log(message); return message; } } env: - name: NAME value: "Kyma"

Note

In the Function manifest, ${context["function-name"]} is a runtime variable that is injected by Kyma. It contains the name of the Function. This is an example of the vendor lock-in, that we mentioned earlier. Using such components makes your code less portable.

The following table shows the different fields of the manifest:

Fields of the Function manifest

FieldDescription
apiVersionThe version of the custom resource.
kindThe kind of the custom resource.
metadataThe metadata of the custom resource.
specThe specification of the custom resource

Let's break down the spec part of the manifest:

Function CR Spec

FieldDescription
spec.runtimeThe runtime of the Function. For example, nodejs22, nodejs20, python312.
spec.sourceThe Function’s source code configuration. For example, inline or gitRepository.
spec.source.inlineThe full source code of the Function defined inline, directly in the Function CR.
spec.source.dependenciesThe dependencies of the Function.
spec.envAn array of key-value pairs used as environment variables for the Function.

There are many more fields that you can use to configure your Function. To get the full list of the fields, see the Function CR documentation.

Summary

Serverless Functions are a good choice if you want to run less code, for example, to process data or respond to events. You can quickly prototype new ideas by deploying lightweight Functions and integrating them with SAP BTP services. Functions also support the event-driven architecture and ensure quick deployments of your code. However, you have to consider that Functions are vendor-specific and that you are limited to the supported runtime environments of Kyma.

Further Reading about Serverless Functions