[Yandex Cloud documentation](../../index.md) > [Yandex Network Load Balancer](../index.md) > [Step-by-step guides](index.md) > Network load balancers > Moving a VM to a target group in a new availability zone

# Moving a VM to a target group in a new availability zone


To move NLB backend VMs to a target group in a different [availability zone](../../overview/concepts/geo-scope.md):

1. [Move](../../compute/operations/vm-control/vm-change-zone.md) your VMs to the new availability zone.
1. Replace [targets](../concepts/target-resources.md) in the target group:

    {% list tabs group=instructions %}
    
    - Management console {#console}
    
      1. In the [management console](https://console.yandex.cloud), select the [folder](../../resource-manager/concepts/resources-hierarchy.md#folder) containing the [network load balancer](../concepts/index.md).
      1. Navigate to **Network Load Balancer**.
      1. In the left-hand panel, select ![image](../../_assets/console-icons/target.svg) **Target groups**.
      1. Select the [target group](../concepts/target-resources.md) in question.
      1. Under **Targets**, remove the old [VMs](../../compute/concepts/vm.md) and add new ones.
    
    - 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](../../cli/index.md) command for removing targets from a [target group](../concepts/target-resources.md):
    
         ```bash
         yc load-balancer target-group remove-targets --help
         ```
    
      1. [Get](target-group-list.md#list) a list of [Network Load Balancer](../index.md) target groups in the default [folder](../../resource-manager/concepts/resources-hierarchy.md#folder).
      1. [Get](target-group-list.md#get) a list of targets linked to the target group.
      1. Remove old [VMs](../../compute/concepts/vm.md) from the target group:
    
         ```bash
         yc load-balancer target-group remove-targets <target_group_ID> \
           --target subnet-id=<VM_1_subnet_ID>,address=<VM_1_internal_IP_address> \
           --target subnet-id=<VM_2_subnet_ID>,address=<VM_2_internal_IP_address> \
           ...
           --target subnet-id=<VM_n_subnet_ID>,address=<VM_n_internal_IP_address>
         ```
    
         Where:
         * `<target_group_ID>`: ID of the target group to remove targets from.
         * `--target`: Settings of the target within the target group:
           * `subnet-id`: [Subnet](../../vpc/concepts/network.md#subnet) ID of the target to remove.
           * `address`: [Internal IP address](../../vpc/concepts/address.md#internal-addresses) of the target to remove.
    
         Result:
    
         ```text
         done (4s)
         id: enplckhtn77s********
         folder_id: b1gt6g8ht345********
         created_at: "2023-10-16T10:42:18Z"
         name: sample-tg
         region_id: ru-central1
         targets:
         ```
    
      1. See the description of the CLI command for adding targets to a target group:
    
         ```bash
         yc load-balancer target-group add-targets --help
         ```
    
      1. Get a list of subnets in the default folder:
    
         ```bash
         yc vpc subnet list
         ```
    
         Result:
    
          ```text
          +----------------------+------------+----------------------+----------------+---------------+------------------+
          |          ID          |    NAME    |      NETWORK ID      | ROUTE TABLE ID |     ZONE      |      RANGE       |
          +----------------------+------------+----------------------+----------------+---------------+------------------+
          | b0cnd1srghnm******** | rucentrald | enpv51f8lple******** |                | ru-central1-d | [192.168.3.0/24] |
          | e2li9tcgi7ii******** | rucentralb | enpv51f8lple******** |                | ru-central1-b | [192.168.0.0/24] |
          | e9b4a9ksc88k******** | rucentrala | enpv51f8lple******** |                | ru-central1-a | [192.168.1.0/24] |
          +----------------------+------------+----------------------+----------------+---------------+------------------+
          ```
    
      1. Get a list of VMs in the default folder:
    
         ```bash
         yc compute instance list
         ```
    
         Result:
    
         ```text
         +----------------------+------------+-------------------+---------+-----------------+--------------+
         |          ID          |    NAME    |      ZONE ID      | STATUS  |   EXTERNAL IP   | INTERNAL IP  |
         +----------------------+------------+-------------------+---------+-----------------+--------------+
         | fhmkchjddi40******** | sample-vm1 | ru-central1-a | RUNNING | 130.193.**.***  | 192.168.1.9  |
         | fhm13ts43oml******** | sample-vm2 | ru-central1-a | RUNNING | 158.160.***.*** | 192.168.1.32 |
         | epdsj30mndgj******** | sample-vm3 | ru-central1-b | RUNNING | 51.250.***.***  | 192.168.0.7  |
         +----------------------+------------+-------------------+---------+-----------------+--------------+
         ```
    
      1. Add targets to the target group:
    
         ```bash
         yc load-balancer target-group add-targets <target_group_ID> \
           --target subnet-id=<VM_1_subnet_ID>,address=<VM_1_internal_IP_address> \
           --target subnet-id=<VM_2_subnet_ID>,address=<VM_2_internal_IP_address>
           ...
           --target subnet-id=<VM_n_subnet_ID>,address=<VM_n_internal_IP_address>
         ```
    
         Where:
         * `<target_group_ID>`: ID of the target group to add targets to.
         * `--target`: Settings of the target within the target group:
           * `subnet-id`: Subnet ID of the target to add.
           * `address`: Internal IP address of the target to add.
    
         Result:
    
         ```text
         done (4s)
         id: enplckhtn77s********
         folder_id: b1gt6g8ht345********
         ...
             address: 192.168.1.32
           - subnet_id: e9b4a9ksc88k********
             address: 192.168.1.9
         ```
    
    - 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. Open the Terraform configuration file, find the section describing the [target group](../concepts/target-resources.md), and edit the `subnet_id` and `address` values under `target`:
    
         {% cut "Target group description example" %}
    
         ```hcl
         resource "yandex_lb_target_group" "foo" {
           name        = "<target_group_name>"
           target {
             subnet_id = "<subnet_ID>"
             address   = "<resource_1_internal_IP_address>"
           }
           target {
             subnet_id = "<subnet_ID>"
             address   = "<resource_2_internal_IP_address>"
           }
         }
         ```
    
         {% endcut %}
    
      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.
    
      You can check updates in the [management console](https://console.yandex.cloud) or using this [CLI](../../cli/index.md) command:
    
      ```bash
      yc load-balancer target-group get <target_group_name>
      ```
    
    - API {#api}
    
      1. To remove targets from a [target group](../concepts/target-resources.md), use the [removeTargets](../api-ref/TargetGroup/removeTargets.md) REST API method for the [TargetGroup](../api-ref/TargetGroup/index.md) resource or the [TargetGroupService/RemoveTargets](../api-ref/grpc/TargetGroup/removeTargets.md) gRPC API call.
      1. To add targets to a target group, use the [addTargets](../api-ref/TargetGroup/addTargets.md) REST API method for the [TargetGroup](../api-ref/TargetGroup/index.md) resource or the [TargetGroupService/AddTargets](../api-ref/grpc/TargetGroup/addTargets.md) gRPC API call.
    
    {% endlist %}

1. If you are using an [internal network load balancer](../concepts/specifics.md#nlb-int-routing) and need to move it to a subnet in the new availability zone that will host your VMs:

    1. [Delete](listener-remove.md) the current listener of your load balancer.
    1. [Add](listener-add.md) a new listener for the new availability zone.

    This will change the network load balancer IP address.

1. [Make sure](check-resource-health.md) the target group VMs in the new availability zone have passed [health checks](../concepts/health-check.md) and got the `HEALTHY` status.

    If the new VMs remain `UNHEALTHY` for too long, make sure their security groups are configured to allow [health check](../concepts/health-check.md#target-statuses) traffic.