Setting up a NAT gateway
vpc.admin and vpc.gateways.user are the minimum roles required to create and configure a NAT gateway.
To create and set up a NAT gateway:
-
In the management console
, go to the folder where you need to create a gateway. -
In the list of services, select Virtual Private Cloud.
-
In the left-hand panel, select Gateways.
-
Click Create.
-
Enter a name for the gateway. The naming requirements are as follows:
- It must be from 2 to 63 characters long.
- It can only contain lowercase Latin letters, numbers, and hyphens.
- It must start with a letter and cannot end with a hyphen.
-
(Optional) Add a gateway description.
-
The default gateway type is
Egress NAT. -
Click Save.
-
In the left-hand panel, select Routing tables.
-
Click Create to add a new table, or select an existing one.
-
Click Add.
-
In the window that opens, select
Gatewayin the Next hop field. -
In the Gateway field, select the NAT gateway you created. The destination prefix will apply automatically.
-
Click Add.
-
Click Save.
Next, associate the route table with a subnet to route traffic from it through the NAT gateway:
- In the left-hand panel, select
Subnets. - In the row with the subnet, click
. - In the menu that opens, select Link routing table.
- In the window that opens, select the created table from the list.
- Click Link.
If you do not have the Yandex Cloud CLI installed yet, install and initialize it.
By default, the CLI uses the folder specified when creating the profile. To change the default folder, use the yc config set folder-id <folder_ID> command. You can also set a different folder for any specific command using the --folder-name or --folder-id parameter.
-
View the description of the CLI create gateway command:
yc vpc gateway create --help -
Create a gateway in the default folder:
yc vpc gateway create \ --name test-gateway -
Get the gateway ID:
yc vpc gateway listResult:
+----------------------+--------------+-------------+ | ID | NAME | DESCRIPTION | +----------------------+--------------+-------------+ | enpkq1v2e7p0******** | test-gateway | | +----------------------+--------------+-------------+ -
Create a route table with the gateway as the next hop and the
0.0.0.0/0destination prefix:yc vpc route-table create \ --name=test-route-table \ --network-name=<network_name> \ --route destination=0.0.0.0/0,gateway-id=enpkq1v2e7p0********Where
--network-nameis the name of the network where you are creating the table. -
Associate the table with the subnet:
yc vpc subnet update <subnet_name> \ --route-table-name=test-route-table
With Terraform
Terraform is distributed under the Business Source License
For more information about the provider resources, see the relevant documentation on the Terraform
If you do not have Terraform yet, install it and configure the Yandex Cloud provider.
To create a NAT gateway, specify it as the next hop in the route table, and associate the table with the subnet, use the following configuration:
data "yandex_vpc_network" "net" {
folder_id = "<folder_ID>"
name = "<network_name>"
}
resource "yandex_vpc_subnet" "subnet" {
folder_id = "<folder_ID>"
name = "<subnet_name>"
v4_cidr_blocks = ["10.20.30.0/24"]
zone = "ru-central1-a"
network_id = data.yandex_vpc_network.net.id
route_table_id = yandex_vpc_route_table.rt.id
}
resource "yandex_vpc_gateway" "nat_gateway" {
folder_id = "<folder_ID>"
name = "test-gateway"
shared_egress_gateway {}
}
resource "yandex_vpc_route_table" "rt" {
folder_id = "<folder_ID>"
name = "test-route-table"
network_id = "<network_ID>"
static_route {
destination_prefix = "0.0.0.0/0"
gateway_id = yandex_vpc_gateway.nat_gateway.id
}
}
Where folder_id is the ID of the folder hosting the subnet.
-
Create a NAT gateway. Use the create REST API method for the Gateway resource or the GatewayService/Create gRPC API call, and provide the following in the request:
-
ID of the folder the gateway will reside in, in the
folderIdparameter. -
Gateaway name in the
nameparameter. The name format is as follows:- It must be from 2 to 63 characters long.
- It can only contain lowercase Latin letters, numbers, and hyphens.
- It must start with a letter and cannot end with a hyphen.
-
-
Associate the NAT gateway with the new route table by using 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
folderIdparameter. -
Route table name in the
nameparameter. Follow these naming requirements:- It must be from 2 to 63 characters long.
- 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
networkIdparameter. -
0.0.0.0/0as the destination subnet prefix, in thestaticRoutes[].destinationPrefixparameter. -
NAT gateway ID in the
staticRoutes[].gatewayIdparameter.To get the NAT gateway ID, use the list REST API method for the Gateway resource or the GatewayService/List gRPC API call. In your request, provide the folder ID in the
folderIdparameter.
-
-
Associate the route table with your subnet by using the update REST API method for the Subnet resource or the SubnetService/Update gRPC API call, and provide the following in the request:
-
Subnet ID in the
subnetIdparameter.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
folderIdrequest parameter.To learn how to find out the folder ID, see Getting the folder ID.
-
Route table ID in the
routeTableIdparameter. -
Name of the
routeTableIdparameter in theupdateMaskparameter.
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
updateMaskparameter as a single comma-separated string. -