Creating static routes
Note
The 0.0.0.0/0
default static route is used for VMs with public IP addresses. If you need to create a NAT instance, create it in a separate subnet.
To create a route table and add static routes:
-
In the management console
, go to the folder you need to create a static route in. -
In the list of services, select Virtual Private Cloud.
-
In the left-hand panel, select
Routing tables. -
Click Create.
-
Enter a name for the route table. The naming requirements are as follows:
- The name must be from 3 to 63 characters long.
- It may contain lowercase Latin letters, numbers, and hyphens.
- The first character must be a letter and the last character cannot be a hyphen.
-
(Optional) Add a description of a route table.
-
Select the network to create the route table in.
-
Click Add a route.
-
In the window that opens, enter the prefix of the destination subnet in CIDR notation.
-
Specify the Next hop, which is an IP address from the allowed ranges.
-
Click Add.
-
Click Create a routing table.
To use static routes, link the route table to a subnet:
- In the left-hand panel, select
Subnets. - In the row with the subnet you need, click
. - In the menu that opens, select Link routing table.
- In the window that opens, select the created table from the list.
- Click Link.
To create a route table and add static routes:
-
View the description of the CLI command for creating route tables:
yc vpc route-table create --help
-
Get the IDs of cloud networks in your cloud:
yc vpc network list
Result:
+----------------------+-----------------+ | ID | NAME | +----------------------+-----------------+ | enp34hbpj8dq******** | yc-auto-subnet | | enp846vf5fus******** | routes-test | +----------------------+-----------------+
-
Create a route table in one of the networks:
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
Where:
--name
: Name of the route table.--network-id
: ID of the network the table will be created in.--route
: Route settings, which include these two parameters:destination
: Destination subnet prefix in CIDR notation.next-hop
: Internal IP address of the VM from the allowed ranges the traffic will be sent through.
Result:
...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
To use static routes, link the route table to a subnet:
-
Get a list of subnets in your cloud:
yc vpc subnet list
Result:
+----------------------+------------------+----------------------+----------------+---------------+------------------+ | ID | NAME | NETWORK ID | ROUTE TABLE ID | ZONE | RANGE | +----------------------+------------------+----------------------+----------------+---------------+------------------+ | b0cf2b0u7nhl******** | subnet-1 | enp846vf5fus******** | | ru-central1-a | [192.168.0.0/24] | +----------------------+------------------+----------------------+----------------+---------------+------------------+
-
Link the route table to one of the subnets:
yc vpc subnet update b0cf2b0u7nhl******** --route-table-id enp1sdveovdp********
Result:
..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********
With Terraform
Terraform is distributed under the Business Source License
For more information about the provider resources, see the documentation on the Terraform
If you don't have Terraform, install it and configure the Yandex Cloud provider.
To create a route table and add static routes:
-
In the configuration file, describe the parameters of the resources you want to create:
-
name
: Name of the route table. The name format is as follows:- The name must be from 3 to 63 characters long.
- It may contain lowercase Latin letters, numbers, and hyphens.
- The first character must be a letter and the last character cannot be a hyphen.
-
network_id
: ID of the network the table will be created in. -
static_route
: Static route description:destination_prefix
: Destination subnet prefix in CIDR notation.next_hop_address
: Internal IP address of the VM from the allowed ranges the traffic will be routed through.
Here is an example of the configuration file structure:
resource "yandex_vpc_route_table" "lab-rt-a" { name = "<route_table_name>" network_id = "<network_ID>" static_route { destination_prefix = "10.2.0.0/16" next_hop_address = "172.16.10.10" } }
To add, update, or delete a route table, use the
yandex_vpc_route_table
resource indicating the network in thenetword id
field, e.g.network_id = "${yandex_vpc_network.lab-net.id}"
.For more information about the
yandex_vpc_route_table
resource parameters in Terraform, see the provider documentation . -
-
Make sure the configuration files are correct.
-
In the command line, go to the folder where you created the configuration file.
-
Run a check using this command:
terraform plan
If the configuration is described correctly, the terminal will display a list of created resources and their parameters. If the configuration contains any errors, Terraform will point them out.
-
-
Deploy cloud resources.
-
If the configuration does not contain any errors, run this command:
terraform apply
-
Confirm creating the resources: type
yes
in the terminal and press Enter.All the resources you need will then be created in the specified folder. You can check the new resources and their settings using the management console
or this CLI command:yc vpc route-table list
-
To create a route table and add static routes to it, use the create REST API method for the RouteTable resource or the RouteTableService/Create 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:- The name must be from 3 to 63 characters long.
- It may contain lowercase Latin letters, numbers, and hyphens.
- The first character must be a letter and the last character cannot be 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.
To use static routes, link the route table to a subnet. Use the update REST API method for the Subnet resource or the SubnetService/Update gRPC API call and provide the following in the request:
-
Network ID in the
subnetId
parameter.To get the subnet ID, use the list REST API method for the Subnet resource or the SubnetService/List 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.
-
Route table ID in the
routeTableId
parameter. -
The name of the
routeTableId
parameter in theupdateMask
parameter.
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.