Yandex Cloud
Search
Discuss with expertTry it for free
  • Customer Stories
  • Documentation
  • Blog
  • All Services
  • System Status
  • Marketplace
    • Featured
    • Infrastructure & Network
    • Data Platform
    • AI for business
    • Security
    • DevOps tools
    • Serverless
    • Monitoring & Resources
  • All Solutions
    • By industry
    • By use case
    • Economics and Pricing
    • Security
    • Technical Support
    • Start testing with double trial credits
    • Cloud credits to scale your IT product
    • Gateway to Russia
    • Cloud for Startups
    • Center for Technologies and Society
    • Yandex Cloud Partner program
    • Price calculator
    • Pricing plans
  • Customer Stories
  • Documentation
  • Blog
© 2026 Direct Cursus Technology L.L.C.
Yandex Cloud Functions
  • Comparing with other Yandex Cloud services
    • Overview
    • Managing dependencies
    • Request handler
    • Invocation context
    • Logging
    • Error handling
    • Using the SDK
  • Tools
  • Pricing policy
  • Access management
  • Terraform reference
  • Monitoring metrics
  • Audit Trails events
  • Public materials
  • Release notes
  • FAQ
  1. Developing in Java
  2. Using the SDK

Using the SDK for a function in Java

Written by
Yandex Cloud
Updated at July 2, 2026
View in Markdown

The runtime does not have a pre-installed library for accessing the Yandex Cloud API. To use it, add a dependency to your Java application. The library source code is available on GitHub. The availability of library modules depends on your language version:

  • java-sdk-functions: Only for Java 11.
  • java-sdk-serverless: For Java 17 or higher.

Other modules are available for any Java version.

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

ExampleExample

The following function gets the folder ID (folderId), 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 VMs.

Warning

To invoke the function, use 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;

import java.util.function.Function;

public class Handler implements Function<String, String> {
  @Override
  public String apply(String folderId) {
    // Getting authorized in the SDK via the service account
    var defaultComputeEngine = Auth.computeEngineBuilder().build();
    var factory = ServiceFactory.builder()
            .credentialProvider(defaultComputeEngine)
            .build();
    var instanceService = factory.create(InstanceServiceGrpc.InstanceServiceBlockingStub.class, InstanceServiceGrpc::newBlockingStub);
    var listInstancesRequest = InstanceServiceOuterClass.ListInstancesRequest.newBuilder().setFolderId(folderId).build();
    // Getting the list of VMs by `folderId` specified in the request
    var listInstancesResponse = instanceService.list(listInstancesRequest);
    var instances = listInstancesResponse.getInstancesList();
    var count = 0;
    for (var instance : instances) {
      if (instance.getStatus() != InstanceOuterClass.Instance.Status.RUNNING) {
        var startInstanceRequest = InstanceServiceOuterClass.StartInstanceRequest.newBuilder().setInstanceId(instance.getId()).build();
        // Starting VMs with IDs specified in the request
        var startInstanceResponse = instanceService.start(startInstanceRequest);
        if (!startInstanceResponse.hasError()) {
          count++;
        }
      }
    }
    return String.format("Started %d instances", count);
  }
}

Was the article helpful?

Previous
Error handling
Next
Overview
© 2026 Direct Cursus Technology L.L.C.