Creating a function in Kotlin
Create and run a user welcome function in Kotlin.
Before you start
Create a folder in Yandex Cloud.
Create a function
- In the management console
, select the folder where you want to create a function. - Select Cloud Functions.
- Click Create function.
- Enter the function name:
kotlin-function
. - Click Create.
If you do not have the Yandex Cloud CLI yet, install and initialize it.
The folder specified in the CLI profile is used by default. You can specify a different folder through the --folder-name
or --folder-id
parameter.
To create a function, run the command:
yc serverless function create --name=kotlin-function
Result:
id: d4eek526ai42********
folder_id: b1gt6g8ht345********
created_at: "2024-12-19T20:02:56.039Z"
name: kotlin-function
http_invoke_url: https://functions.yandexcloud.net/d4eek526ai42********
status: ACTIVE
You can create a function using the create.
You can create a list of function versions using the Yandex Cloud Toolkit plugin
Create the first version of the function
To create a function version, you can use one of the code upload formats. A ZIP archive will be used for the example.
Warning
Files larger than 3.5 MB should be uploaded via Object Storage. For more information about limits, see Quotas and limits in Cloud Functions.
Prepare a ZIP archive with the function code
-
Save the following code to a file named
Handler.kt
:data class Request( val httpMethod: String?, val headers: Map<String, String> = mapOf(), val body: String = "" ) data class Response( val statusCode: Int, val body: String ) fun handle(request: Request): Response { return Response(200, "Hello World!") }
-
Add the
Handler.kt
file into thehello-kotlin.zip
archive.
Create a function version
- In the management console
, select the folder containing the function. - Select Cloud Functions.
- Select the
kotlin-function
function. - Under Last version, click Сreate in editor.
- Select the
Kotlin 2.0
runtime environment. - Disable Add files with code examples and click Continue.
- Set the version parameters:
- Method:
ZIP archive
- File: Attach
hello-kotlin.zip
- Entry point:
Handler
- Timeout, sec:
3
- Memory:
128 MB
- Service account:
Not selected
- Method:
- Click Save changes.
If you do not have the Yandex Cloud CLI yet, install and initialize it.
The folder specified in the CLI profile is used by default. You can specify a different folder through the --folder-name
or --folder-id
parameter.
To create a function version, run the command:
yc serverless function version create \
--function-name=kotlin-function \
--runtime kotlin20 \
--entrypoint Handler \
--memory 128m \
--execution-timeout 3s \
--source-path ./hello-kotlin.zip
Where:
--function-name
: Name of the function whose version you want to create.--runtime
: Runtime environment.entrypoint
: Entry point in<function_file_name>.<handler_name>
format.--memory
: Amount of RAM.--execution-timeout
: Maximum function running time before the timeout is reached.--source-path
: ZIP archive with the function code and required dependencies.
Result:
done (10s)
id: d4ektme8jo2d********
function_id: d4e9arrf01oh********
created_at: "2024-12-19T20:22:15.942Z"
runtime: kotlin20
entrypoint: Handler
resources:
memory: "134217728"
execution_timeout: 3s
image_size: "4096"
status: ACTIVE
tags:
- $latest
log_options:
folder_id: b1gt6g8ht345********
concurrency: "1"
You can create a function version using the createVersion.
You can create a function version using the Yandex Cloud Toolkit plugin
Invoke the function
Note
To allow any user to invoke your function, make it public. For more information about access rights, see Access management in Cloud Functions.
-
In the management console
, select the folder containing the function. -
Select Cloud Functions.
-
Select a function.
-
Go to the
Testing tab. -
In the Version tag field, specify
$latest
to invoke the latest function version. -
In the Payload template field, select
No template
. -
In the Payload field, enter:
{}
-
Click
Run test. -
You will see the testing status under Test result in the Function status field. Important: Maximum function execution time before timeout (including original initialization at first call) is 10 minutes.
-
You will see the function execution result in the Function output field:
{ "statusCode": 200, "body": "Hello World!" }
To invoke a function, run the command:
yc serverless function invoke <function_ID> -d '{}'
Result:
{"statusCode":200,"body":"Hello World!"}
The function version with the $latest
tag is invoked by default.
You can view the function invocation link on the Overview tab, in the Link to invoke field.
For security reasons, you can only invoke a function via HTTPS. Invoke it as a regular HTTP request by pasting the link into the browser address bar and adding the name
parameter to the URL:
https://functions.yandexcloud.net/<function_ID>
The following response will appear on the page:
Hello World!
You can invoke a function using the Yandex Cloud Toolkit plugin
What's next
- For more information about the structure of functions invoked in different ways (HTTP or CLI), see Invoking a function in Cloud Functions.
- Read about service concepts.
- For information about what you can do with functions and their versions, see our step-by-step instructions.