[Yandex Cloud documentation](../../../index.md) > [Yandex Compute Cloud](../../index.md) > [Step-by-step guides](../index.md) > Managing a VM > Moving a VM to a different folder

# Moving a VM to a different folder

When you create a VM, it is placed in the current folder.

In Yandex Cloud, you can move a VM to a different folder within a single cloud. You do not have to stop the VM to move it.

Learn more about the Yandex Cloud resource hierarchy [here](../../../resource-manager/concepts/resources-hierarchy.md).

## Limits {#limits}

When moving a VM, keep in mind the following limitations:

* [Yandex Monitoring](../../../monitoring/index.md) does not support metric relocation: existing metrics remain in the source folder, and new ones will be created in the destination folder.
* You can only move VMs within a single cloud.

## Moving a VM {#relocate-vm}

### Updating a VM folder {#change-folder}

{% list tabs group=instructions %}

- 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. Get a list of all VMs in the default folder:

      ```bash
      yc compute instance list
      ```

      Result:

      ```bash
      +----------------------+-----------------+---------------+---------+----------------------+
      |          ID          |       NAME      |    ZONE ID    | STATUS  |     DESCRIPTION      |
      +----------------------+-----------------+---------------+---------+----------------------+
      | fhm0b28lgfp4******** | first-instance  | ru-central1-a | RUNNING | my first vm via CLI  |
      | fhm9gk85nj7g******** | second-instance | ru-central1-a | RUNNING | my second vm via CLI |
      +----------------------+-----------------+---------------+---------+----------------------+
      ```      

  1. Get a list of all folders in the default cloud:

      ```bash
      yc resource-manager folder list
      ```

      Result:

      ```bash
      +----------------------+--------------------+------------------+--------+
      |          ID          |        NAME        |      LABELS      | STATUS |
      +----------------------+--------------------+------------------+--------+
      | b1gd129pp9ha******** | my-folder          |                  | ACTIVE |
      | b1g66mft1vop******** | default            |                  | ACTIVE |
      +----------------------+--------------------+------------------+--------+
      ```

  1. See the description of the CLI command for moving a VM:

      ```bash
      yc compute instance move --help
      ```

  1. Move the VM to a different folder by specifying the following values: 
      
      * In `id`, enter the VM ID, e.g., `fhm0b28lgfp4********`.
      * In `destination-folder-id`, specify the destination folder ID, e.g., `b1gd129pp9ha********`.

      ```bash
      yc compute instance move \
        --id fhm0b28lgfp4******** \
        --destination-folder-id b1gd129pp9ha********
      ```

      For more information about the `yc compute instance move` command, see the [CLI reference](../../../cli/cli-ref/compute/cli-ref/instance/move.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.

  1. [Configure](../../../resource-manager/operations/folder/set-access-bindings.md) access permissions for the target folder. The account from the source folder you are going to use to perform the operation must have at least the `compute.editor` [role](../../security/index.md#compute-editor) for the target folder.

  1. [Get the target folder ID](../../../resource-manager/operations/folder/get-id.md).
  1. In the configuration file, add the following parameters to the `yandex_compute_instance` resource:

      ```bash
      resource "yandex_compute_instance" "vm-1" {
          ...
          allow_stopping_for_update = true
          folder_id = <target_folder_ID>
          ...
      }
      ```

      Where:

      * `allow_stopping_for_update`: Parameter to allow the VM to stop for updates.
      * `folder_id`: ID of the folder to deploy the VM in (by default, specified from the [environment variable](../../../tutorials/infrastructure-management/terraform-quickstart.md#get-credentials)).

      For more information about `yandex_compute_instance` properties, see [this provider guide](../../../terraform/resources/compute_instance.md).

  1. Apply the new configuration:

      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 update all the required resources. You can check the update using the [management console](https://console.yandex.cloud).

- API {#api}

  Use the [move](../../api-ref/Instance/move.md) REST API method for the [Instance](../../api-ref/Instance/index.md) resource or the [InstanceService/Move](../../api-ref/grpc/Instance/move.md) gRPC API call.

  **Example**

  Below is an example of a Bash script for Linux.
  
  To use it, [get authenticated](../../api-ref/authentication.md) with the API and install [cURL](https://curl.haxx.se).

  You can move a VM without stopping it.

  1. Create a script file:

      ```bash
      sudo touch <file_name>
      ```

  1. Open the file to write the script to:

      ```bash
      sudo nano <file_name>
      ```
  
  1. Place the script in the file: 

      ```bash
      #!/bin/bash

      # Creating variables

      export IAM_TOKEN=`yc iam create-token`
      
      instanceId='<VM_ID>'
      bootDiskId='<VM_boot_disk_ID>'
      destinationFolderId='<folder_ID>'
      
      # Moving a VM

      curl \
        --request POST \
        --header "Authorization: Bearer ${IAM_TOKEN}" \
        --data '{ "destinationFolderId": "'"${destinationFolderId}"'" }' \
        "https://compute.api.cloud.yandex.net/compute/v1/instances/{${instanceId}}:move"
      
      # Moving a boot disk

      curl \
        --request POST \
        --header "Authorization: Bearer ${IAM_TOKEN}" \
        --data '{ "destinationFolderId": "'"${destinationFolderId}"'" }' \
        "https://compute.api.cloud.yandex.net/compute/v1/disks/{${bootDiskId}}:move"
      ```

      Where:

      * `IAM_TOKEN`: IAM token for API authentication.
      * `instanceId`: ID of the VM to move.
      * `bootDiskId`: ID of the boot disk of the VM to move.
      * `destinationFolderId`: ID of the folder to move the VM to.

  1. Make the file executable:

      ```bash
      chmod +x <file_name>
      ```

  1. Run the script:

      ```bash
      ./<file_name>
      ```

{% endlist %}

### Updating a VM subnet {#change-subnet}

After moving a VM, its network interfaces remain connected to the [subnets](../../../vpc/concepts/network.md#subnet) in the source folder. To connect your VM to subnets in the destination folder, follow these steps:

{% list tabs group=instructions %}

- Management console {#console}

  1. In the [management console](https://console.yandex.cloud), select the folder you moved the VM to.
  1. Navigate to **Compute Cloud**.
  1. Click the VM name.
  1. Click **Stop**.
  1. In the window that opens, click **Stop**.
  1. Under **Network**, click ![image](../../../_assets/console-icons/ellipsis.svg) in the top-right corner of the relevant network interface section and select **Edit**.
  1. In the **Subnet** field, select a new subnet and click **Save**.
      If a VM has multiple [network interfaces](../../concepts/network.md), update the subnet for each one.
  1. Click **Start**.

- 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. See the description of the CLI command for updating a subnet:

      ```bash
      yc compute instance update-network-interface --help
      ```

  1. Stop the VM:

      ```bash
      yc compute instance stop fhm0b28lgfp4********
      ```

  1. Get a list of VM network interfaces by specifying the VM ID:

     ```bash
     yc compute instance get fhm0b28lgfp4********
     ```

     Result:

     ```yml
     ...
     network_interfaces:
       - index: "0"
         mac_address: d0:0d:24:**:**:**
         subnet_id: e2lpp96bvvgp********
         primary_v4_address:
           address: 192.168.2.23
       - index: "1"
         mac_address: d0:1d:24:**:**:**
         subnet_id: e2lrucutusnd********
         primary_v4_address:
           address: 192.168.1.32
       - index: "2"
         mac_address: d0:2d:24:**:**:**
         subnet_id: e2lv9c6aek1d********
         primary_v4_address:
           address: 192.168.4.26
     ...
     ```

     Save the `index` field value, i.e., the number of the network interface you want to connect to a different subnet.

  1. Run this command:

      ```bash
      yc compute instance update-network-interface fhm0b28lgfp4******** \
        --subnet-id e2lfibapq818******** \
        --ipv4-address auto \
        --network-interface-index 0 \
        --security-group-id enpi8m85mj14********
      ```

      Where:

      * `--subnet-id`: Subnet in the destination folder.
      * `--ipv4-address`: Internal IP address of the VM network interface in the subnet in the destination folder. Set to `auto` to enable automatic internal address assignment.
      * `--network-interface-index`: VM's network interface number you previously saved.
      * `--security-group-id`: Security group that will be assigned to the VM's network interface.

      Result:

      ```text
      done (9s)
      id: fhm0b28lgfp4********
      folder_id: b1gd73mbrli7********
      created_at: "2023-11-16T06:09:46Z"
      name: oslogigor1
      zone_id: ru-central1-a
      platform_id: standard-v3
      resources:
        memory: "2147483648"
        cores: "2"
        core_fraction: "100"
      status: STOPPED
      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: epdophaf2gh9********
        auto_delete: true
        disk_id: epdophaf2gh9********
      network_interfaces:
        - index: "0"
          mac_address: d0:0d:24:**:**:**
          subnet_id: e2lfibapq818********
          primary_v4_address:
            address: 10.129.0.22
          security_group_ids:
            - enpi8m85mj14********
        - index: "1"
          mac_address: d0:1d:24:**:**:**
          subnet_id: e2lrucutusnd********
          primary_v4_address:
            address: 192.168.1.32
        - index: "2"
          mac_address: d0:2d:24:**:**:**
          subnet_id: e2lv9c6aek1d********
          primary_v4_address:
            address: 192.168.4.26
      gpu_settings: {}
      fqdn: relocated-vm.ru-central1.internal
      scheduling_policy: {}
      network_settings:
      type: STANDARD
      placement_policy: {}
      ```

      If a VM has multiple network interfaces, update the subnet for each one.

  1. Run the VM:

      ```bash
      yc compute instance start fhm0b28lgfp4********
      ```

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

  1. [Configure](../../../resource-manager/operations/folder/set-access-bindings.md) access permissions for the folder you are updating the VM subnet in. The account used to perform the operation must have at least the `vpc.admin` [role](../../../vpc/security/index.md#vpc-admin) for the this folder.

  1. If an additional subnet already exists, [obtain](../../../vpc/operations/subnet-get-info.md) its ID.
  1. Edit the `yandex_compute_instance` resource in the configuration file:

      ```hcl
      resource "yandex_compute_instance" "vm-1" {
        ...
        network_interface {
          subnet_id = "<subnet_ID>"
        }

        allow_stopping_for_update = true
        ...
      }
      ```

      Where:

      * `subnet_id`: [Subnet](../../../vpc/concepts/network.md#subnet) ID.
      * `allow_stopping_for_update`: Parameter to allow the VM to stop for updates.

      For more information about `yandex_compute_instance` properties, see [this provider guide](../../../terraform/resources/compute_instance.md).

  1. Apply the new configuration:

     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 update all the required resources. You can check the update using the [management console](https://console.yandex.cloud).

- API {#api}

  Use the [updateNetworkInterface](../../api-ref/Instance/updateNetworkInterface.md) REST API method for the [Instance](../../api-ref/Instance/index.md) resource or the [InstanceService/UpdateNetworkInterface](../../api-ref/grpc/Instance/updateNetworkInterface.md) gRPC API call.

{% endlist %}