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
    • Start testing with double trial credits
    • Cloud credits to scale your IT product
    • Gateway to Russia
    • Cloud for Startups
    • Education and Science
    • Yandex Cloud Partner program
  • Blog
  • Pricing
  • Documentation
© 2025 Direct Cursus Technology L.L.C.
Tutorials
    • All tutorials
    • Architecture and protection of a basic internet service
    • Cost analysis by resource using Object Storage
      • Configuring a fault-tolerant architecture in Yandex Cloud
      • Integrating an L7 load balancer with Cloud CDN and Object Storage
        • Overview
        • Management console, CLI, and API
        • Terraform
      • Autoscaling an instance group to process messages enqueued in Message Queue
      • Updating an instance group under load
      • Creating a budget trigger that invokes a function to stop a VM
      • Deploying a fault-tolerant architecture with preemptible VMs
      • Creating triggers that invoke a function to stop a VM and send a Telegram notification

In this article:

  • Get your cloud ready
  • Required paid resources
  • Create the auxiliary resources
  • Create a service account
  • Create a cloud network
  • Create an instance group
  • Create Cloud Functions resources
  • Create a function
  • Create a trigger
  • Test instance group scaling
  • How to delete the resources you created
  1. Basic infrastructure
  2. Fault tolerance and scaling
  3. Scheduled instance group scaling
  4. Management console, CLI, and API

Scheduled scaling of instance groups using the management console, CLI, and API

Written by
Yandex Cloud
Updated at May 13, 2025
  • Get your cloud ready
    • Required paid resources
  • Create the auxiliary resources
    • Create a service account
    • Create a cloud network
  • Create an instance group
  • Create Cloud Functions resources
    • Create a function
    • Create a trigger
  • Test instance group scaling
  • How to delete the resources you created

To set up scheduled scaling for an instance group:

  1. Get your cloud ready.
  2. Create an instance group.
  3. Create Cloud Functions resources.
  4. Test instance group scaling.

If you no longer need the resources you created, delete them.

Get your cloud readyGet your cloud ready

Sign up in Yandex Cloud and create a billing account:

  1. Navigate to the management console and log in to Yandex Cloud or register a new account.
  2. On the Yandex Cloud Billing page, make sure you have a billing account linked and it has the ACTIVE or TRIAL_ACTIVE status. If you do not have a billing account, create one and link a cloud to it.

If you have an active billing account, you can navigate to the cloud page to create or select a folder for your infrastructure to operate in.

Learn more about clouds and folders.

In our example, we will use a folder named example-folder.

Required paid resourcesRequired paid resources

The infrastructure support costs include:

  • Fee for disks and continuously running VMs (see Compute Cloud pricing).
  • Function calls, computing resources allocated to executing the function, and outgoing traffic (see Cloud Functions pricing).

Create the auxiliary resourcesCreate the auxiliary resources

Create a service accountCreate a service account

The service account will be linked to the Compute Cloud instance group and the Cloud Functions function.

Management console
CLI
API
  1. In the management console, select the example-folder folder.
  2. At the top of the screen, navigate to the Service accounts tab.
  3. At the top right, click Create service account.
  4. In the Name field, specify vm-scale-scheduled-sa.
  5. Click Add role and select these roles:
    • compute.editor: To manage the instance group.
    • iam.serviceAccounts.user: To link the service account to the group’s VM instances.
    • functions.functionInvoker: To call the Cloud Functions function.
  6. Click Create.
  1. Create a service account:

    yc iam service-account create --name vm-scale-scheduled-sa \
      --folder-name example-folder
    

    Result:

    id: ajeab0cnib1p********
    folder_id: b0g12ga82bcv********
    created_at: "2022-03-15T09:44:35.989446Z"
    name: vm-scale-scheduled-sa
    

    For more information about the yc iam service-account create command, see the CLI reference.

  2. Assign these roles to the service account:

    • compute.editor: To manage the instance group:

      yc resource-manager folder add-access-binding example-folder \
        --service-account-name vm-scale-scheduled-sa \
        --role compute.editor \
        --folder-name example-folder
      
    • iam.serviceAccounts.user: To link the service account to the group’s instances:

      yc resource-manager folder add-access-binding example-folder \
        --service-account-name vm-scale-scheduled-sa \
        --role iam.serviceAccounts.user \
        --folder-name example-folder
      
    • functions.functionInvoker: To call the Cloud Functions function:

      yc resource-manager folder add-access-binding example-folder \
        --service-account-name vm-scale-scheduled-sa \
        --role functions.functionInvoker \
        --folder-name example-folder
      

    For more information about the yc resource-manager folder add-access-binding command, see the CLI reference.

  1. Create a service account named vm-scale-scheduled-sa. To do this, use the create REST API method for the ServiceAccount resource or the ServiceAccountService/Create gRPC API call.

  2. Assign the service account these roles for the current folder:

    • compute.admin: To manage the instance group.
    • iam.serviceAccounts.user: To link the service account to the group’s VM instances.
    • functions.functionInvoker: To call the Cloud Functions function.

    To do this, use the setAccessBindings REST API method for the Folder resource or the FolderService/SetAccessBindings gRPC API call.

Create a cloud networkCreate a cloud network

The instance group will reside in the Yandex Virtual Private Cloud network and its subnets.

Management console
CLI
API
  1. In the management console, select example-folder.
  2. From the list of services, select Virtual Private Cloud.
  3. At the top right, click Create network.
  4. In the Name field, specify vm-scale-scheduled-network.
  5. Select Create subnets.
  6. Click Create network.
  1. Create a network named vm-scale-scheduled-network:

    yc vpc network create --name vm-scale-scheduled-network \
      --folder-name example-folder
    

    Result:

    id: enpabce123hd********
    folder_id: b0g12ga82bcv********
    created_at: "2022-03-15T09:55:50.004472Z"
    name: vm-scale-scheduled-network
    

    For more information about the yc vpc network create command, see the CLI reference.

  2. Create a subnet named vm-scale-scheduled-subnet-a in the ru-central1-a availability zone:

    yc vpc subnet create --name vm-scale-scheduled-subnet-a \
      --folder-name example-folder \
      --network-name vm-scale-scheduled-network --range 192.168.1.0/24 \
      --zone ru-central1-a
    

    Result:

    id: e1lnabc23r1c********
    folder_id: b0g12ga82bcv********
    created_at: "2022-03-15T09:56:51.859345Z"
    name: vm-scale-scheduled-subnet-a
    network_id: enpabce123hd********
    zone_id: ru-central1-a
    v4_cidr_blocks:
    - 192.168.1.0/24
    

    For more information about the yc vpc subnet create command, see the CLI reference.

  3. Create a subnet named vm-scale-scheduled-network-b in the ru-central1-b availability zone:

    yc vpc subnet create --name vm-scale-scheduled-subnet-b \
      --folder-name example-folder \
      --network-name vm-scale-scheduled-network --range 192.168.2.0/24 \
      --zone ru-central1-b
    

    Result:

    id: b1csa2b3clid********
    folder_id: b0g12ga82bcv********
    created_at: "2022-03-15T09:57:48.934429Z"
    name: vm-scale-scheduled-subnet-b
    network_id: enpabce123hd********
    zone_id: ru-central1-b
    v4_cidr_blocks:
    - 192.168.2.0/24
    
  1. Create a network named vm-scale-scheduled-network using the create REST API method for the Network resource or the NetworkService/Create gRPC API call.
  2. Create subnets named vm-scale-scheduled-subnet-a in the ru-central1-a availability zone and vm-scale-scheduled-subnet-b in the ru-central1-b availability zone using the create REST API method for the Subnet resource or the SubnetService/Create gRPC API call.

Create an instance groupCreate an instance group

We will create an instance group with manual scaling so that the Cloud Functions function can manage the group size.

Management console
CLI
API
  1. In the management console, select example-folder.
  2. In the list of services, select Compute Cloud.
  3. In the left-hand panel, select Instance groups.
  4. At the top right, click Create group of virtual machines.
  5. Under Basic parameters:
    • In the Name field, specify vm-scale-scheduled-ig.
    • Select Service account vm-scale-scheduled-sa.
  6. Under Allocation, select ru-central1-a and ru-central1-b in the Availability zone field.
  7. Under Instance template, click Define and do the following in the window that opens:
    • Under Boot disk image, navigate to the Boot disk image tab and select Ubuntu 20.04.
    • Under Computing resources, specify this configuration:
      • Platform: Intel Ice Lake
      • vCPU: 2
      • Guaranteed vCPU performance: 20%
      • RAM: 2 GB
    • Under Network settings:
      • In the Network field, select vm-scale-scheduled-network.
      • In the Public address field, select No address.
    • Under Access:
      • In the Service account field, select vm-scale-scheduled-sa.
      • In the Login field, enter the name of the user to create on the VM. You can enter any username of your choice.
      • In the SSH key field, paste the public SSH key contents. You can create a key pair by following this guide.
    • Click Save.
  8. Under Changes during creation and updates, specify this configuration:
    • Add above target value: 2
    • Reduce below target value: 2
    • Create simultaneously: 2
    • Stop simultaneously: 2
  9. Under Scaling:
    • In the Type field, select Fixed.
    • Specify Size 2.
  10. Click Create.
  1. Get the IDs of the resources you created in the previous steps:

    • vm-scale-scheduled-sa service account ID:

      yc iam service-account get vm-scale-scheduled-sa \
        --folder-name example-folder | grep "^id"
      

      Result:

      id: ajeab0cnib1p********
      

      For more information about the yc iam service-account get command, see the CLI reference.

    • vm-scale-scheduled-network ID:

      yc vpc network get vm-scale-scheduled-network \
        --folder-name example-folder | grep "^id"
      

      Result:

      id: enpabce123hd********
      

      For more information about the yc vpc network get command, see the CLI reference.

    • vm-scale-scheduled-subnet-a ID:

      yc vpc subnet get vm-scale-scheduled-subnet-a \
        --folder-name example-folder | grep "^id"
      

      Result:

      id: e1lnabc23r1c********
      

      For more information about the yc vpc subnet get command, see the CLI reference.

    • vm-scale-scheduled-subnet-b ID:

      yc vpc subnet get vm-scale-scheduled-subnet-b \
        --folder-name example-folder | grep "^id"
      

      Result:

      id: b1csa2b3clid********
      

      For more information about the yc vpc subnet get command, see the CLI reference.

  2. Get the ID of the latest version of the Ubuntu 20.04 LTS public image:

    yc compute image get-latest-from-family ubuntu-2004-lts \
      --folder-id standard-images | grep "^id"
    

    Result:

    id: fd83n3uou8m0********
    

    For more information about the yc compute image get-latest-from-family command, see the CLI reference.

  3. Insert these IDs in the specification.yaml file with the VM group spec:

    name: vm-scale-scheduled-ig
    service_account_id: <service_ account_ID>
    allocation_policy:
       zones:
         - zone_id: ru-central1-a
         - zone_id: ru-central1-b
    instance_template:
      platform_id: standard-v3
      resources_spec:
        memory: 2G
        cores: 2
        core_fraction: 20
      boot_disk_spec:
        mode: READ_WRITE
        disk_spec:
          type_id: network-hdd
          size: 15G
          image_id: <ID_of_Ubuntu_20.04_LTS_latest_version>
      network_interface_specs:
       - network_id: <network_ID>
         subnet_ids:
           - <subnet_ID_in_ru-central1-a_zone>
           - <subnet_ID_in_ru-central1-b_zone>
         primary_v4_address_spec: {}
    scale_policy:
      fixed_scale:
        size: 2
    deploy_policy:
      max_unavailable: 2
      max_creating: 2
      max_expansion: 2
      max_deleting: 2
    
  4. Create an instance group named vm-scale-scheduled-ig based on specification.yaml:

    yc compute instance-group create --file specification.yaml \
      --folder-name example-folder
    

    Result:

    done (2m18s)
    id: cl0kabcquk1g********
    folder_id: b0g12ga82bcv********
    ...
    service_account_id: ajeab0cnib1p********
    status: ACTIVE
    
  1. Get the IDs of the resources you created in the previous steps:

    • vm-scale-scheduled-sa service account ID: Using the get REST API method for the ServiceAccount resource or the ServiceAccountService/Get gRPC API call.
    • vm-scale-scheduled-network ID: Using the get REST API method for the Network resource or the NetworkService/Get gRPC API call.
    • vm-scale-scheduled-subnet-a and vm-scale-scheduled-subnet-b IDs: Using the get REST API method for the Subnet resource or the SubnetService/Get gRPC API call.
  2. Get the ID of the latest version of the public Ubuntu 20.04 image in the standard-images family using the getLatestByFamily REST API method for the Image resource or the ImageService/GetLatestByFamily gRPC API call.

  3. Insert these IDs in the specification.yaml file with the VM group spec:

    name: vm-scale-scheduled-ig
    service_account_id: <service_ account_ID>
    allocation_policy:
       zones:
         - zone_id: ru-central1-a
         - zone_id: ru-central1-b
    instance_template:
      platform_id: standard-v3
      resources_spec:
        memory: 2G
        cores: 2
        core_fraction: 20
      boot_disk_spec:
        mode: READ_WRITE
        disk_spec:
          type_id: network-hdd
          size: 15G
          image_id: <ID_of_Ubuntu_20.04_LTS_latest_version>
      network_interface_specs:
       - network_id: <network_ID>
         subnet_ids:
           - <subnet_ID_in_ru-central1-a_zone>
           - <subnet_ID_in_ru-central1-b_zone>
         primary_v4_address_spec: {}
    scale_policy:
      fixed_scale:
        size: 2
    deploy_policy:
      max_unavailable: 2
      max_creating: 2
      max_expansion: 2
      max_deleting: 2
    
  4. Create an instance group named vm-scale-scheduled-ig based on specification.yaml. To do this, use the createFromYaml REST API method for the InstanceGroup resource or the InstanceGroupService/CreateFromYaml gRPC API call.

Create Cloud Functions resourcesCreate Cloud Functions resources

Create a functionCreate a function

The function will contain the code with the Yandex Cloud CLI commands used to update the instance group.

Management console
CLI
API
  1. In the management console, select example-folder.

  2. Select Cloud Functions.

  3. At the top right, click Create function.

  4. In the Name field, specify vm-scale-scheduled-function.

  5. Click Create.

  6. Select the Bash runtime environment.

  7. Enable Add files with code examples.

  8. Click Continue.

  9. Under Editor, select the handler.sh file.

  10. Replace its contents with the code below:

    # Get ID and current size of the instance group
    IG_SPEC=$(yc compute instance-group get --name $IG_NAME --folder-id $FOLDER_ID --format json)
    IG_ID=$(jq -r ".id" <<< $IG_SPEC)
    IG_SIZE=$(jq -r ".scale_policy.fixed_scale.size" <<< $IG_SPEC)
    
    # Calculate new size for the instance group
    if [ $IG_SIZE = $IG_BASE_SIZE ]; then
        IG_SIZE="$(($IG_BASE_SIZE + 1))"
    else
        IG_SIZE=$IG_BASE_SIZE
    fi
    
    # Update the instance group
    yc compute instance-group update --id $IG_ID --scale-policy-fixed-scale-size $IG_SIZE
    
  11. Under Parameters:

    • Set the Timeout field value to 60.
    • In the Service account field, select vm-scale-scheduled-sa.
    • Under Environment variables, add these variables:
      • IG_NAME = vm-scale-scheduled-ig.
      • IG_BASE_SIZE = 2.
      • FOLDER_ID = <your_folder_ID>. You can get the ID by following this guide.
  12. In the top-right corner, click Save changes.

  1. Create a file named handler.sh with this code:

    # Get ID and current size of the instance group
    IG_SPEC=$(yc compute instance-group get --name $IG_NAME --folder-id $FOLDER_ID --format json)
    IG_ID=$(jq -r ".id" <<< $IG_SPEC)
    IG_SIZE=$(jq -r ".scale_policy.fixed_scale.size" <<< $IG_SPEC)
    
    # Calculate new size for the instance group
    if [ $IG_SIZE = $IG_BASE_SIZE ]; then
        IG_SIZE="$(($IG_BASE_SIZE + 1))"
    else
        IG_SIZE=$IG_BASE_SIZE
    fi
    
    # Update the instance group
    yc compute instance-group update --id $IG_ID --scale-policy-fixed-scale-size $IG_SIZE
    
  2. Create a function:

    yc serverless function create vm-scale-scheduled-function \
      --folder-name example-folder
    

    Result:

    done (1s)
    id: d4e7d67ikvmq********
    folder_id: b1g9hv2loamq********
    ...
    log_group_id: ckgij6l0845h********
    http_invoke_url: https://functions.yandexcloud.net/d4e7d67ikvmq********
    status: ACTIVE
    
  3. Create a function version:

    yc serverless function version create --folder-name example-folder \
      --function-name vm-scale-scheduled-function --runtime bash \
      --source-path ./handler.sh --entrypoint "handler.sh" \
      --execution-timeout 60s \
      --service-account-id <service_account_ID> \
      --environment \
        IG_NAME=vm-scale-scheduled-ig,IG_BASE_SIZE=2,FOLDER_ID=<folder_ID>
    

    Where:

    • --service-account-id: vm-scale-schedule-sa ID. To get the ID, follow the Create a VM group step on the CLI tab.
    • FOLDER_ID in --environment. You can get the ID by following this guide.

    Result:

    done (1s)
    id: d4efmv9n3qf1********
    function_id: d4e7d67ikvmq********
    ...
      FOLDER_ID: b1g9hv2loamq********
      IG_BASE_SIZE: "2"
      IG_NAME: vm-scale-scheduled-ig
    
  1. Create a function named vm-scale-scheduled-function using the create REST API method for the Function resource or the FunctionService/Create gRPC API call. From the output, get the ID of the function you created.

  2. Create a function version using the createVersion REST API method for the Function resource or the FunctionService/CreateVersion gRPC API call. The function version should have this code:

    # Get ID and current size of the instance group
    IG_SPEC=$(yc compute instance-group get --name $IG_NAME --folder-id $FOLDER_ID --format json)
    IG_ID=$(jq -r ".id" <<< $IG_SPEC)
    IG_SIZE=$(jq -r ".scale_policy.fixed_scale.size" <<< $IG_SPEC)
    
    # Calculate new size for the instance group
    if [ $IG_SIZE = $IG_BASE_SIZE ]; then
        IG_SIZE="$(($IG_BASE_SIZE + 1))"
    else
        IG_SIZE=$IG_BASE_SIZE
    fi
    
    # Update the instance group
    yc compute instance-group update --id $IG_ID --scale-policy-fixed-scale-size $IG_SIZE
    

Create a triggerCreate a trigger

A trigger sets conditions for running a function. For scheduled function calls, use a timer trigger.

Management console
CLI
API
  1. In the management console, select example-folder.
  2. Select Cloud Functions.
  3. In the left-hand panel, select Triggers.
  4. At the top right, click Create trigger.
  5. Under Basic settings:
    • In the Name field, specify vm-scale-scheduled-trigger.
    • In the Type field, select Timer.
    • In the Launched resource field, select Function.
  6. Under Timer settings, in the Cron expression field, specify */2 * * * ? *. The trigger will fire every two minutes, e.g., at 09:58, 10:00, 10:02, 10:04, etc.
  7. Under Function settings:
    • In the Function field, specify vm-scale-scheduled-function.
    • In the Function version tag field, select $latest.
    • Select Service account vm-scale-scheduled-sa.
  8. Under Dead Letter Queue settings, reset the Service account (Not selected) field.
  9. Click Create trigger.

Run this command:

yc serverless trigger create timer --folder-name example-folder \
  --name vm-scale-scheduled-trigger \
  --cron-expression "*/2 * * * ? *" \
  --invoke-function-name vm-scale-scheduled-function \
  --invoke-function-tag '$latest' \
  --invoke-function-service-account-name vm-scale-scheduled-sa

Result:

id: a1s22pb0v5i4********
folder_id: b1g9hv2loamq********
created_at: "2022-04-06T14:56:54.253709098Z"
...
      function_tag: $latest
      service_account_id: aje9s4pud13h********
status: ACTIVE

Create a timer trigger with the */2 * * * ? * cron expression by using the create REST API method for the Trigger resource or the TriggerService/Create gRPC API call. This trigger will be linked to vm-scale-scheduled-function of the $latest version and the vm-scale-scheduled-sa service account.

Test instance group scalingTest instance group scaling

Management console
CLI
API
  1. In the management console, select example-folder.
  2. In the list of services, select Compute Cloud.
  3. In the left-hand panel, select Instance groups.
  4. Select the vm-scale-scheduled-ig group.
  5. Under VM states, make sure the number of instances changes every two minutes: increases from 2 to 3, then decreases from 3 to 2, etc. You can also check if the instance group has been updated by opening the Operations tab.

Run the following command several times:

yc compute instance-group get vm-scale-scheduled-ig \
  --folder-name example-folder

Result:

id: cl1l0ljqbmkp********
folder_id: b1g9hv2loamq********
created_at: "2022-03-28T13:24:20.693Z"
...
managed_instances_state:
  target_size: "2"
  running_actual_count: "2"
...

The value of the target_size field for the group should change from 2 to 3 and back.

Get information about the vm-scale-scheduled-ig instance group multiple times using the get REST API method for the InstanceGroup resource or the InstanceGroupService/Get gRPC API call. The value of the target_size field for the group should change from 2 to 3 and back.

How to delete the resources you createdHow to delete the resources you created

To stop paying for the resources you created:

  1. Delete the trigger.
  2. Delete the function.
  3. Delete the instance group.

See alsoSee also

  • Scheduled scaling of VM groups using Terraform

Was the article helpful?

Previous
Overview
Next
Terraform
© 2025 Direct Cursus Technology L.L.C.