# Editing a target group

You can add or remove [VMs](../../compute/concepts/vm.md) from a [target group](../concepts/target-group.md).

## Add a VM to a target group {#add-targets}

To add a VM to a target group:

{% list tabs group=instructions %}

- Management console {#console}

  1. In the [management console](https://console.yandex.cloud), select your target group [folder](../../resource-manager/concepts/resources-hierarchy.md#folder).
  1. Navigate to **Application Load Balancer**.
  1. In the left-hand panel, select ![image](../../_assets/console-icons/target.svg) **Target groups**.
  1. Click your target group name.
  1. Click **Add targets**.
  1. Select a VM from the list or add the target manually:
     1. In the **IP address** field, specify the target's [IP address](../../vpc/concepts/address.md) and select its [subnet](../../vpc/concepts/network.md#subnet).
     1. Optionally, if the target's IP address does not belong to [Yandex Virtual Private Cloud](../../vpc/index.md), select **Outside VPC**.

        For example, you can specify a private IPv4 address from your data center connected to Yandex Cloud through [Yandex Cloud Interconnect](../../interconnect/index.md). This IP address must belong to the [RFC 1918 private address range](https://datatracker.ietf.org/doc/html/rfc1918#section-3). For more information, see [Subnets](../../vpc/concepts/network.md#subnet).


     1. Click **Add target resource**.
  1. Click **Add**.

- 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 adding resources to target groups:

     ```bash
     yc alb target-group add-targets --help
     ```

  1. Run this command, with your target group name, the [subnet](../../vpc/concepts/network.md#subnet) name, and the VM’s [internal IP address](../../vpc/concepts/address.md#internal-addresses) specified:

     ```bash
     yc alb target-group add-targets \
       --name <target_group_name> \
       --target subnet-name=<subnet_name>,ip-address=<VM_internal_IP_address>
     ```

     Result:

     ```text
     done (1s)
     id: a5d751meibht********
     name: <target_group_name>
     targets:
     ...
       - ip_address: <VM_internal_IP_address>
         subnet_id: fo2tgfikh3he********
     created_at: "2021-02-11T11:16:27.770674538Z"
     ```

     When editing a target group, you can add targets outside the [Yandex Virtual Private Cloud](../../vpc/index.md), e.g., residing in your data center connected to Yandex Cloud via [Yandex Cloud Interconnect](../../interconnect/index.md). Target IP addresses must belong to the [RFC 1918 private address range](https://datatracker.ietf.org/doc/html/rfc1918#section-3). For more information, see [Subnets](../../vpc/concepts/network.md#subnet).


     Run this command, with your target group name and the target’s private IPv4 address specified:

     ```bash
     yc alb target-group add-targets \
       --name <target_group_name> \
       --target private-ip-address=true,ip-address=<private_IPv4_address_of_the_resource>
     ```

     Result:

     ```text
     done (1s)
     id: a5d751meibht4ev26...
     name: <target_group_name>
     targets:
     ...
       - ip_address: <private_IPv4_address_of_the_resource>
         private_ipv4_address: true
     created_at: "2023-07-25T08:55:14.172526884Z"
     ```

- 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 relevant documentation 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 and add the `target` section to your target group description:

     ```hcl
     resource "yandex_alb_target_group" "foo" {
       name           = "<target_group_name>"

       target {
         subnet_id    = "<subnet_ID>"
         ip_address   = "<VM_1_internal_IP_address>"
       }

       target {
         subnet_id    = "<subnet_ID>"
         ip_address   = "<VM_2_internal_IP_address>"
       }

       target {
         subnet_id    = "<subnet_ID>"
         ip_address   = "<VM_3_internal_IP_address>"
       }

       target {
         subnet_id    = "<subnet_ID>"
         ip_address   = "<VM_4_internal_IP_address>"
       }
     }
     ```

     Where `yandex_alb_target_group` specifies target group settings:
     * `name`: Target group name.
     * `target`: Target settings:
       * `subnet_id`: ID of the [subnet](../../vpc/concepts/network.md#subnet) hosting the VM. You can get the list of available subnets using the `yc vpc subnet list` [CLI](../../cli/index.md) command.
       * `ip_address`: VM [internal IP address](../../vpc/concepts/address.md#internal-addresses). You can get the list of internal IP addresses using the following CLI command: `yc vpc subnet list-used-addresses --id <subnet_ID>`.

     When editing a target group, you can add targets outside the [Yandex Virtual Private Cloud](../../vpc/index.md), e.g., residing in your data center connected to Yandex Cloud via [Yandex Cloud Interconnect](../../interconnect/index.md):

     ```hcl
     resource "yandex_alb_target_group" "foo" {
       name                   = "<target_group_name>"

       target {
         private_ipv4_address = true
         ip_address           = "<resource_1_IPv4_private_address>"
       }

       target {
         private_ipv4_address = true
         ip_address           = "<resource_2_IPv4_private_address>"
       }

       target {
         private_ipv4_address = true
         ip_address           = "<resource_3_IPv4_private_address>"
       }
     }
     ```


     Where `yandex_alb_target_group` specifies target group settings:
     * `name`: Target group name.
     * `target`: Target settings:
       * `private_ipv4_address`: Setting indicating that the IP address is outside Virtual Private Cloud.
       * `ip_address`: Resource’s private IPv4 address. This IP address must belong to the [RFC 1918 private address range](https://datatracker.ietf.org/doc/html/rfc1918#section-3). For more information, see [Subnets](../../vpc/concepts/network.md#subnet).

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

     You can check target group updates in the [management console](https://console.yandex.cloud) or using this CLI command:

     ```bash
     yc alb target-group get --name <target_group_name>
     ```

     {% note warning "Timeouts" %}
     
     The Terraform provider limits operations with Application Load Balancer target groups to 10 minutes.
     
     Operations in excess of this time will be interrupted.
     
     {% cut "How do I modify these limits?" %}
     
     Add the `timeouts` section to the target group description, e.g.:
     
     ```hcl
     resource "yandex_alb_target_group" "<target_group_name>" {
       ...
       timeouts {
         create = "60m"
         update = "60m"
         delete = "60m"
       }
     }
     ```
     
     {% endcut %}
     
     {% endnote %}

- API {#api}

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

## Remove a VM from a target group {#remove-targets}

To remove a VM from a target group:

{% list tabs group=instructions %}

- Management console {#console}

  1. In the [management console](https://console.yandex.cloud), select the folder with your target group.
  1. Navigate to **Application Load Balancer**.
  1. In the left-hand panel, select ![image](../../_assets/console-icons/target.svg) **Target groups**.
  1. Click the name of the target group in question.
  1. Click ![image](../../_assets/console-icons/ellipsis.svg) next to the VM you need, then select **Delete**.
  1. In the window that opens, click **Delete**.

- 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 removing targets from a target group:

     ```bash
     yc alb target-group remove-targets --help
     ```

  1. Run this command with your target group name, the subnet name, and the VM’s internal IP address specified:

     ```bash
     yc alb target-group remove-targets \
       --name <target_group_name> \
       --target subnet-name=<subnet_name>,ip-address=<VM_internal_IP_address>
     ```

     Result:

     ```text
     id: ds7urm6dn6cm********
     name: <target_group_name>
     folder_id: aoerb349v3h4********
     created_at: "2023-06-10T13:14:55.239094324Z"
     ```

     To remove a target group resource residing outside Virtual Private Cloud, e.g., in your data center connected to Yandex Cloud via Cloud Interconnect, run this command, with the target group name and the resource’s private IPv4 address specified:

     ```bash
     yc alb target-group remove-targets \
       --name <target_group_name> \
       --target private-ip-address=true,ip-address=<private_IPv4_address_of_the_resource>
     ```

     Result:

     ```text
     id: ds7urm6dn6cm********
     name: <target_group_name>
     folder_id: aoerb349v3h4********
     created_at: "2023-06-10T13:14:55.239094324Z"
     ```

- 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 relevant documentation 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 and delete the `target` section with your VM’s IP address from the target group description:

     Sample target group description in the Terraform configuration:

     ```hcl
     resource "yandex_alb_target_group" "foo" {
       name           = "<target_group_name>"

       target {
         subnet_id    = "<subnet_ID>"
         ip_address   = "<VM_1_internal_IP_address>"
       }

       target {
         subnet_id    = "<subnet_ID>"
         ip_address   = "<VM_2_internal_IP_address>"
       }

       target {
         subnet_id    = "<subnet_ID>"
         ip_address   = "<VM_3_internal_IP_address>"
       }
     }
     ```

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

     You can check target group updates in the [management console](https://console.yandex.cloud) or using this CLI command:

     ```bash
     yc alb target-group get --name <target_group_name>
     ```

- API {#api}

  Use the [removeTargets](../api-ref/TargetGroup/removeTargets.md) REST API method for the [TargetGroup](../api-ref/TargetGroup/index.md) resource or the [TargetGroup/RemoveTargets](../api-ref/grpc/TargetGroup/removeTargets.md) gRPC API call.

{% endlist %}