Manage Functions with Kyma CLI

This tutorial shows how to use the available CLI commands to manage Functions in Kyma. You will see how to:

  1. Create local files that contain the basic configuration for a sample "Hello World" Python Function (kyma init function).
  2. Generate a Function custom resource (CR) from these files and apply it on your cluster (kyma apply function).
  3. Fetch the current state of your Function's cluster configuration after it was modified (kyma sync function).

This tutorial is based on a sample Python Function run on a lightweight k3d cluster.

Prerequisites

Before you start, make sure you have these tools installed:

Steps

Follow these steps:

  1. Run the init Kyma CLI command to create local files with the default configuration for a Python Function. Go to the folder in which you want to initiate the workspace content and run this command:

    Click to copy
    kyma init function --runtime python39 --name {FUNCTION_NAME}

    Alternatively, use the --dir {FULL_FOLDER_PATH} flag to point to the directory where you want to create the Function's source files.

    NOTE: Python 3.9 is only one of the available runtimes. Read about all supported runtimes and sample Functions to run on them.

    The init command creates these files in your workspace folder:

  • config.yaml with the Function's configuration

NOTE: See the detailed description of all fields available in the config.yaml file.

  • handler.py with the Function's code and the simple "Hello World" logic
  • requirements.txt with an empty file for your Function's custom dependencies

    This command also sets sourcePath in the config.yaml file to the full path of the workspace folder:

    Click to copy
    name: my-function
    namespace: default
    runtime: python39
    source:
    sourceType: inline
    sourcePath: {FULL_PATH_TO_WORKSPACE_FOLDER}
  1. Run the apply Kyma CLI command to create a Function CR in the YAML format on your cluster:

    Click to copy
    kyma apply function

    TIP: To apply a Function from a different location, use the --filename flag followed by the full path to the config.yaml file.

    Alternatively, use the --dry-run flag to list the file that will be created before you apply it. You can also preview the file's content in the format of your choice by adding the --output {FILE_FORMAT} flag, such as --output yaml.

  2. Once applied, view the Function's details on the cluster:

    Click to copy
    kubectl describe function {FUNCTION_NAME}
  3. Change the Function's source code on the cluster to return "Hello Serverless!":

    a) Edit the Function:

    Click to copy
    kubectl edit function {FUNCTION_NAME}

    b) Modify source as follows:

    Click to copy
    ...
    spec:
    runtime: python39
    source: |-
    def main(event, context):
    return "Hello Serverless!"
  4. Fetch the content of the resource to synchronize your local workspace sources with the cluster changes:

    Click to copy
    kyma sync function {FUNCTION_NAME}
  5. Check the local handler.py file with the Function's code to make sure that the cluster changes were fetched:

    Click to copy
    cat handler.py

    This command returns the result confirming that the local sources were synchronized with cluster changes:

    Click to copy
    def main(event, context):
    return "Hello Serverless!"