Skip to Content

Create Kyma Function Using Kyma CLI

In this tutorial, you will use Kyma CLI to create a Kyma Function.
You will learn
  • How to install and use Kyma CLI
  • How to create a Kyma Function
Created by
jacekon
December 17, 2024
Contributors
nataliasitko
grego952
jacekon

Prerequisites

  • Step 1
  • Step 2
    1. Enable the Istio sidecar proxy injection in the default namespace:

      bash/Powershell
      Copy
      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, it is required to include the workload in the Istio service mesh.

    2. In your terminal, go to your working folder, and run:

      bash/Powershell
      Copy
      kyma function init
      

      If successful, you get the following message:
      Functions files of runtime nodejs22 initialized to dir {WORKING_FOLDER_PATH}

      You have created 2 files in your working folder:

      • handler.js
      • package.json

      You can check the content of the files with your editor (e.g. Visual Studio Code).

    3. To apply your Function to your Kyma runtime, run:

      bash/Powershell
      Copy
      kyma function create hello-function
      

      If successful, you get the following message:

      bash/Powershell
      Copy
      resource default/hello-function applied
      
    4. To verify the Function deployment, run:

      bash/Powershell
      Copy
      kyma function get hello-function
      

      If successful, you get the following result:

      bash/Powershell
      Copy
      NAME             CONFIGURED   BUILT   RUNNING   RUNTIME    GENERATION
      hello-function   True         True    True      nodejs22   1
      
  • Step 3
    1. In your working folder, create a new YAML file named myapirule.yaml. You can do this using a text editor or by running touch myapirule.yaml in your terminal.

    2. Open the file in your editor and add the following APIRule definition:

      yaml
      Copy
      apiVersion: gateway.kyma-project.io/v2
      kind: APIRule
      metadata:
        name: hello-rule
        namespace: default
      spec:
        hosts:
          - hello-host
        service:
          name: hello-function
          port: 80
        gateway: kyma-system/kyma-gateway
        rules:
          - path: /*
            methods: ["GET", "POST"]
            noAuth: true
      
    3. Deploy your APIRule.

      bash/Powershell
      Copy
      kubectl apply -f "{PATH_TO_YOUR_CONFIG_FILE}"
      

      For example:

      bash/Powershell
      Copy
      kubectl apply -f "C:\tools\myapirule.yaml"
      
  • Step 4
Back to top