Working with Serverless Functions

Objectives

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.

Serverless Functions Overview

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
Copy code
Switch to dark mode
1234567891011121314151617181920212223242526272829
apiVersion: serverless.kyma-project.io/v1alpha2 kind: Function metadata: name: hello-kyma-function labels: app: hello-kyma-function spec: runtime: nodejs16 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

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

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:

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.

Summary

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:

Log in to track your progress & complete quizzes