Scheduled scaling of instance groups
Create a Yandex Compute Cloud instance group and set up scheduled scaling for it. A Yandex Cloud Functions function triggered by a timer will manage the instance group size.
To set up scaling for your instance group using standard tools such as the management console, CLI, or API:
- Prepare your cloud.
- Create auxiliary resources.
- Create an instance group.
- Create Cloud Functions resources.
- Test instance group scaling.
If you no longer need the resources you created, delete them.
You can also deploy an infrastructure for scaling your instance group via Terraform using a ready-made configuration file.
Prepare your cloud
Sign up for Yandex Cloud and create a billing account:
- Go to the management console
and log in to Yandex Cloud or create an account if you do not have one yet. - On the Yandex Cloud Billing
page, make sure you have a billing account linked and it has theACTIVE
orTRIAL_ACTIVE
status. If you do not have a billing account, create one.
If you have an active billing account, you can go to the cloud page
Learn more about clouds and folders.
We use a folder named example-folder
as an example.
Required paid resources
The cost of the infrastructure includes:
- Using virtual machines (see Compute Cloud pricing).
- Function calls, computing resources allocated to executing the function, and outgoing traffic (see Cloud Functions pricing).
Create auxiliary resources
Create a service account
The service account will be linked to the Compute Cloud instance group and Cloud Functions function.
-
In the management console
, selectexample-folder
. -
At the top of the screen, go to the Service accounts tab.
-
Click Create service account at the top right.
-
In the Name field, specify
vm-scale-scheduled-sa
. -
Click
Add role and select the following roles:compute.admin
to manage the instance group.iam.serviceAccounts.user
to link the service account to instances in the group.serverless.functions.invoker
to invoke the Cloud Functions function.
-
Click Create.
-
Create a service account:
yc iam service-account create --name vm-scale-scheduled-sa \ --folder-name example-folder
Result:
id: ajeab0cnib1pdefe21dm folder_id: b0g12ga82bcv0cdeferg 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. -
Assign the service account the following roles:
-
compute.admin
to manage the instance group:yc resource-manager folder add-access-binding example-folder \ --service-account-name vm-scale-scheduled-sa \ --role compute.admin \ --folder-name example-folder
-
iam.serviceAccounts.user
to link the service account to instances in the group:yc resource-manager folder add-access-binding example-folder \ --service-account-name vm-scale-scheduled-sa \ --role iam.serviceAccounts.user \ --folder-name example-folder
-
serverless.functions.invoker
to invoke the Cloud Functions function:yc resource-manager folder add-access-binding example-folder \ --service-account-name vm-scale-scheduled-sa \ --role serverless.functions.invoker \ --folder-name example-folder
For more information about the
yc resource-manager folder add-access-binding
command, see the CLI reference. -
-
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. -
Assign the service account the following roles for the current folder:
compute.admin
to manage the instance group.iam.serviceAccounts.user
to link the service account to instances in the group.serverless.functions.invoker
to invoke 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 network
Your instance group will be hosted in the cloud network of Yandex Virtual Private Cloud and its subnets.
- In the management console
, selectexample-folder
. - In the list of services, select Virtual Private Cloud.
- Click Create network at the top right.
- In the Name field, specify
vm-scale-scheduled-network
. - Select Create subnets.
- Click Create network.
-
Create a network named
vm-scale-scheduled-network
:yc vpc network create --name vm-scale-scheduled-network \ --folder-name example-folder
Result:
id: enpabce123hde4ft1r3t folder_id: b0g12ga82bcv0cdeferg 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. -
Create a subnet named
vm-scale-scheduled-subnet-a
in theru-central1-a
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: e1lnabc23r1c9d0efoje folder_id: b0g12ga82bcv0cdeferg created_at: "2022-03-15T09:56:51.859345Z" name: vm-scale-scheduled-subnet-a network_id: enpabce123hde4ft1r3t 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. -
Create a subnet named
vm-scale-scheduled-network-b
in theru-central1-b
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: b1csa2b3clideftjb121 folder_id: b0g12ga82bcv0cdeferg created_at: "2022-03-15T09:57:48.934429Z" name: vm-scale-scheduled-subnet-b network_id: enpabce123hde4ft1r3t zone_id: ru-central1-b v4_cidr_blocks: - 192.168.2.0/24
-
Create a
vm-scale-scheduled-network
using the create REST API method for the Network resource or the NetworkService/Create gRPC API call. -
Create a subnet named
vm-scale-scheduled-subnet-a
in theru-central1-a
availability zone andvm-scale-scheduled-subnet-b
in theru-central1-b
availability zone using the create REST API method for the Subnet resource or the SubnetService/Create gRPC API call.
Create an instance group
An instance group is created with manual scaling so that a Cloud Functions function can manage the group size.
-
In the management console
, selectexample-folder
. -
In the list of services, select Compute Cloud.
-
In the left-hand panel, select
Instance groups. -
Click Create group of virtual machines at the top right.
-
Under Basic parameters:
- In the Name field, specify
vm-scale-scheduled-ig
. - Select Service account
vm-scale-scheduled-sa
.
- In the Name field, specify
-
Under Allocation, select
ru-central1-a
andru-central1-b
in the Availability zone field. -
Under Instance template, click Define and do the following in the window that opens:
-
Under Boot disk image, select Ubuntu 20.04.
-
Under Computing resources, specify the following configuration:
- Platform:
Intel Ice Lake
. - vCPU:
2
. - Guaranteed vCPU performance:
20%
. - RAM:
2 GB
.
- Platform:
-
Under Network settings
- In the Network field, select
vm-scale-scheduled-network
. - In the Public address field, select
No address
.
- In the Network field, select
-
Under Access:
- In the Service account field, select
vm-scale-scheduled-sa
. - In the Login field, enter the name of the user to be created on the VM. Make up a name.
- In the SSH key field, paste the contents of the public SSH key. You can create a key pair by following the instructions.
- In the Service account field, select
-
Click Save.
-
-
Under Allow when creating and updating, specify the following configuration:
- Add above target value:
2
. - Reduce below target value:
2
. - Create simultaneously:
2
. - Stop simultaneously:
2
.
- Add above target value:
-
Under Scaling:
- In the Type field, select
Fixed
. - Set the Size to
2
.
- In the Type field, select
-
Click Create.
-
Get the IDs of the resources you created at the previous steps:
-
The
vm-scale-scheduled-sa
service account ID:yc iam service-account get vm-scale-scheduled-sa \ --folder-name example-folder | grep "^id"
Result:
id: ajeab0cnib1pdefe21dm
For more information about the
yc iam service-account get
command, see the CLI reference. -
The
vm-scale-scheduled-network
ID:yc vpc network get vm-scale-scheduled-network \ --folder-name example-folder | grep "^id"
Result:
id: enpabce123hde4ft1r3t
For more information about the
yc vpc network get
command, see the CLI reference. -
The
vm-scale-scheduled-subnet-a
ID:yc vpc subnet get vm-scale-scheduled-subnet-a \ --folder-name example-folder | grep "^id"
Result:
id: e1lnabc23r1c9d0efoje
For more information about the
yc vpc subnet get
command, see the CLI reference. -
The
vm-scale-scheduled-subnet-b
ID:yc vpc subnet get vm-scale-scheduled-subnet-b \ --folder-name example-folder | grep "^id"
Result:
id: b1csa2b3clideftjb121
For more information about the
yc vpc subnet get
command, see the CLI reference.
-
-
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: fd83n3uou8m03iq9gavu
For more information about the
yc compute image get-latest-from-family
command, see the CLI reference. -
Insert the obtained IDs in the
specification.yaml
file with the instance group specification: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
-
Create an instance group named
vm-scale-scheduled-ig
according tospecification.yaml
:yc compute instance-group create --file specification.yaml \ --folder-name example-folder
Result:
done (2m18s) id: cl0kabcquk1gomdefbkk folder_id: b0g12ga82bcv0cdeferg ... service_account_id: ajeab0cnib1pdefe21dm status: ACTIVE
-
Get the IDs of the resources you created at 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
andvm-scale-scheduled-subnet-b
IDs: Using the get REST API method for the Subnet resource or the SubnetService/Get gRPC API call.
-
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. -
Insert the obtained IDs in the
specification.yaml
file with the instance group specification: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
-
Create an instance group named
vm-scale-scheduled-ig
based on the specification provided inspecification.yaml
. To do this, use the createFromYaml REST API method for the InstanceGroup resource or the InstanceGroupService/CreateFromYaml gRPC API call.
Create Cloud Functions resources
Create a function
The function will contain the code with Yandex Cloud CLI commands that update the instance group.
-
In the management console
, selectexample-folder
. -
Select Cloud Functions.
-
Click Create function at the top right.
-
In the Name field, specify
vm-scale-scheduled-function
. -
Click Create.
-
Select the Bash runtime environment.
-
Enable Add files with code examples.
-
Click Continue.
-
Under Editor, select the
handler.sh
file. -
Replace the file 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
-
Under Parameters:
-
Set the Timeout, sec field value to
60
. -
In the Service account field, select
vm-scale-scheduled-sa
. -
Under Environment variables, add the following variables:
IG_NAME
=vm-scale-scheduled-ig
.IG_BASE_SIZE
=2
.FOLDER_ID
= folder ID. You can retrieve the ID by following instructions.
-
-
In the top-right corner, click Save changes.
-
Create a file named
handler.sh
with the following 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 function:
yc serverless function create vm-scale-scheduled-function \ --folder-name example-folder
Result:
done (1s) id: d4e7d67ikvmqk5uoim47 folder_id: b1g9hv2loamqfnbul7d9 created_at: "2022-04-01T13:48:27.705Z" name: vm-scale-scheduled-function log_group_id: ckgij6l0845hsdc9khiq http_invoke_url: https://functions.yandexcloud.net/d4e7d67ikvmqk5uoim47 status: ACTIVE
-
Create the 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:
- The
--service-account-id
option is the ID of thevm-scale-schedule-sa
service account. To get the ID, follow the instructions in the Create an instance group step on the CLI tab. - The
FOLDER_ID
variable in the--environment
option is the ID of the folder. You can retrieve the ID by following instructions.
Result:
done (1s) id: d4efmv9n3qf1ptjtlof3 function_id: d4e7d67ikvmqk5uoim47 created_at: "2022-04-01T14:04:29.523Z" runtime: bash entrypoint: handler.sh resources: memory: "134217728" execution_timeout: 60s service_account_id: aje9s4pud13hfhk1bfgn image_size: "4096" status: ACTIVE tags: - $latest log_group_id: ckgij6l0845hsdc9khiq environment: FOLDER_ID: b1g9hv2loamqfnbul7d9 IG_BASE_SIZE: "2" IG_NAME: vm-scale-scheduled-ig
- The
-
Create a
vm-scale-scheduled-function
using the create REST API method for the Function resource or the FunctionService/Create gRPC API call. You can find the ID of the created function in the output. -
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 the following 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 trigger
A trigger sets conditions for running a function. For scheduled function calls, use a timer trigger.
-
In the management console
, selectexample-folder
. -
Select Cloud Functions.
-
In the left-hand panel, select
Triggers. -
Click Create trigger at the top right.
-
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
.
- In the Name field, specify
-
Under Timer settings, set the Cron expression field value to
*/2 * * * ? *
so that the trigger fires every two minutes (at 09:58, 10:00, 10:02, 10:04, and so on). -
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
.
- Under Dead Letter Queue settings, reset the Service account (
Not selected
) field.
- In the Function field, specify
-
Click Create trigger.
Run the following 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: a1s22pb0v5i4q432hi0p
folder_id: b1g9hv2loamqfnbul7d9
created_at: "2022-04-06T14:56:54.253709098Z"
name: vm-scale-scheduled-trigger
rule:
timer:
cron_expression: "*/2 * * * ? *"
invoke_function_with_retry:
function_id: d4e7d67ikvmqk5uoim47
function_tag: $latest
service_account_id: aje9s4pud13hfhk1bfgn
status: ACTIVE
Use the create REST API method for the Trigger resource or the TriggerService/Create gRPC API call to create a trigger of the timer
type with the */2 * * * ?
cron expression *linked to the function
vm-scale-scheduled-functionof the
$latestversion and the
vm-scale-scheduled-sa` service account.
Test instance group scaling
- In the management console
, selectexample-folder
. - In the list of services, select Compute Cloud.
- In the left-hand panel, select
Instance groups. - Select the
vm-scale-scheduled-ig
group. - Under VM states, make sure the number of instances changes every two minutes: increases from 2 to 3, then decreases from 3 to 2, and so on. To check if the instance group has been updated, open
Operations.
Run the following command several times:
yc compute instance-group get vm-scale-scheduled-ig \
--folder-name example-folder
Result:
id: cl1l0ljqbmkp8k1f902l
folder_id: b1g9hv2loamqfnbul7d9
created_at: "2022-03-28T13:24:20.693Z"
name: vm-scale-scheduled-ig
...
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 created
To stop paying for the resources you created:
- Delete the trigger
vm-scale-scheduled-trigger
. - Delete the function
vm-scale-scheduled-function
. - Delete the instance group
vm-scale-scheduled-ig
.
How to create an infrastructure using Terraform
Terraform
For more information about the provider resources, see the documentation on the Terraform
If you change the configuration files, Terraform automatically detects which part of your configuration is already deployed, and what should be added or removed.
To set up scaling for your instance group using Terraform:
-
Install Terraform and specify the source for installing the Yandex Cloud provider (see Configure a provider, step 1).
-
Prepare files with the infrastructure description:
Ready-made archiveCreating files manually- Create a directory for files.
- Download the archive
(2 KB). - Unpack the archive to the directory. As a result, it should contain the
vm-scale-scheduled.tf
configuration file and thevm-scale-scheduled-function.zip
archive with the Cloud Functions function code.
-
Create a directory for files.
-
In the directory, create:
-
A configuration file named
vm-scale-scheduled.tf
:vm-scale-scheduled.tf
locals { token = "<OAuth_or_IAM_token>" cloud_id = "<cloud_ID>" folder_id = "<folder_ID>" username = "<VM_username>" ssh_key_path = "<path_to_public_SSH_key>" } terraform { required_providers { yandex = { source = "yandex-cloud/yandex" version = ">= 0.47.0" } } } provider "yandex" { token = local.token cloud_id = local.cloud_id folder_id = local.folder_id } resource "yandex_iam_service_account" "vm-scale-scheduled-sa" { name = "vm-scale-scheduled-sa" } resource "yandex_resourcemanager_folder_iam_member" "vm-scale-scheduled-sa-role-compute" { folder_id = "<folder_ID>" role = "compute.admin" member = "serviceAccount:${yandex_iam_service_account.vm-scale-scheduled-sa.id}" } resource "yandex_resourcemanager_folder_iam_member" "vm-scale-scheduled-sa-role-iam" { folder_id = "<folder_ID>" role = "iam.serviceAccounts.user" member = "serviceAccount:${yandex_iam_service_account.vm-scale-scheduled-sa.id}" } resource "yandex_resourcemanager_folder_iam_member" "vm-scale-scheduled-sa-role-functions" { folder_id = "<folder_ID>" role = "functions.functionInvoker" member = "serviceAccount:${yandex_iam_service_account.vm-scale-scheduled-sa.id}" } resource "yandex_vpc_network" "vm-scale-scheduled-network" { name = "vm-scale-scheduled-network" } resource "yandex_vpc_subnet" "vm-scale-scheduled-subnet-a" { name = "vm-scale-scheduled-subnet-a" zone = "ru-central1-a" v4_cidr_blocks = ["192.168.1.0/24"] network_id = yandex_vpc_network.vm-scale-scheduled-network.id } resource "yandex_vpc_subnet" "vm-scale-scheduled-subnet-b" { name = "vm-scale-scheduled-subnet-b" zone = "ru-central1-b" v4_cidr_blocks = ["192.168.2.0/24"] network_id = yandex_vpc_network.vm-scale-scheduled-network.id } resource "yandex_compute_image" "vm-scale-scheduled-image" { source_family = "ubuntu-2004-lts" } resource "yandex_compute_instance_group" "vm-scale-scheduled-ig" { name = "vm-scale-scheduled-ig" service_account_id = yandex_iam_service_account.vm-scale-scheduled-sa.id allocation_policy { zones = [ "ru-central1-a", "ru-central1-b" ] } instance_template { boot_disk { mode = "READ_WRITE" initialize_params { image_id = yandex_compute_image.vm-scale-scheduled-image.id size = 15 } } platform_id = "standard-v3" resources { cores = 2 core_fraction = 20 memory = 2 } network_interface { network_id = yandex_vpc_network.vm-scale-scheduled-network.id subnet_ids = [ yandex_vpc_subnet.vm-scale-scheduled-subnet-a.id, yandex_vpc_subnet.vm-scale-scheduled-subnet-b.id ] } metadata = { user-data = "#cloud-config\nusers:\n - name: ${local.username}\n groups: sudo\n shell: /bin/bash\n sudo: 'ALL=(ALL) NOPASSWD:ALL'\n ssh-authorized-keys:\n - ${file("${local.ssh_key_path}")}" } } scale_policy { fixed_scale { size = 2 } } deploy_policy { max_unavailable = 2 max_creating = 2 max_expansion = 2 max_deleting = 2 } depends_on = [ yandex_resourcemanager_folder_iam_member.vm-scale-scheduled-sa-role-compute, yandex_resourcemanager_folder_iam_member.vm-scale-scheduled-sa-role-iam ] } resource "yandex_function" "vm-scale-scheduled-function" { name = "vm-scale-scheduled-function" runtime = "bash" user_hash = "function-v1" entrypoint = "handler.sh" content { zip_filename = "vm-scale-scheduled-function.zip" } execution_timeout = "60" memory = "128" service_account_id = yandex_iam_service_account.vm-scale-scheduled-sa.id environment = { IG_NAME = yandex_compute_instance_group.vm-scale-scheduled-ig.name IG_BASE_SIZE = "2" FOLDER_ID = local.folder_id } depends_on = [ yandex_resourcemanager_folder_iam_member.vm-scale-scheduled-sa-role-functions ] } resource "yandex_function_trigger" "vm-scale-scheduled-trigger" { name = "vm-scale-scheduled-trigger" timer { cron_expression = "*/2 * * * ? *" } function { id = yandex_function.vm-scale-scheduled-function.id tag = "$latest" service_account_id = yandex_iam_service_account.vm-scale-scheduled-sa.id } depends_on = [ yandex_resourcemanager_folder_iam_member.vm-scale-scheduled-sa-role-functions ] }
-
A file with the Cloud Functions function code named
handler.sh
:handler.sh
Warning
# 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
-
-
In the directory, create an archive named
vm-scale-scheduled-function.zip
with thehandler.sh
file. Next, delete thehandler.sh
file outside the archive.
-
In the
vm-scale-scheduled.tf
file, paste the following values in thelocals
section:token
: OAuth token (if you are using a Yandex account) or IAM token (if you are using a Yandex account or federated account) to access Yandex Cloud. The IAM token is valid for up to 12 hours but no more than the federation's cookie lifetime.cloud_id
: ID of the cloud to create resources in.folder_id
: ID of the folder to create resources in.username
: Name of the user to be created on the VM. Make up a name.ssh_key_path
: Path to the file with a public SSH key to authenticate the user on the VM. You can create a key pair by following the instructions.
-
Create resources:
-
In the terminal, change to the folder where you edited the configuration file.
-
Make sure the configuration file is correct using the command:
terraform validate
If the configuration is correct, the following message is returned:
Success! The configuration is valid.
-
Run the command:
terraform plan
The terminal will display a list of resources with parameters. No changes are made at this step. If the configuration contains errors, Terraform will point them out.
-
Apply the configuration changes:
terraform apply
-
Confirm the changes: type
yes
in the terminal and press Enter.
-