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

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

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

For example:For example:

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

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 runs 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 generates
    // data required for authorization in the SDK using this token
    Credentials: ycsdk.InstanceServiceAccount(),
  })
  if err != nil {
    return nil, err
  }
  // Getting the Compute Instance list by the 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 using the filter: disabled, tags contain 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 filtering 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 file:

module example

go 1.14

Was the article helpful?

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