[Yandex Cloud documentation](../../../index.md) > [Yandex Cloud Backup](../../index.md) > [Step-by-step guides](../index.md) > Backup policies > Linking a VM or BareMetal server to a policy

# Linking a VM or Yandex BareMetal server to a backup policy


You can only link a VM or BareMetal server to a backup policy if they are connected to Yandex Cloud Backup. For more information, see [Managing VMs in Cloud Backup](../index.md#connect-vm) and [Managing BareMetal servers in Cloud Backup](../index.md#connect-baremetal).

{% list tabs group=instructions %}

- Management console {#console}

  1. In the [management console](https://console.yandex.cloud), select a folder where you want to link a VM or BareMetal server to a backup policy.
  1. Navigate to **Cloud Backup**.
  1. Navigate to the ![policies](../../../_assets/console-icons/calendar.svg) **Backup policies** tab.
  1. Select the policy to link the VM or BareMetal server to.
  1. Under **Attached resources**, click ![image](../../../_assets/console-icons/plus.svg) **Attach a VM**, and in the window that opens:

      1. Depending on the resource you want to link to the backup policy, select the **Virtual machines** or **BareMetal servers** tab and select the VM or server from the list.

          If the VM or BareMetal server you want to link to the backup policy is not listed, make sure it is connected to Cloud Backup.
      1. Click **Attach**.

- CLI {#cli}

  If you do not have the Yandex Cloud CLI yet, [install and initialize it](../../../cli/quickstart.md#install).

  The folder used by default is the one specified when [creating](../../../cli/operations/profile/profile-create.md) the CLI profile. To change the default folder, use the `yc config set folder-id <folder_ID>` command. You can also specify a different folder for any command using `--folder-name` or `--folder-id`. If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.

  1. View the description of the CLI command to link a VM or BareMetal server to a backup policy: 

      ```bash
      yc backup policy apply --help
      ```

  1. Get the ID of the policy to link the VM or BareMetal server to:

      ```bash
      yc backup policy list
      ```
      
      Result:
      
      ```text
      +----------------------+----------------------+---------+---------+---------------------+---------------------+
      |          ID          |      FOLDER ID       |  NAME   | ENABLED |     CREATED AT      |     UPDATED AT      |
      +----------------------+----------------------+---------+---------+---------------------+---------------------+
      | abc7n3wln123******** | ghi681qpe789******** | policy1 | true    | 2023-07-03 09:12:02 | 2023-07-03 09:12:43 |
      | deflqbiwc456******** | ghi681qpe789******** | policy2 | true    | 2023-07-07 14:58:23 | 2023-07-07 14:58:23 |
      +----------------------+----------------------+---------+---------+---------------------+---------------------+
      ```

  1. Get the ID of the VM to link:

      ```bash
      yc compute instance list
      ```
      
      Result:
      
      ```text
      +----------------------+---------+---------------+---------+--------------+-------------+
      |          ID          |  NAME   |    ZONE ID    | STATUS  | EXTERNAL IP  | INTERNAL IP |
      +----------------------+---------+---------------+---------+--------------+-------------+
      | jklp0o9i8012******** | my-vm-1 | ru-central1-b | RUNNING | 51.250.**.** | 192.168.*.* |
      | mnoa5s6d8345******** | my-vm-2 | ru-central1-b | RUNNING | 84.201.**.** | 192.168.*.* |
      +----------------------+---------+---------------+---------+--------------+-------------+
      ```

      To get the BareMetal server IDs, select **BareMetal** from the list of services of the relevant [folder](../../../resource-manager/concepts/resources-hierarchy.md#folder) in the [management console](https://console.yandex.cloud). The IDs are specified in the server list in the **ID** field.

  1. Link the VM or BareMetal server by the policy ID:

      ```bash
      yc backup policy apply <policy_ID> \
        --instance-ids <VM_or_BareMetal_server_IDs>
      ```

      Where `--instance-ids` are the IDs of the VMs or BareMetal servers connected to Cloud Backup you need to link to the backup policy. Multiple IDs should be comma-separated.

  For more information about this command, see the [CLI reference](../../../cli/cli-ref/backup/cli-ref/policy/apply.md).

- Terraform {#tf}

  With [Terraform](https://www.terraform.io/), 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](https://github.com/hashicorp/terraform/blob/main/LICENSE). The [Yandex Cloud provider for Terraform](https://github.com/yandex-cloud/terraform-provider-yandex) is distributed under the [MPL-2.0](https://www.mozilla.org/en-US/MPL/2.0/) license.
  
  For more information about the provider resources, see the guides on the [Terraform](https://www.terraform.io/docs/providers/yandex/index.html) website or [its mirror](../../../terraform/index.md).

  If you do not have Terraform yet, [install it and configure the Yandex Cloud provider](../../../tutorials/infrastructure-management/terraform-quickstart.md#install-terraform).
  
  
  To manage infrastructure using Terraform under a service account or user accounts (a Yandex account, a federated account, or a local user), [authenticate](../../../terraform/authentication.md) using the appropriate method.

  {% note info %}

  Currently, you can only associate a [Compute Cloud VM instance](../../../compute/concepts/vm.md) with a [backup policy](../../concepts/policy.md) using Terraform. To associate a [BareMetal server](../../../baremetal/concepts/servers.md), use the [management console](https://console.yandex.cloud), [Yandex Cloud CLI](../../../cli/cli-ref/backup/cli-ref/policy/apply.md), or [API](../../backup/api-ref/Policy/apply.md).

  {% endnote %}

  To associate a VM with a backup policy:

  1. In the Terraform configuration file, describe the parameters for associating the VM with the policy:

      ```hcl
      resource "yandex_backup_policy_bindings" "test_backup_binding" {
        instance_id = "<VM_ID>"
        policy_id   = "<policy_ID>"
      }
      ```

      Where:

      * `instance_id`: [ID](../../../compute/operations/vm-info/get-info.md#outside-instance) of the VM you want to associate with the policy.
      * `policy_id`: [ID](get-info.md) of the policy to associate the VM with.

     For more information about `yandex_backup_policy_bindings` properties, see [this provider guide](../../../terraform/resources/backup_policy_bindings.md).
  1. Create the resources:

     1. In the terminal, navigate to the configuration file directory.
     1. Make sure the configuration is correct using this command:
     
        ```bash
        terraform validate
        ```
     
        If the configuration is valid, you will get this message:
     
        ```bash
        Success! The configuration is valid.
        ```
     
     1. Run this command:
     
        ```bash
        terraform plan
        ```
     
        You will see a list of resources and their properties. No changes will be made at this step. Terraform will show any errors in the configuration.
     1. Apply the configuration changes:
     
        ```bash
        terraform apply
        ```
     
     1. Type `yes` and press **Enter** to confirm the changes.

     Terraform will create all the required resources. You can check the new resources in the [management console](https://console.yandex.cloud) or using this [CLI](../../../cli/quickstart.md) command:

     ```bash
      yc backup policy list-applications
     ```

- API {#api}

  Use the [apply](../../backup/api-ref/Policy/apply.md) REST API method for the [Policy](../../backup/api-ref/Policy/index.md) resource or the [PolicyService/Apply](../../backup/api-ref/grpc/Policy/apply.md) gRPC API call.

{% endlist %}

#### See also {#see-also}

* [Creating a Linux VM with a connection to Cloud Backup](../create-vm.md)
* [Creating a Windows Server VM with a connection to Cloud Backup](../create-vm-windows.md)
* [Creating a backup policy](create.md)
* [Unlinking a VM or Yandex BareMetal server from a backup policy](detach-vm.md)
* [Connecting Compute Cloud VMs and Yandex BareMetal servers to Cloud Backup](../../concepts/vm-connection.md)
* [Backup policies](../../concepts/policy.md)