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

Business Scenario

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

Note

We strongly recommend first performing the steps in the live environment.

Live Environment

In this exercise, you will perform the following steps:

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

Prerequisites

  • You have successfully created a SAP BTP, Kyma runtime instance in the SAP BTP subaccount.
  • You have added the Serverless module. See Adding Kyma Modules.
  • You have configured kubectl to work with your SAP BTP, Kyma runtime instance.

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. Create a Serverless Function using 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.

    3. List all Serverless Functions in your 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.

    Use the following data:

    FieldValue
    Namehello-kyma-function
    Runtimeuse the latest supported version
    SourceInline editor

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

    2. Go to WorkloadsFunctions and choose Create.

      The Create Function view opens.

    3. Enter the following information:

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

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

    4. Choose the Create button.

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

  3. Expose and test the Function using HTTP.

    Note

    We expose the Function using HTTP so that we can test it in a browser. You will find out more about exposure later in the course.

    Use the following data:

    FieldValue
    Namehello-kyma-function-api
    Servicehello-kyma-function (port: 80)
    1. Navigate to Discovery and NetworkAPI Rules and choose Create.

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

      • Name: hello-kyma-function-api
      • Service Name: hello-kyma-function
      • Choose the host of your Kyma cluster, and replace the * wildcard character with the subdomain hello-kyma-function .
      • Access Strategy: No Auth

      This will expose your function as an HTTP endpoint. Only GET requests are allowed.

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

    4. 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