[Yandex Cloud documentation](../../../index.md) > [Yandex Compute Cloud](../../index.md) > [Step-by-step guides](../index.md) > Managing an instance group > Moving an instance group with a network load balancer to a different availability zone

# Moving an instance group with a network load balancer to a different availability zone


To move an [instance group](../../concepts/instance-groups/index.md) with a [network load balancer](../../../network-load-balancer/concepts/index.md) created using [Yandex Network Load Balancer](../../../network-load-balancer/index.md):
1. [Create](../../../vpc/operations/subnet-create.md) a [subnet](../../../vpc/concepts/network.md#subnet) in the [availability zone](../../../overview/concepts/geo-scope.md) you want to move your instance group to.
1. Add the group instances to the new availability zone:

   {% list tabs group=instructions %}
   
   - Management console {#console}
   
     1. In the [management console](https://console.yandex.cloud), open the [folder](../../../resource-manager/concepts/resources-hierarchy.md#folder) containing the [instance group](../../concepts/instance-groups/index.md) you need.
     1. Navigate to **Compute Cloud**.
     1. In the left-hand panel, select ![image](../../../_assets/console-icons/layers-3-diagonal.svg) **Instance groups**.
     1. Select the instance group to update.
     1. In the top-right corner, click ![image](../../../_assets/console-icons/pencil.svg) **Edit**.
     1. Under **Allocation**, add the [availability zone](../../../overview/concepts/geo-scope.md) you want to move the instance group to. 
     1. If your instance group is a [manually scaled](../../concepts/instance-groups/scale.md#fixed-scale) one, under **Scaling**, specify a group size sufficient to place instances in all the selected availability zones.
   
        You will be able to reset the number of instances back to the initial one after all the instances in the group are moved to the new availability zone and deleted from the previous one.
     1. If your instance group is an [autoscaling](../../concepts/instance-groups/scale.md#auto-scale) one and has the `OPPORTUNISTIC` [shutdown strategy](../../concepts/instance-groups/policies/deploy-policy.md#strategy), change the strategy to `PROACTIVE` in the **Stop VMs by strategy** field.
   
        You will be able to reset the shutdown strategy back to `OPPORTUNISTIC` after all the instances in the group are moved to the new availability zone and deleted from the previous one.
     1. Click **Save**.
   
   - 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. Open the [instance group](../../concepts/instance-groups/index.md) [specification](../../concepts/instance-groups/specification.md) file and edit the [instance template](../../concepts/instance-groups/instance-template.md):
        * Under `allocation_policy`, add a new [availability zone](../../../overview/concepts/geo-scope.md).
        * Under `network_interface_specs`, add the ID of the previously created [subnet](../../../vpc/concepts/network.md#subnet).
        * If your instance group is a [manually scaled](../../concepts/instance-groups/scale.md#fixed-scale) one, under `scale_policy`, specify a group size sufficient to place instances in all the selected availability zones.
   
          You will be able to reset the number of instances back to the initial one after all the instances in the group are moved to the new availability zone and deleted from the previous one.
        * If your instance group is an [autoscaling](../../concepts/instance-groups/scale.md#auto-scale) one and has the `OPPORTUNISTIC` [shutdown strategy](../../concepts/instance-groups/policies/deploy-policy.md#strategy), change the strategy to `PROACTIVE` under `deploy_policy`.
   
          You will be able to reset the shutdown strategy back to `OPPORTUNISTIC` after all the instances in the group are moved to the new availability zone and deleted from the previous one.
     1. See the description of the CLI command for updating an instance group:
   
        ```bash
        yc compute instance-group update --help
        ```
   
     1. Get a list of all instance groups in the default [folder](../../../resource-manager/concepts/resources-hierarchy.md#folder):
   
        ```bash
        yc compute instance-group list
        ```
   
        Result:
   
        ```text
        +----------------------+---------------------------------+--------+--------+
        |          ID          |              NAME               | STATUS |  SIZE  |
        +----------------------+---------------------------------+--------+--------+
        | cl15sjqilrei******** | first-fixed-group-with-balancer | ACTIVE |      3 |
        | cl19s7dmihgm******** | test-group                      | ACTIVE |      2 |
        +----------------------+---------------------------------+--------+--------+
        ```
   
     1. Update the instance group:
   
        ```bash
        yc compute instance-group update \
          --id <instance_group_ID> \
          --file <instance_specification_file>
        ```
   
        Where:
        * `--id`: Instance group ID.
        * `--file`: Path to the instance group specification file.
   
        Result:
   
        ```text
        id: cl15sjqilrei********
        ...
        allocation_policy:
        zones:
        - zone_id: <previous_availability_zone>
        - zone_id: <new_availability_zone>
        ...
        ```
   
   - Terraform {#tf}
   
     If you do not have Terraform yet, [install it and configure the Yandex Cloud provider](../../../tutorials/infrastructure-management/terraform-quickstart.md#install-terraform).
     1. Open the Terraform configuration file for the [instance group](../../concepts/instance-groups/index.md). Specify the new [availability zone](../../../overview/concepts/geo-scope.md) under `allocation_policy` and the ID of the previously created [subnet](../../../vpc/concepts/network.md#subnet) under `network_interface`.
   
        ```hcl
        ...
        network_interface {
          subnet_ids = [
            "<ID_of_subnet_in_previous_availability_zone>",
            "<ID_of_subnet_in_new_availability_zone>"
          ]
        }
        ...
        allocation_policy {
          zones = [
            "<previous_availability_zone>",
            "<new_availability_zone>"
          ]
        }
        ...
        ```
   
        Where:
        * `zones`: Availability zones to host the instance group, both the new and previous ones.
        * `subnet_ids`: IDs of subnets in the availability zones to host the instance group.
   
        If your instance group is a [manually scaled](../../concepts/instance-groups/scale.md#fixed-scale) one, under `scale_policy`, specify a group size sufficient to place instances in all the selected availability zones.
   
        ```hcl
        ...
        scale_policy {
          fixed_scale {
            size = <number_of_instances_in_group>
          }
        }
        ...
        ```
   
        You will be able to reset the number of instances back to the initial one after all the instances in the group are moved to the new availability zone and deleted from the previous one.
   
        If your instance group is an [autoscaling](../../concepts/instance-groups/scale.md#auto-scale) one and has the `OPPORTUNISTIC` [shutdown strategy](../../concepts/instance-groups/policies/deploy-policy.md#strategy), change the strategy to `PROACTIVE`:
   
        ```hcl
        ...
        deploy_policy {
          strategy = "proactive" 
        }
        ...
        ```
   
        You will be able to reset the shutdown strategy back to `OPPORTUNISTIC` after all the instances in the group are moved to the new availability zone and deleted from the previous one.
   
        For more information about resource properties in Terraform, see [this provider guide](../../../terraform/resources/compute_instance_group.md).
     1. Apply the changes:
   
        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.
   
        This will add the new availability zone for your instance group. You can check the updates using the [management console](https://console.yandex.cloud) or this [CLI](../../../cli/quickstart.md) command:
   
        ```bash
        yc compute instance-group get <instance_group_name>
        ```
   
   - API {#api}
   
     Use the [update](../../instancegroup/api-ref/InstanceGroup/update.md) REST API method for the [InstanceGroup](../../instancegroup/api-ref/InstanceGroup/index.md) resource or the [InstanceGroupService/Update](../../instancegroup/api-ref/grpc/InstanceGroup/update.md) gRPC API call.
   
     If your instance group is a [manually scaled](../../concepts/instance-groups/scale.md#fixed-scale) one, specify a group size sufficient to place instances in all the selected availability zones. You will be able to reset the number of instances back to the initial one after all the instances in the group are moved to the new availability zone and deleted from the previous one.
   
     If your instance group is an [autoscaling](../../concepts/instance-groups/scale.md#auto-scale) one and has the `OPPORTUNISTIC` [shutdown strategy](../../concepts/instance-groups/policies/deploy-policy.md#strategy), change the strategy to `PROACTIVE`. You will be able to reset the shutdown strategy back to `OPPORTUNISTIC` after all the instances in the group are moved to the new availability zone and deleted from the previous one.
   
   {% endlist %}
   
   Wait until the instances appear in the new availability zone and get the `Running Actual` status.

1. Based on the [load balancer type](../../../network-load-balancer/concepts/nlb-types.md), follow these steps:
   * **External load balancer** (`EXTERNAL`):
     1. Wait until the resources of the target group in the new availability zone pass a [health check](../../../network-load-balancer/concepts/health-check.md) and get the `HEALTHY` status. See [Checking target health statuses](../../../network-load-balancer/operations/check-resource-health.md).

        After that, the load balancer will start routing traffic through the new availability zone. This may take up to two minutes. See [Achieving routing convergence in the availability zone](../../../network-load-balancer/concepts/specifics.md#nlb-zone-converge).
     1. Delete the group instances from the previous availability zone:

        {% list tabs group=instructions %}
        
        - Management console {#console}
        
          1. In the [management console](https://console.yandex.cloud), open the [folder](../../../resource-manager/concepts/resources-hierarchy.md#folder) containing the [instance group](../../concepts/instance-groups/index.md) you need.
          1. Navigate to **Compute Cloud**.
          1. In the left-hand panel, select ![image](../../../_assets/console-icons/layers-3-diagonal.svg) **Instance groups**.
          1. Select the instance group to update.
          1. In the top-right corner, click ![image](../../../_assets/console-icons/pencil.svg) **Edit**.
          1. Under **Allocation**, deselect the previous [availability zone](../../../overview/concepts/geo-scope.md).
          1. Click **Save**.
        
        - CLI {#cli}
        
          1. Open the [instance group](../../concepts/instance-groups/index.md) [specification](../../concepts/instance-groups/specification.md) file and edit the [instance template](../../concepts/instance-groups/instance-template.md):
             * Delete the previous availability zone from the `allocation_policy` section.
             * Delete the ID of the [subnet](../../../vpc/concepts/network.md#subnet) in the previous availability zone from the `network_interface_specs` section.
          1. Update the instance group:
        
             ```bash
             yc compute instance-group update \
               --id <instance_group_ID> \
               --file <instance_specification_file>
             ```
        
             Where:
             * `--id`: Instance group ID.
             * `--file`: Path to the instance group specification file.
        
             Result:
        
             ```text
             id: cl15sjqilrei********
             ...
             allocation_policy:
             zones:
             - zone_id: <new_availability_zone>
             ...
             ```
        
        - Terraform {#tf}
        
          1. Open the Terraform configuration file for the [instance group](../../concepts/instance-groups/index.md). Delete the previous [availability zone](../../../overview/concepts/geo-scope.md) from the `allocation_policy` section and the ID of the [subnet](../../../vpc/concepts/network.md#subnet) in the previous availability zone from the `network_interface` section:
        
             ```hcl
             ...
             network_interface {
               subnet_ids = ["<ID_of_subnet_in_new_availability_zone>"]
             }
             ...
             allocation_policy {
               zones = ["<new_availability_zone>"]
             }
             ...
             ```
        
             Where:
             * `zones`: Availability zone to move the instance group to. You can specify multiple availability zones.
             * `subnet_ids`: ID of the subnet in the availability zone you want to move your instance group to.
        
             For more information about resource properties in Terraform, see [this provider guide](../../../terraform/resources/compute_instance_group.md).
          1. Apply the changes:
        
             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.
        
             The group instances will be deleted from the previous availability zone. You can check the updates using the [management console](https://console.yandex.cloud) or this [CLI](../../../cli/quickstart.md) command:
        
             ```bash
             yc compute instance-group get <instance_group_name>
             ```
        
        - API {#api}
        
          Use the [update](../../instancegroup/api-ref/InstanceGroup/update.md) REST API method for the [InstanceGroup](../../instancegroup/api-ref/InstanceGroup/index.md) resource or the [InstanceGroupService/Update](../../instancegroup/api-ref/grpc/InstanceGroup/update.md) gRPC API call.
        
        {% endlist %}

   * **Internal load balancer** (`INTERNAL`):
     1. Move the resources requiring access to the internal load balancer to the previously created subnet.
     1. Switch to a new [listener](../../../network-load-balancer/concepts/listener.md):

        {% 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) with your load balancer.
          1. Navigate to **Network Load Balancer**.
          1. Click your load balancer's name.
          1. Under **Listeners**, click ![image](../../../_assets/horizontal-ellipsis.svg) and select **Remove listener**.
          1. At the top right, click ![image](../../../_assets/plus-sign.svg) **Create listener** and [create](../../../network-load-balancer/operations/listener-add.md) a new listener.

             When creating a new listener, select a subnet in the availability zone you want to move the load balancer to.
          1. Click **Add**.

        - CLI {#cli}

          1. See the description of the [CLI](../../../cli/index.md) command for deleting a listener:

             ```bash
             yc load-balancer network-load-balancer remove-listener --help
             ```

          1. Get a list of all network load balancers in the default [folder](../../../resource-manager/concepts/resources-hierarchy.md#folder):

             ```bash
             yc load-balancer network-load-balancer list
             ```

             Result:

             ```text
             +----------------------+---------------+-----------------+----------+----------------+------------------------+----------+
             |          ID          |     NAME      |    REGION ID    |   TYPE   | LISTENER COUNT | ATTACHED TARGET GROUPS |  STATUS  |
             +----------------------+---------------+-----------------+----------+----------------+------------------------+----------+
             | enp2btm6uvdr******** | nlb-34aa5-db1 | ru-central1 | INTERNAL |              0 |                        |  ACTIVE  |
             | enpvg9o73hqh******** | test-balancer | ru-central1 | EXTERNAL |              0 |                        |  ACTIVE  |
             +----------------------+---------------+-----------------+----------+----------------+------------------------+----------+
             ```

          1. Get the listener name:

             ```bash
             yc load-balancer network-load-balancer get <load_balancer_name>
             ```

             Result:

             ```text
             id: enp2btm6uvdr********
             folder_id: b1gmit33ngp3********
             ...
             listeners:
               - name: listener-980ee-af3
                 address: 172.17.0.36
             ```

          1. Delete the listener:

             ```bash
             yc load-balancer network-load-balancer remove-listener <load_balancer_name> \
               --listener-name <listener_name>
             ```

             Where `--listener-name` is the name of the listener to delete.

             Result:

             ```text
             done (1s)
             id: enpvg9o73hqh********
             folder_id: b1gmit33ngp3********
             created_at: "2023-08-09T13:44:57Z"
             name: nlb-34aa5-db1
             region_id: ru-central1
             status: INACTIVE
             type: INTERNAL
             ```

          1. See the description of the CLI command for adding a listener:

             ```bash
             yc load-balancer network-load-balancer add-listener --help
             ```

          1. Add a listener:

             ```bash
             yc load-balancer network-load-balancer add-listener <load_balancer_name> \
               --listener name=<listener_name>,`
                          `port=<port>,`
                          `target-port=<target_port>,`
                          `internal-subnet-id=<subnet_ID>
             ```

             Where:
             * `name`: Listener name.
             * `port`: Port the load balancer will listen to incoming traffic on.
             * `target-port`: Target port the load balancer will route traffic to.
             * `internal-subnet-id`: ID of the subnet in the availability zone you want to move your load balancer to.

             Result:

             ```text
             done (1s)
             id: enp2btm6uvdr********
             folder_id: b1gmit33ngp3********
             created_at: "2023-08-09T08:37:03Z"
             name: nlb-34aa5-db1
             region_id: ru-central1
             status: ACTIVE
             type: INTERNAL
             listeners:
               - name: new-listener
                 address: 10.0.0.16
                 port: "22"
                 protocol: TCP
                 target_port: "22"
                 subnet_id: e2lgp8o00g06********
                 ip_version: IPV4
             ```

        - Terraform {#tf}

          1. Open the Terraform file that contains the load balancer configuration and edit the `name` and `subnet_id` field values under `listener`:

             ```hcl
             listener { 
               name = "<new_listener_name>" 
               port = 80 
               target_port = 81 
               protocol = "tcp" 
               internal_address_spec { 
                 subnet_id = "<ID_of_subnet_in_target_availability_zone>" 
                 ip_version = "ipv4" 
               } 
             }
             ```

             Where:
             * `name`: Listener name.
             * `port`: Port on which the load balancer will listen to incoming traffic.
             * `target_port`: Target port the load balancer will route traffic to. 
             * `subnet_id`: ID of the subnet in the availability zone you want to move your instance group to.

             For more information about resource properties in Terraform, see [this provider guide](../../../terraform/resources/lb_network_load_balancer.md).
          1. Apply the changes:

             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.

             This will add the new listener to the new availability zone. You can check the new listener using the [management console](https://console.yandex.cloud).

        - API {#api}

          1. To remove a network load balancer listener, use the [removeListener](../../../network-load-balancer/api-ref/NetworkLoadBalancer/removeListener.md) REST API method for the [NetworkLoadBalancer](../../../network-load-balancer/api-ref/NetworkLoadBalancer/index.md) resource or the [NetworkLoadBalancerService/RemoveListener](../../../network-load-balancer/api-ref/grpc/NetworkLoadBalancer/removeListener.md) gRPC API call, providing the following in your request:
             * Load balancer ID in the `networkLoadBalancerId` parameter.
             * Name of the listener in the `listenerName` parameter.

             You can get the load balancer ID with a [list of network load balancers in the folder](../../../network-load-balancer/operations/load-balancer-list.md#list), and the listener name with [network load balancer details](../../../network-load-balancer/operations/load-balancer-list.md#get).
          1. To add a network load balancer's listener, use the [addListener](../../../network-load-balancer/api-ref/NetworkLoadBalancer/addListener.md) API method for the [NetworkLoadBalancer](../../../network-load-balancer/api-ref/NetworkLoadBalancer/index.md) resource or the [NetworkLoadBalancerService/AddListener](../../../network-load-balancer/api-ref/grpc/NetworkLoadBalancer/addListener.md) gRPC API call, and provide the following in your request:

             ```json
             {
               "listenerSpec": {
                 "name": "<listener_name>",
                 "port": "<incoming_port>",
                 "targetPort": "<target_port>",
                 "internalAddressSpec": {
                   "subnetId": "<subnet_ID>"
                 }
               }
             }
             ```

             Where:
             * `name`: Listener name.
             * `port`: Port on which the load balancer will listen to incoming traffic.
             * `targetPort`: Target port the load balancer will route traffic to.
             * `subnetId`: ID of the subnet in the availability zone you want to move your load balancer to.

        {% endlist %}

        {% note warning %}

        Your listener [IP address](../../../vpc/concepts/address.md) will change. Make sure to specify the new listener IP address in the settings of the resources sending traffic through the load balancer.

        {% endnote %}

     1. Delete the group instances from the previous availability zone:

        {% list tabs group=instructions %}
        
        - Management console {#console}
        
          1. In the [management console](https://console.yandex.cloud), open the [folder](../../../resource-manager/concepts/resources-hierarchy.md#folder) containing the [instance group](../../concepts/instance-groups/index.md) you need.
          1. Navigate to **Compute Cloud**.
          1. In the left-hand panel, select ![image](../../../_assets/console-icons/layers-3-diagonal.svg) **Instance groups**.
          1. Select the instance group to update.
          1. In the top-right corner, click ![image](../../../_assets/console-icons/pencil.svg) **Edit**.
          1. Under **Allocation**, deselect the previous [availability zone](../../../overview/concepts/geo-scope.md).
          1. Click **Save**.
        
        - CLI {#cli}
        
          1. Open the [instance group](../../concepts/instance-groups/index.md) [specification](../../concepts/instance-groups/specification.md) file and edit the [instance template](../../concepts/instance-groups/instance-template.md):
             * Delete the previous availability zone from the `allocation_policy` section.
             * Delete the ID of the [subnet](../../../vpc/concepts/network.md#subnet) in the previous availability zone from the `network_interface_specs` section.
          1. Update the instance group:
        
             ```bash
             yc compute instance-group update \
               --id <instance_group_ID> \
               --file <instance_specification_file>
             ```
        
             Where:
             * `--id`: Instance group ID.
             * `--file`: Path to the instance group specification file.
        
             Result:
        
             ```text
             id: cl15sjqilrei********
             ...
             allocation_policy:
             zones:
             - zone_id: <new_availability_zone>
             ...
             ```
        
        - Terraform {#tf}
        
          1. Open the Terraform configuration file for the [instance group](../../concepts/instance-groups/index.md). Delete the previous [availability zone](../../../overview/concepts/geo-scope.md) from the `allocation_policy` section and the ID of the [subnet](../../../vpc/concepts/network.md#subnet) in the previous availability zone from the `network_interface` section:
        
             ```hcl
             ...
             network_interface {
               subnet_ids = ["<ID_of_subnet_in_new_availability_zone>"]
             }
             ...
             allocation_policy {
               zones = ["<new_availability_zone>"]
             }
             ...
             ```
        
             Where:
             * `zones`: Availability zone to move the instance group to. You can specify multiple availability zones.
             * `subnet_ids`: ID of the subnet in the availability zone you want to move your instance group to.
        
             For more information about resource properties in Terraform, see [this provider guide](../../../terraform/resources/compute_instance_group.md).
          1. Apply the changes:
        
             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.
        
             The group instances will be deleted from the previous availability zone. You can check the updates using the [management console](https://console.yandex.cloud) or this [CLI](../../../cli/quickstart.md) command:
        
             ```bash
             yc compute instance-group get <instance_group_name>
             ```
        
        - API {#api}
        
          Use the [update](../../instancegroup/api-ref/InstanceGroup/update.md) REST API method for the [InstanceGroup](../../instancegroup/api-ref/InstanceGroup/index.md) resource or the [InstanceGroupService/Update](../../instancegroup/api-ref/grpc/InstanceGroup/update.md) gRPC API call.
        
        {% endlist %}

     1. Make sure the subnet in the previous availability zone has no resources left.
     1. [Delete](../../../vpc/operations/subnet-delete.md) the subnet in the previous availability zone.