Creating and Testing Serverless Functions

Objectives
After completing this lesson, you will be able to:

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.

Exercise Options

You can perform this exercise in two ways:

  1. Platform Simulation – follow the step-by-step instructions within the simulation.
  2. Live Environment - by using the instructions provided below, you can perform the steps in the SAP BTP Free Tier account.
Note
We strongly recommend first performing the steps in the Live Environment.

Platform Simulation

Choose the Start Exercise button below to open a simulation of the platform. Then follow the step-by-step instructions to create and test a serverless function.

Live Environment

In this exercise, you will perform the following steps:

  1. Create a serverless function via kubectl.
  2. Create a serverless function via the Kyma Dashboard.
  3. Expose and test the function via HTTP.

Prerequisites

  • You have successfully created a SAP BTP, Kyma runtime instance in the SAP BTP subaccount.
  • You have configured kubectl to work with your SAP BTP, Kyma runtime instance.

Task 1: Create a serverless function via kubectl

Steps

  1. Create a serverless function via kubectl.

    1. Open your terminal and list all current serverless functions in your default namespace:

      Code snippet
      kubectl get functions
      Copy code

      Your namespace has no functions, so it should return an empty list.

    2. Create a serverless function using the following command:

      Code snippet
      kubectl apply -n default -f https://raw.githubusercontent.com/kyma-runtime-learning-journey/main/unit_3/functions/hello-world-function.yaml
      Copy code
    3. List all serverless functions in your namespace again:

      Code snippet
      kubectl get functions
      Copy code

      You should see the function you just created.

  2. Create a Serverless Function via the Dashboard.

    Use the following data:

    FieldValue
    Namehello-kyma-function
    RuntimeNode.js 14
    SourceInline editor

    1. Open the Kyma Dashboard and navigate to the functions workload view.

    2. Select your default namespace in the namespace dropdown menu.

    3. Choose the Create Function button.

      A pop-up window opens.

    4. Enter the information from the table above into the Advanced tab.

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

    5. Choose the Create button.

      Your function is now being created.

  3. Expose and test the function via HTTP.

    Note
    We expose the function via HTTP so that we can test it via 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 Service DiscoveryAPI Rules and choose Create API Rule.

      This opens a pop-up window.

    2. Enter the following information on the Simple tab and choose on the Create button:

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

    3. Navigate back to the Function Workloads view and open the hello-kyma-function function.

    4. Open the endpoint URL in your browser and choose the URL in the API Rules view.

    5. You should see the following message:

      Hello World from the Kyma Function main running on nodejs14!

Result

Bravo, you have successfully created and tested a serverless function!

Log in to track your progress & complete quizzes