Creating and Testing Serverless Functions

Objective

After completing this lesson, you will be able to create and test a serverless function

Create and Test a Serverless Function

Usage Scenario

You are a developer who needs to create an API to receive and process data. You have heard of the approach to use Serverless Functions to implement such an API, so you want to learn how to create a Serverless Function and how to use it.

Task Flow

In this exercise, you will perform the following steps:

  1. Create a Serverless Function using kubectl.
  2. Create a Serverless Function using Kyma dashboard.
  3. Expose and test the Function using HTTP.

Prerequisites

Task 1: Create and Test a Serverless Function

Steps

  1. Create a Serverless Function using kubectl.

    1. Open your terminal and list all current Serverless Functions in your default namespace:

      Code Snippet
      1
      kubectl get functions

      If your namespace has no Functions, you get an empty list.

    2. Enable the Istio sidecar proxy injection in the default namespace.

      Code Snippet
      1
      kubectl label namespace default istio-injection=enabled --overwrite

      If you create a Function in a namespace with enabled Istio sidecar injection, an Istio sidecar proxy is automatically injected to the Function's Pod during its creation. This makes the Function part of the Istio service mesh. To expose a workload using an APIRule custom resource, which you will do in step 3, it is required to include the workload in the Istio service mesh.

    3. To create a Serverless Function, run the following command:

      Code Snippet
      1
      kubectl apply -n default -f https://raw.githubusercontent.com/sap-samples/kyma-runtime-learning-journey/main/unit_3/functions/hello-world-function.yaml

      If successful, you get the output: function.serverless.kyma-project.io/hello-world-function created.

    4. List all s in the default namespace again:

      Code Snippet
      1
      kubectl get functions

      The Function hello-world-function is on the list.

  2. Create a Serverless Function using Kyma dashboard.

    1. Open Kyma dashboard, go to Namespaces, and choose default.

    2. Go to WorkloadsFunctions and choose Create.

    3. Enter the following information:

      • Name: hello-kyma-function
      • Language: JavaScript
      Create a new function

      You can also switch to the YAML tab to see, copy, or edit the YAML manifest of the Function.

    4. Choose the Create button.

      Result

      Your function is now being created. After a few seconds, it has the status Running.

  3. Expose and test the Function using HTTP.

    Note

    Exposing the Function using HTTP allows you to test it in a browser. You will find out more about exposure later in the course.
    1. Go to the Kyma Environment section of your subaccount overview to check the default domain of your Kyma cluster.

      Copy the value of APIServerURL after https://api. For example, for the URL https://api.xxx123.kyma.ondemand.com, copy xxx123.kyma.ondemand.com.

    2. Go to Discovery and NetworkAPI Rules and choose Create.

    3. Enter the following information and choose the Create button:

      • Name: hello-kyma-function-api
      • Service Name: hello-kyma-function
      • Port: 80
      • Host: hello-kyma-function-api.<your-domain>

        Replace <your-domain> with the copied domain name.

      • Access Strategy: No Auth

      By creating this APIRule CR, you expose your function as an HTTP endpoint. Only GET requests are allowed.

    4. Paste the URL hello-kyma-function-api.<your-domain> in your browser.

      Result

      You get the following message:

      Hello world from the Kyma Function hello-kyma-function running on nodejs20!

Result

Bravo, you have successfully created and tested a Serverless Function!

Log in to track your progress & complete quizzes