[Yandex Cloud documentation](../../index.md) > [Yandex Network Load Balancer](../index.md) > [Step-by-step guides](index.md) > Network load balancers > Adding a listener

# Adding a listener to a network load balancer

{% list tabs group=instructions %}

- Management console {#console}
  
  To add a [listener](../concepts/listener.md) to a network load balancer:
  
  1. In the [management console](https://console.yandex.cloud), select the folder where you need to add a listener to a load balancer.
  1. Navigate to **Network Load Balancer**.
  1. Next to the load balancer to add a listener to, click ![image](../../_assets/console-icons/ellipsis.svg) and select **Add listener**.
  1. In the window that opens, specify these listener settings:

     * **Name**.
     * **Protocol**: `TCP` or `UDP`.

        {% note info %}

        By default, the listener uses TCP. To use UDP, [contact technical support](https://center.yandex.cloud/support).

        {% endnote %}

     * **Port** where the listener will listen for incoming traffic. The possible values range from `1` to `32767`.
     * **Target port** to which the load balancer will redirect traffic. The possible values range from `1` to `32767`.
     * 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.
  
  To add a [listener](../concepts/listener.md) to a network load balancer, run this command:

  ```bash
  yc load-balancer network-load-balancer add-listener <load_balancer_name_or_ID> \
     --listener name=<listener_name>,`
               `port=<port>,`
               `target-port=<target_port>,`
               `protocol=<protocol>,`
               `external-address=<external_IP_address>,`
               `external-ip-version=<IP_address_version>
  ```

  Where:

  * `--listener`: Listener settings:
      * `name`: Listener name.
      * `port`: Port on which the load balancer will listen to incoming traffic. The possible values range from `1` to `32767`.
      * `target-port`: Port to which the load balancer will redirect traffic. The possible values range from `1` to `32767`.
      * `protocol`: Protocol the listener will use, `tcp` or `udp`.
      * `external-address`: External IP address of the listener.
      * `external-ip-version`: External IP address version, `ipv4` or `ipv6`.

  You can get the load balancer ID and name with the [list of network load balancers in the folder](load-balancer-list.md#list).

- 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 and add the `listener` section to the network load balancer description:

     ```hcl
     resource "yandex_lb_network_load_balancer" "foo" {
       name = "<load_balancer_name>"
       ...
       listener {
         name = "<listener_name>"
         port = <port_number>
         external_address_spec {
           ip_version = "<IP_address_version>"
         }
       }
       ...
     }
     ```

     Where:

     * `name`: Name of the network load balancer.
     * `listener`: Listener settings:
       * `name`: Listener name.
       * `port`: Port in the range from `1` to `32767` the network load balancer will receive incoming traffic at.
       * `external_address_spec`: Specification of the listener for the external load balancer settings:
         * `ip_version`: External IP address specification. Specify the IP address version, `ipv4` or `ipv6`. The default value is `ipv4`.

     For more information about `yandex_lb_network_load_balancer` properties in Terraform, see [this provider guide](../../terraform/resources/lb_network_load_balancer.md).

  1. Make sure the settings are correct.

     1. In the command line, navigate to the directory that contains the current Terraform configuration files defining the infrastructure.
     1. Run this command:
     
        ```bash
        terraform validate
        ```
     
        Terraform will show any errors found in your configuration files.

  1. Add a listener.

     1. Run this command to view the planned changes:
     
        ```bash
        terraform plan
        ```
     
        If you described the configuration correctly, the terminal will display a list of the resources to update and their parameters. This is a verification step that does not apply changes to your resources.
     
     1. If everything looks correct, apply the changes:
        1. Run this command:
     
           ```bash
           terraform apply
           ```
     
        1. Confirm updating the resources.
        1. Wait for the operation to complete.

- API {#api}

  To add a listener to a network load balancer, use the [addListener](../api-ref/NetworkLoadBalancer/addListener.md) REST API method for the [NetworkLoadBalancer](../api-ref/NetworkLoadBalancer/index.md) resource or the [NetworkLoadBalancerService/AddListener](../api-ref/grpc/NetworkLoadBalancer/addListener.md) gRPC API call.

{% endlist %}

## Examples {#examples}

### Adding a listener to a network load balancer {#add-listener}

Add a listener with the following test settings to `test-load-balancer`:

* Name: `test-listener`
* Port: `80`
* Target port: `81`
* Protocol: `TCP`
* IP address version: `ipv4`

{% list tabs group=instructions %}

- CLI {#cli}

  Run this command:

  ```bash
  yc load-balancer network-load-balancer add-listener test-load-balancer \
     --listener name=test-listener,`
               `port=80,`
               `target-port=81,`
               `protocol=tcp,`
               `external-ip-version=ipv4
  ```

- Terraform {#tf}

  1. Open the Terraform configuration file and add the `listener` section to the network load balancer description:

     ```hcl
     resource "yandex_lb_network_load_balancer" "foo" {
       name = "test-load-balancer"
       listener {
         name        = "test-listener"
         port        = 80
         target_port = 81
         protocol    = "tcp"
         external_address_spec {
           ip_version = "ipv4"
         }
       }
     }
     ```

     For more information about the resources you can create with Terraform, see [this provider guide](../../terraform/resources/lb_network_load_balancer.md).

  1. Make sure the settings are correct.

     1. In the command line, navigate to the directory that contains the current Terraform configuration files defining the infrastructure.
     1. Run this command:
     
        ```bash
        terraform validate
        ```
     
        Terraform will show any errors found in your configuration files.

  1. Create a network load balancer.

     1. Run this command to view the planned changes:
     
        ```bash
        terraform plan
        ```
     
        If you described the configuration correctly, the terminal will display a list of the resources to update and their parameters. This is a verification step that does not apply changes to your resources.
     
     1. If everything looks correct, apply the changes:
        1. Run this command:
     
           ```bash
           terraform apply
           ```
     
        1. Confirm updating the resources.
        1. Wait for the operation to complete.

- API {#api}

  Use the [addListener](../api-ref/NetworkLoadBalancer/addListener.md) API method, providing the following in the request body:

  ```api
  {
    "listenerSpec": {
      "name": "test-listener",
      "port": "80",
      "protocol": "TCP",
      "targetPort": "81",
      "externalAddressSpec": {
        "ipVersion": "ipv4"
      }
    }
  }
  ```

{% endlist %}