[Yandex Cloud documentation](../../index.md) > [Yandex Virtual Private Cloud](../index.md) > [Step-by-step guides](index.md) > Static routing > Creating a static route

# Creating a static route

{% note info %}

VMs with public IP addresses use the default static route (`0.0.0.0/0`). If you need to [create a NAT instance](../../tutorials/routing/nat-instance/index.md), create it in a separate subnet.

{% endnote %}

{% list tabs group=instructions %}

- Management console {#console}

  To create a route table with [static routes](../concepts/routing.md):

  1. In the [management console](https://console.yandex.cloud), select the folder where you need to create a static route.
  1. Navigate to **Virtual Private Cloud**.
  1. In the left-hand panel, select ![image](../../_assets/console-icons/route.svg) **Routing tables**.
  1. Click **Create**.
  1. Enter a name for the route table. Follow these naming requirements:

      * Length: between 3 and 63 characters.
      * It can only contain lowercase Latin letters, numbers, and hyphens.
      * It must start with a letter and cannot end with a hyphen.

  1. (Optional) Add a description of a route table.
  1. Select the network to create the route table in.
  1. Click **Add**.
  1. In the window that opens, enter the destination subnet prefix in CIDR notation.
  1. Specify the **Next hop**, which is an IP address from the [allowed ranges](../concepts/network.md#subnet).
  1. Click **Add**.
  1. Click **Create routing table**.

  To use static routes, associate the route table with a subnet:

  1. In the left-hand panel, select ![image](../../_assets/console-icons/nodes-right.svg) **Subnets**.
  1. In the row with the subnet, click ![image](../../_assets/console-icons/ellipsis.svg).
  1. In the menu that opens, select **Link routing table**.
  1. In the window that opens, select your route table from the list.
  1. Click **Link**.

- CLI {#cli}

  To create a route table with [static routes](../concepts/routing.md):

  1. View the description of the CLI command for creating route tables:

      ```bash
      yc vpc route-table create --help
      ```

  1. Get your cloud network ID:

      ```bash
      yc vpc network list
      ```

      Result:

      ```text
      +----------------------+-----------------+
      |          ID          |      NAME       |
      +----------------------+-----------------+
      | enp34hbpj8dq******** | yc-auto-subnet  |
      | enp846vf5fus******** | routes-test     |
      +----------------------+-----------------+
      ```

  1. Create a route table in one of the networks:

      ```bash
      yc vpc route-table create \
        --name=<table_name> \
        --network-id=<network_ID> \
        --route destination=<destination_prefix>,next-hop=<internal_IP_address>
      ```

      Where:

      * `--name`: Name of the route table.
      * `--network-id`: ID of the network where the table will be created.
      * `--route`: Route settings, which include these two parameters:

          * `destination`: Destination subnet prefix in CIDR notation, e.g., `0.0.0.0/0`.
          * `next-hop`: Internal IP address of the VM from the [allowed ranges](../concepts/network.md#subnet) the traffic will be sent through, e.g., `192.168.1.5`.

      Result:

      ```text
      ...done
      id: enpsi6b08q2v********
      folder_id: b1gqs1teo2q2********
      created_at: "2019-06-24T09:57:54Z"
      name: test-route-table
      network_id: enp846vf5fus********
      static_routes:
      - destination_prefix: 0.0.0.0/0
        next_hop_address: 192.168.1.5
      ```

  Link your route table to a subnet that will use its static routes:

  1. Get a list your cloud subnets:

      ```bash
      yc vpc subnet list
      ```

      Result:

      ```text
      +----------------------+------------------+----------------------+----------------+---------------+------------------+
      |          ID          |       NAME       |      NETWORK ID      | ROUTE TABLE ID |     ZONE      |      RANGE       |
      +----------------------+------------------+----------------------+----------------+---------------+------------------+
      | b0cf2b0u7nhl******** | subnet-1         | enp846vf5fus******** |                | ru-central1-a | [192.168.0.0/24] |
      +----------------------+------------------+----------------------+----------------+---------------+------------------+
      ```

  1. Associate the route table with one of the subnets:

      ```bash
      yc vpc subnet update <subnet_ID> \
        --route-table-id <route_table_ID>
      ```

      Result:

      ```text
      ..done
      id: b0cf2b0u7nhl********
      folder_id: b1gqs1teo2q2********
      created_at: "2019-03-12T13:27:22Z"
      name: subnet-1
      network_id: enp846vf5fus********
      zone_id: ru-central1-a
      v4_cidr_blocks:
      - 192.168.0.0/24
      route_table_id: enp1sdveovdp********
      ```

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

  To create a route table with [static routes](../concepts/routing.md):

  1. In the configuration file, describe the resources you want to create:

     * `name`: Route table name. Use the following name format:

          * Length: between 3 and 63 characters.
          * It can only contain lowercase Latin letters, numbers, and hyphens.
          * It must start with a letter and cannot end with a hyphen.

     * `network_id`: ID of the network to host the table.
     * `static_route`: Static route description:

        * `destination_prefix`: Destination subnet prefix in CIDR notation.
        * `next_hop_address`: Gateway VM internal IP address serving as the next hop for the [allowed](../concepts/network.md#subnet) traffic.

     Here is an example of the configuration file structure:

     ```hcl
     resource "yandex_vpc_route_table" "test-route-table" {
     name       = "<route_table_name>"
       network_id = "<network_ID>"
       static_route {
         destination_prefix = "<destination_prefix>"
         next_hop_address   = "<internal_IP_address>"
       }
     }
     ```

     To add, update, or delete a route table, use the `yandex_vpc_route_table` resource indicating the network in the `network_id` field, e.g., `network_id = yandex_vpc_network.test_route_table.id`.

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

  1. Make sure your configuration files are correct.

     1. In the terminal, navigate to the directory where you created your configuration file.
     1. Run a check using this command:

        ```bash
        terraform plan
        ```

     If the configuration is correct, the terminal will display a list of the resources and their settings. Otherwise, Terraform will show any detected errors.

  1. Deploy the cloud resources.

     1. If the configuration is correct, run this command:

        ```bash
        terraform apply
        ```

     1. Confirm creating the resources by typing `yes` and pressing **Enter**.

        This will create all the resources you need in the specified folder. You can see their detailed description using the [management console](https://console.yandex.cloud) or this [CLI](../../cli/quickstart.md) command:

        ```bash
        yc vpc route-table list
        ```

        Result:

        ```text
        +----------------------+-----------------------+-------------+----------------------+
        |          ID          |         NAME          | DESCRIPTION |      NETWORK-ID      |
        +----------------------+-----------------------+-------------+----------------------+
        | enpahlhr1vnl******** | terraform-route-table |             | enp0asmd9pr9******** |
        +----------------------+-----------------------+-------------+----------------------+
        ```

- API {#api}

  To create a route table and add [static routes](../concepts/routing.md) to it, use the [create](../api-ref/RouteTable/create.md) REST API method for the [RouteTable](../api-ref/RouteTable/index.md) resource or the [RouteTableService/Create](../api-ref/grpc/RouteTable/create.md) gRPC API call, and provide the following in the request:

  * ID of the folder the route table will reside in, in the `folderId` parameter.
  * Route table name in the `name` parameter. The name format is as follows:

      * Length: between 3 and 63 characters.
      * It can only contain lowercase Latin letters, numbers, and hyphens.
      * It must start with a letter and cannot end with a hyphen.

  * ID of the network the route table will reside in, in the `networkId` parameter.
  * Destination subnet prefix in CIDR notation in the `staticRoutes[].destinationPrefix` parameter.
  * Internal IP address of the VM the traffic will be routed through in the `staticRoutes[].nextHopAddress` parameter. The IP address must be within the [allowed range](../concepts/network.md#subnet).

  To use static routes, associate the route table with a subnet. Use the [update](../api-ref/Subnet/update.md) REST API method for the [Subnet](../api-ref/Subnet/index.md) resource or the [SubnetService/Update](../api-ref/grpc/Subnet/update.md) gRPC API call and provide the following in the request:

  * Subnet ID in the `subnetId` parameter.

      To get the subnet ID, use the [list](../api-ref/Subnet/list.md) REST API method for the [Subnet](../api-ref/Subnet/index.md) resource or the [SubnetService/List](../api-ref/grpc/Subnet/list.md) gRPC API call and provide the folder ID in the `folderId` request parameter.

      To learn how to find out the folder ID, see [Getting the folder ID](../../resource-manager/operations/folder/get-id.md).

  * Route table ID in the `routeTableId` parameter.
  * Name of the `routeTableId` parameter in the `updateMask` parameter.

  {% note warning %}
  
  The API method will assign default values to all the parameters of the object you are modifying unless you explicitly provide them in your request. To avoid this, list the settings you want to change in the `updateMask` parameter as a single comma-separated string.
  
  {% endnote %}

{% endlist %}

## Example {#example}

Create a route table and associate it with your subnet. The example uses the following properties:

* Folder ID: `b1g681qpemb4********`
* Network ID: `enp846vf5fus********`
* Subnet ID: `b0cf2b0u7nhl********`
* Route table name: `test-route-table`
* Destination subnet prefix: `0.0.0.0/0`
* Internal IP address: `192.168.1.5`

{% list tabs group=instructions %}

- CLI {#cli}

  1. Create a route table:

      ```bash
      yc vpc route-table create \
        --name=test-route-table \
        --network-id=enp846vf5fus******** \
        --route destination=0.0.0.0/0,next-hop=192.168.1.5
      ```

  1. Associate the route table with your subnet:

      ```bash
      yc vpc subnet update b0cf2b0u7nhl******** \
        --route-table-id enp1sdveovdp********
      ```

- Terraform {#tf}

  1. In the configuration file, list the route table properties and specify `route_table_id` for your subnet:

      ```hcl
      resource "yandex_vpc_route_table" "test_route_table" {
        name       = "test-route-table"
        network_id = "enp846vf5fus********"
        static_route {
          destination_prefix = "0.0.0.0/0"
          next_hop_address   = "192.168.1.5"
        }
      }

      resource "yandex_vpc_subnet" "example_subnet" {
        name           = "example-subnet"
        network_id     = "enp846vf5fus********"
        zone           = ru-central1-a
        v4_cidr_blocks = ["10.2.0.0/16"]
        # Associating the route table with the subnet
        route_table_id = yandex_vpc_route_table.test_route_table.id
      }
      ```

      For more information about the resources you can create with Terraform, see [this provider guide](../../terraform/resources/vpc_route_table.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. Apply the changes.

     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}

  1. To create a route table, use the [create](../api-ref/RouteTable/create.md) REST API method for the [RouteTable](../api-ref/RouteTable/index.md) resource or the [RouteTableService/Create](../api-ref/grpc/RouteTable/create.md) gRPC API call, and provide the following in the request body:

      ```api
      {
        "folderId": "b1g681qpemb4********",
        "name": "test-route-table",
        "networkId": "enp846vf5fus********",
        "staticRoutes": [
          {
            "destinationPrefix": "0.0.0.0/0",
            "nextHopAddress": "192.168.1.5"
          }
        ]
      }
      ```

  1. To associate a route table with a subnet, use the [update](../api-ref/Subnet/update.md) REST API method for the [Subnet](../api-ref/Subnet/index.md) resource or the [SubnetService/Update](../api-ref/grpc/Subnet/update.md) gRPC API call, and provide the following in the request body:

      ```api
      {
        "updateMask": "routeTableId",
        "subnet": {
          "routeTableId": "enpfs106jh40********"
        }
      }
      ```

{% endlist %}