[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 an L7 load balancer to a different availability zone

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


To move an [instance group](../../concepts/instance-groups/index.md) with a [Yandex Application Load Balancer](../../../application-load-balancer/index.md) [L7 load balancer](../../../application-load-balancer/concepts/application-load-balancer.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 [VM](../../concepts/vm.md) instance group to.
1. Enable traffic for the L7 load balancer in the new availability zone:

    {% list tabs group=instructions %}

    - Management console {#console}

      1. In the [management console](https://console.yandex.cloud), select the folder with your load balancer.
      1. Navigate to **Application Load Balancer**.
      1. Click ![image](../../../_assets/console-icons/ellipsis.svg) next to the load balancer you need, then select **Edit**.
      1. In the window that opens, under **Allocation**, enable traffic in the availability zone you want to move your instance group to.
      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. See the description of the CLI command for enabling load balancer traffic:

          ```bash
          yc application-load-balancer load-balancer enable-traffic --help
          ```

      1. Get a list of all L7 load balancers in the default folder:

          ```bash
          yc application-load-balancer load-balancer list
          ```

          Result:

          ```text
          +----------------------+-----------------------+-------------+----------------+---------+
          |          ID          |         NAME          |  REGION ID  | LISTENER COUNT | STATUS  |
          +----------------------+-----------------------+-------------+----------------+---------+
          | ds732hi8pn9n******** |      sample-alb1      | ru-central1 |              1 |  ACTIVE |
          | f3da23i86n2v******** |      sample-alb2      | ru-central1 |              1 |  ACTIVE |
          +----------------------+-----------------------+-------------+----------------+---------+
          ```

      1. Enable traffic:

          ```bash
          yc application-load-balancer load-balancer enable-traffic <load_balancer_name> \
            --zone <availability_zone>
          ```

          Where `--zone` is the availability zone you want to move your instance group to.

          Result:

          ```text
          id: ds7pmslal3km********
          name: sample-alb1
          folder_id: b1gmit33ngp3********
          status: ACTIVE
          region_id: ru-central1
          network_id: enpn46stivv8********
          allocation_policy:
            locations:
              - zone_id: ru-central1-a
                subnet_id: e9bavnqlbiuk********
                disable_traffic: true
              - zone_id: ru-central1-b
                  subnet_id: e2lgp8o00g06********
              - zone_id: ru-central1-d
                  subnet_id: b0cv501fvp13********
          log_group_id: ckgah4eo2j0r********
          security_group_ids:
            - enpdjc5bitmj********
          created_at: "2023-08-09T08:34:24.887765763Z"
          log_options: {}
          ```

    - 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 L7 load balancer and, under `allocation_policy`, specify the new availability zone and the ID of the subnet you created earlier:

         ```hcl
         ...
           allocation_policy {
             location {
               zone_id   = [
                 "<previous_availability_zone>",
                 "<new_availability_zone>"
               ]
               subnet_id = [
                 "<subnet_ID_in_previous_availability_zone>",
                 "<ID_of_subnet_in_new_availability_zone>"
               ]
             }
           }
         }
         ...
         ```

         Where:
         * `zone_id`: Availability zones where the L7 load balancer will accept traffic.
         * `subnet_id`: IDs of the subnets in these availability zones.

         For more information about resource properties in Terraform, see [this provider guide](../../../terraform/resources/alb_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.

         Incoming traffic will now be routed to the new availability zone through the L7 load balancer. You can check this using the [management console](https://console.yandex.cloud) or this [CLI](../../../cli/index.md) command:

         ```bash
         yc alb load-balancer get <L7_load_balancer_name>
         ```

   - API {#api}

     Use the [update](../../../application-load-balancer/api-ref/LoadBalancer/update.md) REST API method for the [LoadBalancer](../../../application-load-balancer/api-ref/LoadBalancer/index.md) resource or the [LoadBalancerService/Update](../../../application-load-balancer/api-ref/grpc/LoadBalancer/update.md) gRPC API call.

   {% endlist %}

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