Yandex.Cloud
  • Services
  • Why Yandex.Cloud
  • Solutions
  • Pricing
  • Documentation
  • Contact us
Get started
Yandex Cloud Functions
  • Getting started
    • Overview
    • Creating a function
      • Overview
      • Python
      • Node.js
      • PHP
      • Go
      • Bash
    • Creating a trigger
      • Overview
      • Timer
      • Trigger for Message Queue
      • Trigger for Object Storage
      • Trigger for Container Registry
      • Trigger for Cloud Logs
      • Trigger for Yandex IoT Core
  • Step-by-step instructions
    • All instructions
    • Using functions to get an IAM token for a service account
    • Managing rights to access functions
    • Managing functions
      • Creating a function
      • Managing function versions
      • Working in the code editor
      • Invoking a function
      • Updating a function
      • View monitoring charts
      • Viewing the execution log
      • Delete function
    • Managing triggers
      • Getting information about a trigger
      • Creating a timer
      • Creating a trigger for Message Queue
      • Creating a trigger for Object Storage
      • Creating a trigger for Container Registry
      • Creating a trigger for Cloud Logs
      • Creating a trigger for Yandex IoT Core
      • Updating a trigger
      • Deleting a trigger
  • Concepts
    • Overview
    • Function
    • Invoking a function
    • Runtime environment
      • Overview
      • Environment
      • Execution context
    • Builder
    • Trigger
      • Overview
      • Timer
      • Trigger for Message Queue
      • Trigger for Object Storage
      • Trigger for Container Registry
      • Trigger for Cloud Logs
      • Trigger for Yandex IoT Core
    • Dead Letter Queue
    • Log groups
    • Monitoring
    • Backups
    • Quotas and limits
  • Developing in Node.js
    • Overview
    • Managing dependencies
    • Request handler
    • Invocation context
    • Logging
    • Handling errors
    • Using the SDK
  • Developing in Python
    • Overview
    • Managing dependencies
    • Request handler
    • Invocation context
    • Logging
    • Handling errors
    • Using the SDK
  • Developing in Go
    • Overview
    • Managing dependencies
    • Request handler
    • Invocation context
    • Logging
    • Handling errors
    • Using the SDK
  • Developing in PHP
    • Overview
    • Managing dependencies
    • Request handler
    • Invocation context
    • Logging
    • Handling errors
  • Developing in Bash
    • Overview
    • Request handler
    • Logging
    • Handling errors
    • Using the SDK
  • Developing in Java
    • Overview
    • Programming model
      • Overview
      • Function interface
      • YcFunction interface
      • HttpServlet class
      • Spring Boot
    • Managing dependencies
    • Request handler
    • Invocation context
    • Logging
    • Handling errors
    • Using the SDK
  • Developing in R
    • Overview
    • Programming model
    • Managing dependencies
    • Request handler
    • Invocation context
    • Logging
    • Handling errors
  • Developing in C#
    • Overview
    • Programming model
      • Overview
      • Function interface
      • YcFunction interface
    • Managing dependencies
    • Request handler
    • Invocation context
    • Logging
    • Handling errors
    • Using the SDK
  • Use cases
    • Creating skills for Alice
  • Pricing policy
  • Access management
  • API Functions reference
    • Authentication in the API
    • gRPC
      • Overview
      • FunctionService
      • OperationService
    • REST
      • Overview
      • Function
        • Overview
        • create
        • createVersion
        • delete
        • get
        • getVersion
        • getVersionByTag
        • list
        • listAccessBindings
        • listOperations
        • listRuntimes
        • listTagHistory
        • listVersions
        • removeTag
        • setAccessBindings
        • setTag
        • update
        • updateAccessBindings
  • API Triggers reference
    • Authentication in the API
    • gRPC
      • Overview
      • TriggerService
      • OperationService
    • REST
      • Overview
      • Trigger
        • Overview
        • create
        • delete
        • get
        • list
        • listOperations
        • pause
        • resume
        • update
  • Questions and answers
  1. Developing in Go
  2. Using the SDK

Using the SDK

    The runtime doesn't have a pre-installed library that lets you access the Yandex.Cloud API.To use the library, add a dependency to your application. You can find the source code of the library on GitHub.

    Software development kits (SDK) let you interact with Yandex.Cloud services using the service account specified in the function.

    Example:

    The following function receives a request with two fields (FolderId and Tag) as an input, authorizes 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

    Invoke the function using the YC 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
    }
    

    The go.mod file:

    module example
    
    go 1.14
    
    Language / Region
    Careers
    Privacy policy
    Terms of use
    Brandbook
    © 2021 Yandex.Cloud LLC