Working with serverless functions

After completing this lesson, you will be able to:

After completing this lesson, you will be able to:

  • Describe briefly the serverless functions
  • Identify appropriate situations for using serverless functions
  • Use different ways to invoke a serverless function
  • Create a serverless function

What are serverless functions?

A function is a serverless workload type which is provided by the project "Kyma" through an custom resource (CR). "Serverless" does not mean that there is no server or infrastructure involved. It simply means that the server is managed by a third party and you do not have to worry about it. You can focus on the code of your function.

Technically, Functions in Kyma relies on multiple Kubernetes resources such as Deployments, Services, HorizontalPodAutoscalers, and Jobs.

So in terms of serverless functions in Kyma, you can simply run code snippets without providing and maintaining the container image or the infrastructure. And like deployments based on containers, functions can also be scaled , exposed and subscribed to events.

Pros and cons of serverless functions

Click on the "Pro" and "Con" buttons to find out more.

Invoking serverless functions

Functions can be invoked in different ways. The following table shows the different ways to invoke a function:

Ways, to invoke a function

Invocation MethodDescription
HTTPThe function is invoked by an HTTP request. The endpoint must be exposed first.
EventThe function is invoked by an event.

The following image illustrates how the function is invoked by an Event:

Creating serverless functions

You can create a function either by declaring a Function manifest in YAML or by using the visual editor in the Kyma Dashboard. The visual editor will generate the Function manifest for you.

Defining a function manifest

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

A function manifest (version v1alhpa2) in YAML for a simple hello world example might look like this:

Code snippet
kind: Function
  name: hello-kyma-function
    app: hello-kyma-function
  runtime: nodejs16
      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}!`;
             return message;
    - name: NAME
      value: "Kyma"
Copy code

As you can see above, the ${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 manifest

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:

Spec part of the manifest

FieldDescriptionPossible Values
spec.runtimeThe runtime of the function.nodejs16, nodejs14, python39
spec.sourceThe specification of the function.inline, gitRepository
spec.source.sourceThe source code of the function. 
spec.source.dependenciesThe dependencies of the function. 
spec.envThe environment variables of the function. 

To get a full list of the fields, see the Function CR documentation. There are many more fields that you can use to configure scaling, resources, gitRepository and many more.

Using the Kyma Dashboard

On the Dashboard, you can navigate to the Functions section and click the Create Function button. The following image shows the form to create a function:

After creating a function, you can use the web editor to create the function code. The following image shows the web editor:

Learning how to expose a function or subscribe a function to an event will be covered in other units of this course.


Functions are a good choice if you want to run small pieces of code, for example, to process data or to respond to events. You can split your microservice into smaller functions if you want to scale it differently. Functions also supports the event-driven architecture and ensures 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

Find more information about serverless functions here:

Save progress to your learning plan by logging in or creating an account

Login or Register