Yandex Cloud
Search
Contact UsGet started
  • Blog
  • Pricing
  • Documentation
  • All Services
  • System Status
    • Featured
    • Infrastructure & Network
    • Data Platform
    • Containers
    • Developer tools
    • Serverless
    • Security
    • Monitoring & Resources
    • ML & AI
    • Business tools
  • All Solutions
    • By industry
    • By use case
    • Economics and Pricing
    • Security
    • Technical Support
    • Customer Stories
    • Gateway to Russia
    • Cloud for Startups
    • Education and Science
  • Blog
  • Pricing
  • Documentation
Yandex project
© 2025 Yandex.Cloud LLC
Yandex Cloud Functions
  • Comparison with other Yandex Cloud services
    • Overview
    • Managing dependencies
    • Request handler
    • Invocation context
    • Logging
    • Handling errors
    • Using the SDK
  • Tools
  • Pricing policy
  • Access management
  • Terraform reference
  • Monitoring metrics
  • Audit Trails events
  • Release notes
  • FAQ
  1. Developing in Kotlin
  2. Using the SDK

Using the SDK for Kotlin functions

Written by
Yandex Cloud
Updated at May 12, 2025

The runtime environment does not have a pre-installed library for accessing the Yandex Cloud API. To use the library, add a dependency to your Kotlin application. The library source code is available on GitHub.

Warning

From the java-sdk-serverless and java-sdk-functions modules, only java-sdk-serverless is available for Kotlin. There are no restrictions on other modules.

The SDK (Software Development Kit) helps you manage Yandex Cloud resources on behalf of the service account specified in the function parameters.

ExampleExample

The following function gets the folder ID (folderId) as an input, gets authorized in the SDK, retrieves a list of all Compute Cloud VMs in the specified folder, and restarts those that are stopped. As a result, it returns a message with the number of running instances.

Invoke the function using the Yandex Cloud CLI or an HTTP request with the ?integration=raw parameter.

import yandex.cloud.api.compute.v1.InstanceOuterClass
import yandex.cloud.api.compute.v1.InstanceServiceGrpc
import yandex.cloud.api.compute.v1.InstanceServiceOuterClass
import yandex.cloud.sdk.ServiceFactory
import yandex.cloud.sdk.auth.Auth

fun handle(folderId: String): String {
    // Getting authorized in the SDK via the service account
    val defaultComputeEngine = Auth.computeEngineBuilder().build()
    val factory = ServiceFactory.builder()
        .credentialProvider(defaultComputeEngine)
        .build()
    val instanceService = factory.create(
        InstanceServiceGrpc.InstanceServiceBlockingStub::class.java,
        InstanceServiceGrpc::newBlockingStub
    )
    val listInstancesRequest =
        InstanceServiceOuterClass.ListInstancesRequest.newBuilder().setFolderId(folderId).build()
    // Getting a list of VMs based on `folderId` specified in the request
    val instances = instanceService.list(listInstancesRequest).instancesList
    var count = 0
    for (instance in instances) {
        if (instance.status != InstanceOuterClass.Instance.Status.RUNNING) {
            val startInstanceRequest =
                InstanceServiceOuterClass.StartInstanceRequest.newBuilder().setInstanceId(instance.id).build()
            // Starting a VM with IDs specified in the request
            val startInstanceResponse = instanceService.start(startInstanceRequest)
            if (!startInstanceResponse.hasError()) {
                count++
            }
        }
    }
    return "Started $count instances"
}

Was the article helpful?

Previous
Handling errors
Next
Overview
Yandex project
© 2025 Yandex.Cloud LLC