Using the SDK for Java functions
The runtime environment does not have a pre-installed library for working with the Yandex Cloud API. To use the library, add a dependency to your Java application. The library source code is available on GitHub
java-sdk-functions
: Only for Java 11java-sdk-serverless
: Java 17 or higher
Other modules are available for any version of Java.
SDK (Software Development Kit)
Example
The following function receives the folderId
as an input, authorizes in the SDK, gets a list of all Compute Cloud instances in the specified folder, and restarts stopped instances. As a result, it returns a message with the number of running instances.
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) {
// SDK authorization using 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();
// Retrieving Compute Instance list based on FolderId in 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();
// Launching Compute Instance with specifid ID
var startInstanceResponse = instanceService.start(startInstanceRequest);
if (!startInstanceResponse.hasError()) {
count++;
}
}
}
return String.format("Started %d instances", count);
}
}