Moving a VM to another folder
When you create a VM, it is placed in the current folder.
In Yandex Cloud, you can move a VM to another folder within one cloud. You do not have to stop the VM when moving it.
You can learn more about the resource hierarchy in Yandex Cloud here.
Limitations
Limitations when moving a VM:
- In Yandex Monitoring, metrics are not movable. The metrics in the previous folder stay there and new metrics will already be created in the new folder.
- You can move VMs only within a single cloud.
Moving a VM
Updating a VM's folder
If you do not have the Yandex Cloud command line interface yet, install and initialize it.
The folder specified in the CLI profile is used by default. You can specify a different folder using 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 | +----------------------+--------------------+------------------+--------+
-
View the description of the CLI command for moving a VM:
yc compute instance move --help
-
Move the VM to another folder with the following parameters:
- In
id
, enter the ID of the VM, e.g.,fhm0b28lgfp4********
. - In
destination-folder-id
, enter the ID of the destination folder, 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 a sample Bash script for Linux OS.
To use the example, authenticate in the API and install cURL
You can move a VM without stopping it.
-
Create a script file:
sudo touch <filename>
-
Open the file to write the script to:
sudo nano <filename>
-
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 the VM curl -X POST "https://compute.api.cloud.yandex.net/compute/v1/instances/{${instanceId}}:move" \ -H "Authorization: Bearer ${IAM_TOKEN}" \ -d '{ "destinationFolderId": "'"${destinationFolderId}"'" }' # Moving the boot disk curl -X POST "https://compute.api.cloud.yandex.net/compute/v1/disks/{${bootDiskId}}:move" \ -H "Authorization: Bearer ${IAM_TOKEN}" \ -d '{ "destinationFolderId": "'"${destinationFolderId}"'" }'
Where:
IAM_TOKEN
: IAM token used for authentication in the API.instanceId
: ID of the VM to move.bootDiskId
: ID of the boot disk of the VM being moved.destinationFolderId
: ID of the folder to move the VM to.
-
Make the file executable:
chmod +x <filename>
-
Run the script:
./<filename>
Updating a VM's subnet
A moved VM remains connected to the subnet in the source folder. To move your VM to a subnet in the destination folder:
- In the management console
, select the folder where you moved the VM. - Select Compute Cloud.
- Click the VM name.
- Click Stop.
- In the window that opens, click Stop.
- Under Network interface, click
- In the Subnet field, select a new subnet and click Save.
- Click Start.
If you do not have the Yandex Cloud command line interface yet, install and initialize it.
The folder specified in the CLI profile is used by default. You can specify a different folder using the --folder-name
or --folder-id
parameter.
-
Stop the VM:
yc compute instance stop fhm0b28lgfp4********
-
View a description of the update subnet CLI command:
yc compute instance update-network-interface --help
-
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
: VM internal IP.--network-interface-index
: VM's network interface index.--security-group-id
: ID of the security group to be assigned to the VM.
Result:
done (9s) id: epdk82knf9rj******** folder_id: b1gd73mbrli7******** created_at: "2023-11-16T06:09:46Z" name: oslogigor1 zone_id: ru-central1-b 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:14:40:a9:77 subnet_id: e2lfibapq818******** primary_v4_address: address: 10.129.0.22 gpu_settings: {} fqdn: relocated-vm.ru-central1.internal scheduling_policy: {} network_settings: type: STANDARD placement_policy: {}
-
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.