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 Go
  2. Using the SDK

Using the SDK for a function in Go

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 Go application. The library source code is available on GitHub.

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

ExampleExample

The following function receives a request with two fields (FolderId and Tag), gets authorized in the SDK, retrieves a list of all Compute Cloud VMs in the specified folder, filters them by the specified tag, 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.

package main

import (
  "context"
  "fmt"
  "github.com/yandex-cloud/go-genproto/yandex/cloud/compute/v1"
  "github.com/yandex-cloud/go-genproto/yandex/cloud/operation"
  "github.com/yandex-cloud/go-sdk"
)

func startComputeInstance(ctx context.Context, sdk *ycsdk.SDK, id string) (*operation.Operation, error) {
  // Operation that starts the compute instance with the specified ID
  return sdk.Compute().Instance().Start(ctx, &compute.StartInstanceRequest{
    InstanceId: id,
  })
}

type Request struct {
  FolderId string `json:"folderId"`
  Tag      string `json:"tag"`
}

type Response struct {
  StatusCode int         `json:"statusCode"`
  Body       interface{} `json:"body"`
}

func StartComputeInstances(ctx context.Context, request *Request) (*Response, error) {
  // Authorization in the SDK using a service account
  sdk, err := ycsdk.Build(ctx, ycsdk.Config{
    // Calling the InstanceServiceAccount method automatically requests an IAM token and uses it to generate
    // data required for authorization in the SDK.
    Credentials: ycsdk.InstanceServiceAccount(),
  })
  if err != nil {
    return nil, err
  }
  // Getting the compute instance list by FolderId specified in the request
  listInstancesResponse, err := sdk.Compute().Instance().List(ctx, &compute.ListInstancesRequest{
    FolderId: request.FolderId,
  })
  if err != nil {
    return nil, err
  }
  instances := listInstancesResponse.GetInstances()
  count := 0
  // Filtering the compute instance list: stopped instances containing the tag specified in the request.
  for _, i := range instances {
    labels := i.Labels
    if _, ok := labels[request.Tag]; ok && i.Status != compute.Instance_RUNNING {
      // Running the compute instances that meet the filter criteria
      _, err := startComputeInstance(ctx, sdk, i.GetId())
      if err != nil {
        return nil, err
      }
      count++
    }
  }
  return &Response{
    StatusCode: 200,
    Body:       fmt.Sprintf("Started %d instances", count),
  }, nil
}

go.mod:

module example

go 1.14

Was the article helpful?

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