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:
- Create local files that contain the basic configuration for a sample "Hello World" Python Function (
kyma init function
). - Generate a Function custom resource (CR) from these files and apply it on your cluster (
kyma apply function
). - Fetch the current state of your Function's cluster configuration after it was modified (
kyma sync function
).
NOTE: Read about Istio sidecars in Kyma and why you want them. Then, check how to enable automatic Istio sidecar proxy injection. For more details, see Default Istio setup in Kyma.
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:
- Docker
- Kyma CLI
- Kyma installed locally or on a cluster
Steps
Follow these steps:
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:NOTE: Learn why you want sidecar and how to enabled them. Follow Istio sidecars in Kyma and why you want them, Default Istio setup in Kyma and Enable automatic Istio sidecar proxy injection
Click to copykyma 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" logicrequirements.txt
with an empty file for your Function's custom dependenciesThis command also sets sourcePath in the
config.yaml
file to the full path of the workspace folder:Click to copyname: my-functionnamespace: defaultruntime: python39source:sourceType: inlinesourcePath: {FULL_PATH_TO_WORKSPACE_FOLDER}
Run the
apply
Kyma CLI command to create a Function CR in the YAML format on your cluster:Click to copykyma apply functionTIP: To apply a Function from a different location, use the
--filename
flag followed by the full path to theconfig.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
.Once applied, view the Function's details on the cluster:
Click to copykubectl describe function {FUNCTION_NAME}Change the Function's source code on the cluster to return "Hello Serverless!":
a) Edit the Function:
Click to copykubectl edit function {FUNCTION_NAME}b) Modify source as follows:
Click to copy...spec:runtime: python39source: |-def main(event, context):return "Hello Serverless!"Fetch the content of the resource to synchronize your local workspace sources with the cluster changes:
Click to copykyma sync function {FUNCTION_NAME}Check the local
handler.py
file with the Function's code to make sure that the cluster changes were fetched:Click to copycat handler.pyThis command returns the result confirming that the local sources were synchronized with cluster changes:
Click to copydef main(event, context):return "Hello Serverless!"