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.
Yandex Cloud Backup
  • Getting started
    • All guides
    • Activating the service
    • Viewing service resource operations
    • Viewing backup statistics
    • All tutorials
    • Connecting a BareMetal server to Cloud Backup
      • Overview
      • Management console, CLI, and API
      • Terraform
  • Access management
  • Pricing policy
  • Terraform reference
  • Monitoring metrics
  • Audit Trails events
  • Release notes
  • Troubleshooting

In this article:

  • Prepare your cloud
  • Required paid resources
  • Activate the service
  • Create an infrastructure
  • How to delete the resources you created
  1. Tutorials
  2. Linking a backup policy to a VM automatically
  3. Terraform

Linking a Yandex Cloud Backup policy to a VM automatically using Terraform

Written by
Yandex Cloud
Updated at May 13, 2025
  • Prepare your cloud
    • Required paid resources
  • Activate the service
  • Create an infrastructure
  • How to delete the resources you created

To create a virtual machine with automatic linking to a Cloud Backup policy:

  1. Prepare your cloud.
  2. Activate the service.
  3. Create an infrastructure.

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

Prepare your cloudPrepare your cloud

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 infrastructure support cost includes:

  • Fee for VM computing resources (see Yandex Compute Cloud pricing).
  • Fee for VM disks (see Yandex Compute Cloud pricing).
  • Fee for using a dynamic external IP address (see Yandex Virtual Private Cloud pricing).
  • Fee for VMs connected to Cloud Backup and the backup size (see Yandex Cloud Backup pricing).

Activate the serviceActivate the service

Note

The minimum folder role required to activate the service is backup.editor (see its description for details).

Management console
CLI
  1. In the management console, select the folder you want to create a VM with a Cloud Backup connection in.

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

  3. If you have not activated Cloud Backup yet, click Activate.

    If there is no Activate button, and you have access to creating a VM with a Cloud Backup connection, it means the service has already been activated. Proceed to the next step.

If you do not have the Yandex Cloud (CLI) command line interface 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. View the description of the CLI command to activate the service:

    yc backup provider activate --help
    
  2. Activate the service in the default folder:

    yc backup provider activate --async
    

    Where --async displays the operation progress info. This is an optional parameter.

    Result:

    id: cdgmnefxiatx********
    description: activate provider
    created_at: "2024-10-14T09:03:47.960564Z"
    created_by: ajec1gaqcmtr********
    modified_at: "2024-10-14T09:03:47.960564Z"
    done: true
    metadata:
      '@type': type.googleapis.com/yandex.cloud.backup.v1.ActivateProviderMetadata
      folder_id: b1go3el0d8fs********
    response:
      '@type': type.googleapis.com/google.protobuf.Empty
      value: {}
    

After activation, the system automatically creates the following backup policies:

  • Default daily: Daily incremental backup with the last 15 backups retained.
  • Default weekly: Weekly incremental backup with the last 15 backups retained.
  • Default monthly: Monthly incremental backup with the last 15 backups retained.

If you prefer not to create them, use the --skip-default-policy parameter.

Create an infrastructureCreate an infrastructure

Note

Linking a Yandex Cloud Backup policy to a VM is available for Terraform provider version 0.127.0 or higher.

With Terraform, you can quickly create a cloud infrastructure in Yandex Cloud and manage it using configuration files. These files store the infrastructure description written in HashiCorp Configuration Language (HCL). If you change the configuration files, Terraform automatically detects which part of your configuration is already deployed, and what should be added or removed.

Terraform is distributed under the Business Source License. The Yandex Cloud provider for Terraform is distributed under the MPL-2.0 license.

For more information about the provider resources, see the documentation on the Terraform website or mirror website.

To create an infrastructure using Terraform:

  1. Install Terraform, get the authentication credentials, and specify the source for installing the Yandex Cloud provider (see Configure a provider, Step 1).

  2. Prepare files with the infrastructure description:

    Ready-made configuration
    Manually
    1. Clone the repository with configuration files.

      git clone https://github.com/yandex-cloud-examples/yc-baas-backup-policy-auto-binding
      
    2. Go to the directory with the repository. Make sure it contains the following files:

      • backup-policy-auto-binding-config.tf: New infrastructure configuration.
      • cloud-init.yaml: VM metadata file.
    1. Create a folder for configuration files.

    2. In the folder, create:

      1. backup-policy-auto-binding-config.tf configuration file:
      backup-policy-auto-binding-config.tf
      # Configuring a provider 
      
      terraform {
        required_providers {
          yandex = {
            source = "yandex-cloud/yandex"
          }
        }
        required_version = ">= 0.13"
      }
      
      provider "yandex" {
        zone = "ru-central1-a"
      }
      
      # Creating a service account
      
      resource "yandex_iam_service_account" "my_sa" {
        name = "backup-sa"
      }
      
      # Assigning roles to a service account
      
      resource "yandex_resourcemanager_folder_iam_member" "my_binding" {
        folder_id = yandex_iam_service_account.my_sa.folder_id
        role      = "backup.editor"
        member    = "serviceAccount:${yandex_iam_service_account.my_sa.id}"
      }
      
      # Creating a cloud network
      
      resource "yandex_vpc_network" "my_backup_network" {
        name = "cloud-network"
      }
      
      # Creating a cloud subnet
      
      resource "yandex_vpc_subnet" "my_backup_subnet" {
        zone           = "ru-central1-a"
        network_id     = yandex_vpc_network.my_backup_network.id
        v4_cidr_blocks = ["192.168.0.0/24"]
      }
      
      # Creating a security group
      # https://yandex.cloud/ru/docs/backup/concepts/vm-connection#vm-network-access
      
      resource "yandex_vpc_security_group" "my_backup_security_group" {
        name       = "backup-sg"
        network_id = yandex_vpc_network.my_backup_network.id
        egress {
          protocol       = "TCP"
          from_port      = 7770
          to_port        = 7800
          v4_cidr_blocks = ["84.47.172.0/24"]
        }
        egress {
          protocol       = "TCP"
          port           = 443
          v4_cidr_blocks = ["213.180.204.0/24", "213.180.193.0/24", "178.176.128.0/24", "84.201.181.0/24", "84.47.172.0/24"]
        }
        egress {
          protocol       = "TCP"
          port           = 80
          v4_cidr_blocks = ["213.180.204.0/24", "213.180.193.0/24"]
        }
        egress {
          protocol       = "TCP"
          port           = 8443
          v4_cidr_blocks = ["84.47.172.0/24"]
        }
        egress {
          protocol       = "TCP"
          port           = 44445
          v4_cidr_blocks = ["51.250.1.0/24"]
        }
        ingress {
          protocol       = "TCP"
          description    = "ssh"
          v4_cidr_blocks = ["0.0.0.0/0"]
          port           = 22
        }
      }
      
      # Getting information about an image for a VM boot disk
      
      data "yandex_compute_image" "ubuntu" {
        family = "ubuntu-2204-lts"
      }
      
      # Creating a VM boot disk
      
      resource "yandex_compute_disk" "boot-disk" {
        type     = "network-ssd"
        zone     = "ru-central1-a"
        size     = "20"
        image_id = data.yandex_compute_image.ubuntu.id
      }
      
      # Creating a VM
      
      resource "yandex_compute_instance" "my_backup_compute" {
        name               = "backup-instance"
        platform_id        = "standard-v3"
        zone               = "ru-central1-a"
        service_account_id = yandex_iam_service_account.my_sa.id
        network_interface {
          subnet_id          = yandex_vpc_subnet.my_backup_subnet.id
          security_group_ids = [yandex_vpc_security_group.my_backup_security_group.id]
          nat                = true
        }
      
        boot_disk {
          disk_id = yandex_compute_disk.boot-disk.id
        }
      
        resources {
          cores  = 2
          memory = 4
        }
      
        metadata = {
          user-data = "${file("cloud-init.yaml")}"
        }
      }
      
      # Creating backup policies
      
      resource "yandex_backup_policy" "my_policy" {
        name                = "weekly-backup"
        fast_backup_enabled = true
        retention {
          after_backup = false
        }
        reattempts {
          enabled      = true
          interval     = "1m"
          max_attempts = 10
        }
        scheduling {
          scheme            = "ALWAYS_INCREMENTAL"
          weekly_backup_day = "FRIDAY"
          backup_sets {
            execute_by_time {
              repeat_at = ["03:00"]
              type      = "WEEKLY"
              weekdays  = ["FRIDAY"]
            }
          }
        }
        vm_snapshot_reattempts {
          enabled      = true
          interval     = "1m"
          max_attempts = 10
        }
      }
      
      # Linking a backup policy to a VM
      
      resource "yandex_backup_policy_bindings" "my_backup_binding" {
        instance_id = yandex_compute_instance.my_backup_compute.id
        policy_id   = yandex_backup_policy.my_policy.id
      }
      
      1. cloud-init.yaml VM metadata file:
      cloud-init.yaml
      #cloud-config
      datasource:
       Ec2:
        strict_id: false
      ssh_pwauth: no
      users:
      - name: <username>
        sudo: ALL=(ALL) NOPASSWD:ALL
        shell: /bin/bash
        ssh_authorized_keys:
        - <public_SSH_key>
      packages:
        - curl
        - perl
        - jq
      runcmd:
        - curl https://storage.yandexcloud.net/backup-distributions/agent_installer.sh | sudo bash
      

    For more information about the parameters of resources used in Terraform, see the provider documentation:

    • Service account: yandex_iam_service_account.
    • Assigning a role to a service account: yandex_resourcemanager_folder_iam_member.
    • Network: yandex_vpc_network.
    • Subnets: yandex_vpc_subnet.
    • Security group: yandex_vpc_security_group.
    • VM image data: yandex_compute_image.
    • VM boot disk: yandex_compute_disk.
    • VM instance: yandex_compute_instance.
    • Backup policy: yandex_backup_policy. You can create a new policy or use one of those generated automatically upon service activation.
    • Linking a backup policy to a VM: yandex_backup_policy_bindings. To link one of the backup policies generated automatically upon service activation, get its ID.
  3. In the cloud-init.yaml file, set the following user-defined parameters:

    • name: VM username, e.g., vm-user.
    • ssh_authorized_keys: Contents of the public key file. You need to create a key pair for the SSH connection yourself.
  4. Create resources:

    1. In the terminal, change to the folder where you edited the configuration file.

    2. 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.
      
    3. 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.

    4. Apply the configuration changes:

      terraform apply
      
    5. Confirm the changes: type yes in the terminal and press Enter.

Note

When the VM switches to the Running status, a Cloud Backup agent will start installing on it. This may take from 5 to 10 minutes.

A policy is linked asynchronously after you create and initialize a VM, as well as install and configure a Cloud Backup agent. This may take up to 10-15 minutes. As a result, the virtual machine will appear in the list of Cloud Backup VMs and in the list of VMs linked to the weekly-backup policy.

You can monitor the installation progress using the VM serial port in the management console.

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

To stop paying for the resources you created:

  1. Open the backup-policy-auto-binding.tf configuration file and delete the description of the new infrastructure from it.

  2. Apply the changes:

    1. In the terminal, change to the folder where you edited the configuration file.

    2. 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.
      
    3. 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.

    4. Apply the configuration changes:

      terraform apply
      
    5. Confirm the changes: type yes in the terminal and press Enter.

See alsoSee also

  • Linking a Yandex Cloud Backup policy to a VM automatically using the management console, CLI, or API

Was the article helpful?

Previous
Management console, CLI, and API
Next
Service overview
© 2025 Direct Cursus Technology L.L.C.