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
Tutorials
    • All tutorials
    • Basic internet service architecture and protection
    • 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
      • 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
  • Download a project
  • Create a service account and static access key
  • Create a cloud network and subnet
  • Create a budget
  • Create a Message Queue
  • Create a Cloud Functions function the budget trigger will invoke
  • Create a budget trigger
  • Register your Telegram bot
  • Create a Cloud Functions function the Message Queue budget trigger will invoke
  • Create a trigger for Message Queue
  • Create Compute Cloud VMs
  • Make sure the trigger stops the VMs and sends Telegram notifications
  • How to delete the resources you created
  1. Basic infrastructure
  2. Fault tolerance and scaling
  3. Creating triggers that invoke a function to stop a VM and send a Telegram notification

Creating triggers that invoke Cloud Functions functions to stop a VM and send Telegram notifications

Written by
Yandex Cloud
Updated at May 7, 2025
  • Get your cloud ready
    • Required paid resources
    • Download a project
    • Create a service account and static access key
    • Create a cloud network and subnet
  • Create a budget
  • Create a Message Queue
  • Create a Cloud Functions function the budget trigger will invoke
  • Create a budget trigger
  • Register your Telegram bot
  • Create a Cloud Functions function the Message Queue budget trigger will invoke
  • Create a trigger for Message Queue
  • Create Compute Cloud VMs
  • Make sure the trigger stops the VMs and sends Telegram notifications
  • How to delete the resources you created

In this tutorial, you will create a serverless infrastructure that will stop the virtual machines and send Telegram notifications when the VM resource use exceeds budget thresholds.

The budget trigger will invoke the Cloud Functions function that will stop the Compute Cloud virtual machines and send a message to the Yandex Message Queue queue.

The Message Queue trigger will deliver the queued messages to the second Cloud Functions function that will send Telegram notifications via a dedicated bot.

To deploy a project:

  1. Get your cloud ready.
  2. Create a budget.
  3. Create a Message Queue queue.
  4. Create a Cloud Functions function that the budget trigger will invoke.
  5. Create a budget trigger.
  6. Register your Telegram bot.
  7. Create a Cloud Functions function the Message Queue trigger will invoke.
  8. Create a trigger for Message Queue.
  9. Create Compute Cloud VM instances.
  10. Make sure the trigger stops the VMs and sends Telegram notifications.

Create all specified Yandex Cloud resources in the same folder.

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.

Required paid resourcesRequired paid resources

The cost of resources includes:

  • Fee for VM computing resources (see Compute Cloud pricing).
  • Fee for VM disks (see Compute Cloud pricing).
  • Fee for a dynamic public IP address (see Yandex Virtual Private Cloud pricing).
  • Fee for the number of function calls, computing resources allocated to a function, and outgoing traffic (see Cloud Functions pricing).
  • Fee for the number of requests to queues and outgoing traffic (see Message Queue pricing).
  • Fee for logging operations and data storage in a log group (see Yandex Cloud Logging pricing) if you use Cloud Logging.

Download a projectDownload a project

Clone the repository containing the project:

git clone https://github.com/yandex-cloud-examples/yc-telegram-bot-with-trigger-for-budget

Create a service account and static access keyCreate a service account and static access key

  1. Create a service account:

    Management console
    CLI
    API
    1. In the management console, select your folder.
    2. In the list of services, select Identity and Access Management.
    3. Click Create service account.
    4. In the Name field, specify the name: service-account-for-budget.
    5. In the Roles in the folder field, click Add role and select the editor role.
    6. Click Create.

    If you do not have the Yandex Cloud CLI yet, install and initialize it.

    The folder specified when creating the CLI profile is used by default. To change the default folder, use the yc config set folder-id <folder_ID> command. You can specify a different folder using the --folder-name or --folder-id parameter.

    1. Create a service account named service-account-for-budget:

      yc iam service-account create \
        --name service-account-for-budget
      

      Result:

      done (1s)
      id: ajed1o6dd581********
      folder_id: b1g9d2k0itu4********
      created_at: "2024-02-23T18:32:38.365175925Z"
      name: service-account-for-budget
      

      Save the service account ID (id): you will need it in the next step.

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

    2. Assign the service account the editor role for your folder:

      yc resource-manager folder add-access-binding <folder_ID> \
        --role editor \
        --subject serviceAccount:<service_account_ID>
      

      Where:

      • <folder_name>: Name of your folder in Yandex Cloud.
      • <service_account_ID>: Service account ID you saved in the previous step.

      Result:

      done (2s)
      effective_deltas:
        - action: ADD
          access_binding:
            role_id: editor
            subject:
              id: ajed1o6dd581********
              type: serviceAccount
      

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

    To create a service account, use the create REST API method for the ServiceAccount resource or the ServiceAccountService/Create gRPC API call.

    To assign the service account a role for the folder, use the updateAccessBindings REST API method for the Folder resource or the FolderService/UpdateAccessBindings gRPC API call.

  2. Create a static access key:

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

    2. In the list of services, select Identity and Access Management.

    3. In the left-hand panel, select Service accounts.

    4. Select the service-account-for-budget service account from the list that opens.

    5. In the top panel, click Create new key and select Create static access key.

    6. If required, specify the key description and click Create.

    7. Save the ID and secret key.

      Alert

      After you close this dialog, the key value will not be shown again.

    Run this command:

    yc iam access-key create \
      --service-account-name service-account-for-budget
    

    Result:

    access_key:
      id: aje75gg0rp8k********
      service_account_id: ajed1o6dd581********
      created_at: "2024-02-23T18:36:41.061060561Z"
      key_id: YCAJEK_r3Z_EvxRAR********
    secret: YCPQhHFMx6rnWXQC9ID425gk3V9YnUc********
    

    Save the ID (key_id) and secret key (secret). This is the only time you can copy this key as it will not be shown again.

    For more information about the yc iam access-key create command, see the CLI reference.

    To create an access key, use the create REST API method for the AccessKey resource or the AccessKeyService/Create gRPC API call.

Create a cloud network and subnetCreate a cloud network and subnet

Management console
CLI
API
  1. In the management console, select your folder.
  2. In the list of services, select Virtual Private Cloud.
  3. At the top right, click Create network.
  4. In the Name field, specify my-sample-network.
  5. In the Advanced field, disable the Create subnets option.
  6. Click Create network.
  7. In the left-hand panel, select Subnets.
  8. At the top right, click Create.
  9. In the Name field, specify sample-subnet-ru-central1-b.
  10. In the Zone field, select the ru-central1-b availability zone.
  11. In the Network field, select the my-sample-network cloud network.
  12. In the CIDR field, specify 192.168.1.0/24.
  13. Click Create subnet.
  1. Create a network named my-sample-network:

    yc vpc network create my-sample-network
    

    Result:

    id: enp2gjcvrd59********
    folder_id: b1g9d2k0itu4********
    created_at: "2024-02-23T18:39:17Z"
    name: my-sample-network
    default_security_group_id: enp9uobl2c33********
    

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

  2. Create a subnet named sample-subnet-ru-central1-b in the ru-central1-b availability zone:

    yc vpc subnet create sample-subnet-ru-central1-b \
      --zone ru-central1-b \
      --network-name my-sample-network \
      --range 192.168.1.0/24
    

    Result:

    id: e2l1ejkvq4jv********
    folder_id: b1g9d2k0itu4********
    created_at: "2024-02-23T18:40:26Z"
    name: sample-subnet-ru-ru-central1-b
    network_id: enp2gjcvrd59********
    zone_id: ru-central1-b
    v4_cidr_blocks:
      - 192.168.1.0/24
    

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

  1. To create a network, use the create REST API method for the Network resource or the NetworkService/Create gRPC API call.

  2. To create a subnet, use the create REST API method for the Subnet resource or the SubnetService/Create gRPC API call.

Create a budgetCreate a budget

The user needs the editor role to create a budget. To get notifications, the viewer role is sufficient.

Yandex Cloud Billing interface
API
  1. Go to Yandex Cloud Billing.

  2. Select a billing account.

  3. In the General information section, in the ID field, copy the billing account ID. You will need it when creating a trigger for budgets.

  4. Go to the Budgets tab and click Create budget.

  5. Under General information, specify:

    • Name: vm-budget.

    • Type: Due and payable.

    • Amount: Amount of consumption expenses, e.g. ₽10.

    • Calculation period: Monthly.

    • Expires: Budget expiration date.

      The end date is when the budget stops calculating usage and sending notifications. The end date is the last day of the month. It must be within five years of the current date.

    • Notify: Select yourself.

  6. Under Scope, select your current folder and the Compute Cloud service.

  7. Under Limits, set the threshold values as a percentage upon reaching which:

    • Specified users will get notifications.
    • Trigger for budgets will fire.

    For example, you can set two thresholds: 50% and 100%.

  8. Click Create.

  9. A window will open with the new vm-budget. In the ID field, copy the ID of the new budget. You will need it later when creating a trigger.

To create a budget, use the create method for the Budget resource or the BudgetService/Create gRPC API call.

Create a Message QueueCreate a Message Queue

Management console
AWS CLI
  1. In the management console, select your folder.

  2. Select Message Queue.

  3. Click Create queue.

  4. Under Basic parameters, specify:

    • Name: budget-queue
    • Type: Standard

    Leave the other parameters unchanged.

  5. Click Create.

  6. Click the name of the your new queue: budget-queue. From the window that opens, copy the field values:

    • URL: Queue URL
    • ARN: Queue ID

    Save these values; you will need them in the next steps.

  1. Install and configure the AWS CLI. When configuring, use the static access key you created in the previous step.

  2. Create a queue:

    aws sqs create-queue \
      --queue-name budget-queue \
      --endpoint https://message-queue.api.cloud.yandex.net/
    

    Result:

    {
        "QueueUrl": "https://message-queue.api.cloud.yandex.net/b1glti4eser3********/dj600000001c********/budget-queue"
    }
    

    Save the QueueUrl value; you will need it in the next steps.

  3. Get the queue ID by specifying the previously saved URL in the --queue-url parameter:

    aws sqs get-queue-attributes \
      --queue-url <queue_URL> \
      --attribute-names QueueArn \
      --endpoint https://message-queue.api.cloud.yandex.net/
    

    Result:

    {
        "Attributes": {
            "QueueArn": "yrn:yc:ymq:ru-central1:b1g9d2k0itu4********:budget-queue"
        }
    }
    

    Save the QueueArn queue ID; you will need it in the next steps.

Create a Cloud Functions function the budget trigger will invokeCreate a Cloud Functions function the budget trigger will invoke

  1. Prepare a ZIP archive with the function code.

    Navigate to the yc-telegram-bot-with-trigger-for-budget/steps/4-create-budget-trigger-handler directory and add files to the src_bgt.zip archive:

    zip src_bgt.zip budget_trigger_handler.go go.mod
    
  2. Create a function:

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

    2. Select Cloud Functions.

    3. Create a function:

      1. Click Create function.
      2. Specify the function name: budget-trigger-handler.
      3. Click Create.
    4. Create a function version:

      1. Select golang119 as the runtime environment, disable the Add files with code examples option, and click Continue.

      2. Specify the ZIP archive upload method and attach the src_bgt.zip archive you created in the previous step.

      3. Specify the entry point: budget_trigger_handler.Handler.

      4. Under Parameters, specify:

        • Timeout: 5
        • Memory: 512 MB
        • Service account: service-account-for-budget
        • Environment variables:
          • FOLDER_ID : ID of the folder where you want the VMs stopped.
          • TAG: target-for-stop.
          • AWS_ACCESS_KEY_ID: Static access key ID you saved in the previous step.
          • AWS_ACCESS_KEY_ID: Secret key value of the static access key you saved in the previous step.
          • BUDGET_QUEUE_URL: budget-queue URL you saved in the previous step.
      5. Click Save changes.

    1. Create a function named budget-trigger-handler:

      yc serverless function create \
        --name budget-trigger-handler
      

      Result:

      id: d4e4aigfdm0b********
      folder_id: b1g9d2k0itu4********
      created_at: "2024-02-23T20:15:06.456Z"
      name: budget-trigger-handler
      http_invoke_url: https://functions.yandexcloud.net/d4e4aigfdm0b********
      status: ACTIVE
      

      For more information about the yc serverless function create command, see the CLI reference.

    2. Create a version of the budget-trigger-handler function:

      yc serverless function version create \
        --function-name budget-trigger-handler \
        --memory=512m \
        --execution-timeout=5s \
        --runtime=golang119 \
        --entrypoint=budget_trigger_handler.Handler \
        --service-account-id=<service_account_ID> \
        --environment FOLDER_ID=<folder_ID> \
        --environment TAG=target-for-stop \
        --environment AWS_ACCESS_KEY_ID=<static_key_ID> \
        --environment AWS_SECRET_ACCESS_KEY=<private_key_value> \
        --environment BUDGET_QUEUE_URL=<queue_URL> \
        --source-path="./src_bgt.zip"
      

      Where:

      • --service-account-id: ID of the service-account-for-budget service account you saved in the previous step. This is the service account to invoke the function.

      • --environment: Environment variables:

        • FOLDER_ID: ID of the folder where you want the VMs stopped.
        • AWS_ACCESS_KEY_ID: Static access key ID you saved in the previous step.
        • AWS_ACCESS_KEY_ID: Secret key value of the static access key you saved in the previous step.
        • BUDGET_QUEUE_URL: budget-queue URL you saved in the previous step.
      • --source-path: Path to the src_bgt.zip archive.

      Result:

      done (2m32s)
      id: d4em5gaeev2b********
      function_id: d4e4aigfdm0b********
      created_at: "2024-02-23T20:31:18.458Z"
      runtime: golang119
      entrypoint: budget_trigger_handler.Handler
      resources:
        memory: "536870912"
      execution_timeout: 5s
      service_account_id: ajed1o6dd581********
      image_size: "23977984"
      status: ACTIVE
      tags:
        - $latest
      environment:
        AWS_ACCESS_KEY_ID: YCAJEK_r3Z_EvxRAR********
        AWS_SECRET_ACCESS_KEY: YCPQhHFMx6rnWXQC9ID425gk3V9YnUc0********
        BUDGET_QUEUE_URL: https://message-queue.api.cloud.yandex.net/b1glti4eser3********/dj600000001c********/budget-queue
        FOLDER_ID: b1g9d2k0itu4********
        TAG: target-for-stop
      log_options:
        folder_id: b1g9d2k0itu4********
      

      For more information about the yc serverless function version create command, see the CLI reference.

    To create a function, use the create method for the Function resource or the FunctionService/Create gRPC API call.

    To create a function version, use the createVersion method for the Function resource or the FunctionService/CreateVersion gRPC API call.

Create a budget triggerCreate a budget trigger

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

  2. Select Cloud Functions.

  3. In the left-hand panel, select Triggers.

  4. Click Create trigger.

  5. Under Basic settings:

    • In the Name field, specify the trigger name: trigger-for-budget-from-yc.
    • In the Type field, select Budget.
    • In the Launched resource field, select Function.
  6. Under Budget settings, select your billing account and the vm-budget budget you created earlier.

  7. Under Function settings, select the budget-trigger-handler function and specify the service-account-for-budget service account. This is the service account to invoke the function.

  8. Click Create trigger.

To create a budget trigger that invokes the budget-trigger-handler function, run the following command:

yc serverless trigger create billing-budget \
  --name trigger-for-budget-from-yc \
  --invoke-function-name budget-trigger-handler \
  --invoke-function-service-account-id <service_account_ID>  \
  --billing-account-id <billing_account_ID>  \
  --budget-id <budget_ID>

Where:

  • --invoke-function-service-account-id: ID of the service-account-for-budget service account you saved in the previous step. This is the service account to invoke the function.
  • --billing-account-id: Billing account ID saved earlier when creating a budget.
  • --budget-id: Budget ID you saved in the previous step.

Result:

id: a1sjrukc64hs********
folder_id: b1g9d2k0itu4********
created_at: "2024-02-23T21:20:03.308963151Z"
name: trigger-for-budget-from-yc
rule:
  billing_budget:
    billing_account_id: dn276oa9slgm********
    budget_id: dn2jr6qt0q9k********
    invoke_function:
      function_id: d4e4aigfdm0b********
      function_tag: $latest
      service_account_id: ajed1o6dd581********
status: ACTIVE

For more information about the yc serverless trigger create billing-budget command, see the CLI reference.

To create a trigger for budgets, use the create method for the Trigger resource or the TriggerService/Create gRPC API call.

Register your Telegram botRegister your Telegram bot

  1. Register a bot in Telegram and get a token:

    1. To register a new bot, launch the BotFather bot and send the following command:

      /newbot
      

      Result:

      Alright, a new bot. How are we going to call it? Please choose a name for your bot.
      
    2. In the next message, specify the name of your bot, e.g., YC VM budget informer. This is the name users chatting with the bot will see.

      Result:

      Good. Now let's choose a username for your bot. It must end in `bot`. Like this, for example: TetrisBot or tetris_bot.
      
    3. Specify the username of your bot, e.g., YCVMBudgetInformerBot. You can use it to find the bot in Telegram. The username must end with ...Bot or ..._bot. In response, you will receive an access token for the HTTP API.

      Result:

      Done! Congratulations on your new bot. You will find it at t.me/YCVMBudgetInformerBot. You can now add a description, about section and profile picture for your bot, see /help for a list of commands. By the way, when you've finished creating your cool bot, ping our Bot Support if you want a better username for it. Just make sure the bot is fully operational before you do this.
      
      Use this token to access the HTTP API:
      715063****:AAGuj****-0VrIm7j9i****************
      Keep your token secure and store it safely, it can be used by anyone to control your bot.
      
      For a description of the Bot API, see this page: https://core.telegram.org/bots/api
      
  2. Get ChatID of the user who will be receiving notifications from the bot:

    1. Save the API token you got in the previous step into the TELEGRAM_BOT_API_TOKEN environment variable:

      export TELEGRAM_BOT_API_TOKEN=<Telegram_API_token>
      
    2. Navigate to the yc-telegram-bot-with-trigger-for-budget/steps/6-register-telegram-bot directory and run the following command:

      go run .
      

      If required, pre-install the Go and telegram_bot packages.

      The command will launch a Telegram bot that will return the ChatID of the current user in response to any message.

    3. In Telegram, find your bot by its username, e.g., YCVMBudgetInformerBot, and send any message to it.

      Result:

      59529*****
      

      Save the ChatID you have received in response. You will need it later.

    4. Return to the terminal and terminate the bot by pressing Ctrl + C.

Create a Cloud Functions function the Message Queue budget trigger will invokeCreate a Cloud Functions function the Message Queue budget trigger will invoke

  1. Prepare a ZIP archive with the function code.

    Navigate to the yc-telegram-bot-with-trigger-for-budget/steps/7-create-budget-queue-handler directory and add files to the src_queue.zip archive:

    zip src_queue.zip budget_queue_handler.go utils.go go.mod
    
  2. Create a function:

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

    2. Select Cloud Functions.

    3. Create a function:

      1. Click Create function.
      2. Specify the function name: budget-queue-handler.
      3. Click Create.
    4. Create a function version:

      1. Select golang119 as the runtime environment, disable the Add files with code examples option, and click Continue.

      2. Specify the ZIP archive upload method and attach the src_queue.zip archive you created in the previous step.

      3. Specify the entry point: budget_queue_handler.HandleBudgetQueueMessage.

      4. Under Parameters, specify:

        • Timeout: 5
        • Memory: 512 MB
        • Service account: service-account-for-budget
        • Environment variables:
          • TELEGRAM_BOT_API_TOKEN: API token of the Telegram bot you saved in the previous step.
          • TELEGRAM_BOT_CHAT_ID: ChatID of the Telegram user you saved in the previous step.
      5. Click Save changes.

    1. Create a function named budget-queue-handler:

      yc serverless function create \
        --name budget-queue-handler
      

      Result:

      id: d4e6r2g9trt5********
      folder_id: b1g9d2k0itu4********
      created_at: "2024-02-24T07:36:04.092Z"
      name: budget-queue-handler
      http_invoke_url: https://functions.yandexcloud.net/d4e6r2g9trt5********
      status: ACTIVE
      
    2. Create a version of the budget-queue-handler function:

      yc serverless function version create \
        --function-name budget-queue-handler \
        --memory 512m \
        --execution-timeout 5s \
        --runtime golang119 \
        --entrypoint budget_queue_handler.HandleBudgetQueueMessage \
        --service-account-id <service_account_ID> \
        --environment TELEGRAM_BOT_API_TOKEN=<Telegram_bot_API_token> \
        --environment TELEGRAM_BOT_CHAT_ID=<Telegram_user_ChatID> \
        --source-path="./src_queue.zip"
      

      Where:

      • --service-account-id: ID of the service-account-for-budget service account you saved in the previous step. This is the service account to invoke the function.

      • --environment: Environment variables:

        • TELEGRAM_BOT_API_TOKEN: API token of the Telegram bot you saved in the previous step.
        • TELEGRAM_BOT_CHAT_ID: ChatID of the Telegram user you saved in the previous step.
      • --source-path: Path to the src_queue.zip archive.

      Result:

      done (29s)
      id: d4e41l4gj7rk********
      function_id: d4e6r2g9trt5********
      created_at: "2024-02-24T07:43:32.464Z"
      runtime: golang119
      entrypoint: budget_queue_handler.HandleBudgetQueueMessage
      resources:
        memory: "536870912"
      execution_timeout: 5s
      service_account_id: ajed1o6dd581********
      image_size: "5464064"
      status: ACTIVE
      tags:
        - $latest
      environment:
        TELEGRAM_BOT_API_TOKEN: 7150638333:AAGujjyua-0VrIm7j9iG3mOxmz1********
        TELEGRAM_BOT_CHAT_ID: "59529*****"
      log_options:
        folder_id: b1g9d2k0itu4********
      

    To create a function, use the create method for the Function resource or the FunctionService/Create gRPC API call.

    To create a function version, use the createVersion method for the Function resource or the FunctionService/CreateVersion gRPC API call.

Create a trigger for Message QueueCreate a trigger for Message Queue

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

  2. Select Cloud Functions.

  3. In the left-hand panel, select Triggers.

  4. Click Create trigger.

  5. Under Basic settings:

    • In the Name field, specify the trigger name: budget-queue-trigger.
    • In the Type field, select Message Queue.
    • In the Launched resource field, select Function.
  6. Under Message Queue message settings, select the resources you created in the previous steps:

    • In the Message queue field, select budget-queue.
    • In the Service account field, select service-account-for-budget.
  7. Under Function settings, select the budget-queue-handler function and specify the service-account-for-budget service account. This is the service account to invoke the function.

  8. Click Create trigger.

To create a trigger for Message Queue that invokes the budget-queue-handler function, run the following command:

yc serverless trigger create message-queue \
  --name budget-queue-trigger \
  --queue <queue_ID> \
  --queue-service-account-id <service_account_ID> \
  --invoke-function-name budget-queue-handler \
  --invoke-function-service-account-id <service_account_ID> \
  --batch-size 1 \
  --batch-cutoff 10s

Where:

  • --queue: budget-queue ID you saved when creating the queue.
  • --queue-service-account-id: ID of the service-account-for-budget service account you saved in the previous step. The system will use the service account to read messages from the queue.
  • --invoke-function-service-account-id: ID of the service-account-for-budget service account you saved in the previous step. This is the service account to invoke the function.

Result:

id: a1sqq6341kvh********
folder_id: b1g9d2k0itu4********
created_at: "2024-02-24T08:35:39.296084612Z"
name: budget-queue-trigger
rule:
  message_queue:
    queue_id: yrn:yc:ymq:ru-central1:b1g9d2k0itu4********:budget-queue
    service_account_id: ajed1o6dd581********
    batch_settings:
      size: "1"
      cutoff: 10s
    invoke_function:
      function_id: d4e6r2g9trt5********
      function_tag: $latest
      service_account_id: ajed1o6dd581********
status: ACTIVE

For more information about the yc serverless trigger create message-queue command, see the CLI reference.

To create a trigger for Message Queue, use the create method for the Trigger resource or the TriggerService/Create gRPC API call.

Create Compute Cloud VMsCreate Compute Cloud VMs

Management console
CLI
API
  1. In the management console, select the folder where you want to create your VM.

  2. In the list of services, select Compute Cloud.

  3. In the left-hand panel, select Virtual machines.

  4. Click Create virtual machine.

  5. Under Boot disk image, select Ubuntu 22.04 LTS.

  6. Under Location, select the ru-central1-b availability zone.

  7. Under Network settings:

    • In the Subnet field, select the sample-subnet-ru-central1-b subnet you created earlier.
    • In the Public IP address field, select Auto.
  8. Under Access, select SSH key and specify the information required to access the VM:

    • In the Login field, specify a username, e.g., yc-user.
    • In the SSH key field, select the SSH key saved in your organization user profile.

      If there are no saved SSH keys in your profile, or you want to add a new key:

      • Click Add key.
      • Enter a name for the SSH key.
      • Upload or paste the contents of the public key file. You need to create a key pair for the SSH connection to a VM yourself.
      • Click Add.

      The SSH key will be added to your organization user profile.

      If users cannot add SSH keys to their profiles in the organization, the added public SSH key will only be saved to the user profile of the VM being created.

  9. Under General information:

    • Specify the VM name, target-instance-1.
    • In the Labels field, click Add label and enter target-for-stop:true. Click Enter to save the label.
  10. Click Create VM.

Create a virtual machine:

yc compute instance create \
  --name target-instance-1 \
  --labels target-for-stop=true \
  --zone ru-central1-b \
  --network-interface subnet-name=sample-subnet-ru-central1-b,nat-ip-version=ipv4 \
  --create-boot-disk image-folder-id=standard-images,image-family=ubuntu-2204-lts \
  --ssh-key ~/.ssh/<key_name>.pub

Where --ssh-key is the path to the public SSH key The VM will automatically create the user called yc-user for this key.

Result:

done (39s)
id: epdbhj4gssbp********
folder_id: b1g9d2k0itu4********
created_at: "2024-02-24T09:29:16Z"
name: target-instance-1
labels:
  target-for-stop: "true"
zone_id: ru-central1-b
platform_id: standard-v2
resources:
  memory: "2147483648"
  cores: "2"
  core_fraction: "100"
status: RUNNING
metadata_options:
  gce_http_endpoint: ENABLED
  aws_v1_http_endpoint: ENABLED
  gce_http_token: ENABLED
  aws_v1_http_token: DISABLED
boot_disk:
  mode: READ_WRITE
  device_name: epd4uuhf52oe********
  auto_delete: true
  disk_id: epd4uuhf52oe********
network_interfaces:
  - index: "0"
    mac_address: d0:0d:b8:**:**:**
    subnet_id: e2l1ejkvq4jv********
    primary_v4_address:
      address: 192.168.1.9
      one_to_one_nat:
        address: 158.160.**.***
        ip_version: IPV4
serial_port_settings:
  ssh_authorization: INSTANCE_METADATA
gpu_settings: {}
fqdn: epdbhj4gssbp********.auto.internal
scheduling_policy: {}
network_settings:
  type: STANDARD
placement_policy: {}

For more information about the yc compute instance create command, see the CLI reference.

To create a VM, use the create REST API method for the Instance resource or the InstanceService/Create gRPC API call.

Similarly, create two more VMs: target-instance-2 and target-instance-3. For the latter, set the target-for-stop label to false.

Make sure the trigger stops the VMs and sends Telegram notificationsMake sure the trigger stops the VMs and sends Telegram notifications

Wait until the resource use of your VMs reaches the threshold values specified in your budget. Make sure that, when it happens, the target-instance-1 and target-instance-2 virtual machines stop, and the Telegram bot sends you a budget trigger notification:

Budget trigger was triggered!

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

To stop paying for the resources you created:

  1. Delete the Cloud Functions triggers.
  2. Delete the Cloud Functions functions.
  3. Delete the VM instances.
  4. Delete the Message Queue queue.
  5. Delete the subnet, then delete the cloud network.

Was the article helpful?

Previous
Deploying a fault-tolerant architecture with preemptible VMs
Next
Getting started with Terraform
Yandex project
© 2025 Yandex.Cloud LLC