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.
Limitations
When moving a VM, keep in mind the following limitations:
- Yandex Monitoring 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
Updating a VM folder
If you do not have the Yandex Cloud CLI yet, install and initialize it.
The folder specified in the CLI profile is used by default. You can specify a different folder through the --folder-name
or --folder-id
parameter.
-
Get a list of all VMs in the default folder:
yc compute instance list
Result:
+----------------------+-----------------+---------------+---------+----------------------+ | 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 | +----------------------+-----------------+---------------+---------+----------------------+
-
Get a list of all folders in the default cloud:
yc resource-manager folder list
Result:
+----------------------+--------------------+------------------+--------+ | ID | NAME | LABELS | STATUS | +----------------------+--------------------+------------------+--------+ | b1gd129pp9ha******** | my-folder | | ACTIVE | | b1g66mft1vop******** | default | | ACTIVE | +----------------------+--------------------+------------------+--------+
-
See the description of the CLI command for moving a VM:
yc compute instance move --help
-
Move the VM to a different folder with the following parameters:
- In
id
, enter the VM ID, e.g.,fhm0b28lgfp4********
. - In
destination-folder-id
, specify the destination folder ID, e.g.,b1gd129pp9ha********
.
yc compute instance move \ --id fhm0b28lgfp4******** \ --destination-folder-id b1gd129pp9ha********
For more information about the
yc compute instance move
command, see the CLI reference. - In
Use the move REST API method for the Instance resource or the InstanceService/Move gRPC API call.
Example
Below is an example of a Bash script for Linux.
To use it, get authenticated with the API and install cURL
You can move a VM without stopping it.
-
Create a script file:
sudo touch <file_name>
-
Open the file to write the script to:
sudo nano <file_name>
-
Place the script in the file:
#!/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.
-
Make the file executable:
chmod +x <file_name>
-
Run the script:
./<file_name>
Updating a VM subnet
After moving a VM, its network interfaces remain connected to the subnets in the source folder. To connect your VM to subnets in the destination folder, follow these steps:
- In the management console
, select the folder you moved the VM to. - Select Compute Cloud.
- Click the VM name.
- Click Stop.
- In the window that opens, click Stop.
- Under Network, click
in the top-right corner of the relevant network interface section and select Edit. - In the Subnet field, select a new subnet and click Save.
If a VM has multiple network interfaces, update the subnet for each one. - Click Start.
If you do not have the Yandex Cloud CLI yet, install and initialize it.
The folder specified in the CLI profile is used by default. You can specify a different folder through the --folder-name
or --folder-id
parameter.
-
See the description of the CLI command for updating a subnet:
yc compute instance update-network-interface --help
-
Stop the VM:
yc compute instance stop fhm0b28lgfp4********
-
Get a list of VM network interfaces by specifying the VM ID:
yc compute instance get fhm0b28lgfp4********
Result:
... 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. -
Run this command:
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 toauto
to enable automatic internal address assignment.--network-interface-index
: VM network interface number you saved earlier.--security-group-id
: ID of the security group to assign to the VM network interface.
Result:
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.
-
Run the VM:
yc compute instance start fhm0b28lgfp4********
Use the updateNetworkInterface REST API method for the Instance resource or the InstanceService/UpdateNetworkInterface gRPC API call.