Costs
Nebius AI Cloud charges you for running Compute virtual machines. For more details, see Compute pricing.Prerequisites
- CLI
- Go SDK
- Python SDK
- JavaScript SDK
- Install and configure the Nebius AI Cloud CLI.
- Make sure that you or the service account that you use is in a group that has at least the
editorrole within your tenant or project; for example, the defaulteditorsgroup. You can check this in the Administration → IAM section of the web console.
- Install and initialize the Nebius SDK for Go.
- Make sure that you or the service account that you use is in a group that has at least the
editorrole within your tenant or project; for example, the defaulteditorsgroup. You can check this in the Administration → IAM section of the web console.
- Install and initialize the Nebius SDK for Python.
- Make sure that you or the service account that you use is in a group that has at least the
editorrole within your tenant or project; for example, the defaulteditorsgroup. You can check this in the Administration → IAM section of the web console.
- Install and initialize the Nebius SDK for JavaScript.
- Make sure that you or the service account that you use is in a group that has at least the
editorrole within your tenant or project; for example, the defaulteditorsgroup. You can check this in the Administration → IAM section of the web console.
Steps
Create a gateway virtual machine
The subnet for the gateway VM must be a separate subnet. In later steps, you create a routing table (route table) and assign it to subnets to which the new routing should apply; assigning this routing table to the gateway VM’s subnet leads to routing loops.
- CLI
- Go SDK
- Python SDK
- JavaScript SDK
-
Get the ID of the network that contains the subnets for which you want to set up routing:
Save the network’s ID from the
nebius vpc network list.metadata.idparameter. -
Create a subnet for the gateway VM:
Save the subnet’s ID from the
nebius vpc subnet create --name gateway-subnet --network-id <network_ID>.metadata.idparameter. -
Create a private allocation for the gateway VM:
Save the allocation’s ID from the
nebius vpc allocation create \ --name gateway-allocation \ --ipv4-private-subnet-id <gateway_subnet_ID>.metadata.idparameter. -
Create the gateway VM. It must have a network interface that is located in the created subnet, has the allocated private IP address and a public IP address:
nebius compute instance create \ --name gateway-vm \ --network-interfaces "[{ \"name\": \"eth0\", \"ip_address\": { \"allocation_id\": \"<gateway_allocation_ID>\" }, \"subnet_id\": \"<gateway_subnet_ID>\", \"public_ip_address\": {} }]" \ <other_parameters>For more details about VM parameters, see How to create a virtual machine in Nebius AI Cloud.Full example
VM_BOOT_DISK_ID=$(nebius compute disk create \ --name gateway-boot-disk \ --size-gibibytes 50 \ --type network_ssd \ --source-image-family-image-family ubuntu24.04-cuda13.0 \ --block-size-bytes 4096 \ --format jsonpath='{.metadata.id}') nebius compute instance create \ --name gateway-vm-example \ --resources-platform cpu-d3 \ --resources-preset 4vcpu-16gb \ --boot-disk-existing-disk-id $VM_BOOT_DISK_ID \ --boot-disk-attach-mode read_write \ --boot-disk-device-id boot-disk \ --network-interfaces "[{ \"name\": \"eth0\", \"ip_address\": { \"allocation_id\": \"<gateway_allocation_ID>\" }, \"subnet_id\": \"<gateway_subnet_ID>\", \"public_ip_address\": {} }]" \ --cloud-init-user-data "users: - name: $USER sudo: ALL=(ALL) NOPASSWD:ALL shell: /bin/bash ssh_authorized_keys: - $(cat ~/.ssh/id_ed25519.pub)"
-
Get the ID of the network that contains the subnets for which you want to set up routing. List the networks in the project, with
ParentIdset to the project ID:Save the network’s ID fromnatNetworksService := sdk.Services().VPC().V1(). Network() natNetworksResponse, err := natNetworksService.List( ctx, &vpc.ListNetworksRequest{ ParentId: "<project_ID>", }, ) if err != nil { return err } fmt.Println(natNetworksResponse)Metadata.Id. -
Create a subnet for the gateway VM. Set
Metadata.ParentIdto the project ID andSpec.NetworkIdto the network ID:The code saves the subnet ID togatewaySubnetService := sdk.Services().VPC().V1(). Subnet() gatewaySubnetOperation, err := gatewaySubnetService.Create( ctx, &vpc.CreateSubnetRequest{ Metadata: &common.ResourceMetadata{ Name: "gateway-subnet", ParentId: "<project_ID>", }, Spec: &vpc.SubnetSpec{ NetworkId: "<network_ID>", }, }, ) if err != nil { return err } if _, err = gatewaySubnetOperation.Wait(ctx); err != nil { return err } gatewaySubnetID := gatewaySubnetOperation.ResourceID() fmt.Println(gatewaySubnetOperation)gatewaySubnetID. -
Create a private allocation for the gateway VM. Set
Metadata.ParentIdto the project ID andSubnetIdto the gateway subnet ID:The code saves the allocation ID togatewayIpService := sdk.Services().VPC().V1(). Allocation() gatewayIpOperation, err := gatewayIpService.Create( ctx, &vpc.CreateAllocationRequest{ Metadata: &common.ResourceMetadata{ Name: "gateway-allocation", ParentId: "<project_ID>", }, Spec: &vpc.AllocationSpec{ IpSpec: &vpc.AllocationSpec_Ipv4Private{ Ipv4Private: &vpc.IPv4PrivateAllocationSpec{ Pool: &vpc.IPv4PrivateAllocationSpec_SubnetId{ SubnetId: "<gateway_subnet_ID>", }, }, }, }, }, ) if err != nil { return err } if _, err = gatewayIpOperation.Wait(ctx); err != nil { return err } allocationID := gatewayIpOperation.ResourceID() fmt.Println(gatewayIpOperation)allocationID. -
Create the gateway VM. It must have a network interface that is located in the created subnet, has the allocated private IP address and a public IP address:
In the code, set the following parameters:
gatewayInstanceService := sdk.Services().Compute().V1(). Instance() gatewayInstanceOperation, err := gatewayInstanceService.Create( ctx, &compute.CreateInstanceRequest{ Metadata: &common.ResourceMetadata{ Name: "gateway-vm", ParentId: "<project_ID>", }, Spec: &compute.InstanceSpec{ Resources: &compute.ResourcesSpec{ Platform: "cpu-e2", Size: &compute.ResourcesSpec_Preset{ Preset: "2vcpu-8gb", }, }, BootDisk: &compute.AttachedDiskSpec{ AttachMode: compute.AttachedDiskSpec_READ_WRITE, Type: &compute.AttachedDiskSpec_ExistingDisk{ ExistingDisk: &compute.ExistingDisk{ Id: "<boot_disk_ID>", }, }, DeviceId: "boot-device-id", }, NetworkInterfaces: []*compute.NetworkInterfaceSpec{ &compute.NetworkInterfaceSpec{ Name: "eth0", SubnetId: "<gateway_subnet_ID>", IpAddress: &compute.IPAddress{ AllocationId: "<gateway_allocation_ID>", }, PublicIpAddress: &compute.PublicIPAddress{}, }, }, }, }, ) if err != nil { return err } if _, err = gatewayInstanceOperation.Wait(ctx); err != nil { return err } fmt.Println(gatewayInstanceOperation)Metadata.ParentId: ID of the project.IdunderSpec.BootDisk: ID of the boot disk.SubnetId: ID of the gateway subnet.AllocationId: ID of the gateway allocation.
PublicIpAddressgives the VM a public IP address.Spec.Resources.PlatformandPresetunderSpec.Resources.Sizeare example values; change them if needed.For more details about VM parameters, see How to create a virtual machine in Nebius AI Cloud.Full example
The full example also creates a boot disk and adds a user through cloud-init. Set<username>and<SSH_public_key>to the username and the public SSH key for connecting to the VM.exampleDiskService := sdk.Services().Compute().V1(). Disk() exampleDiskOperation, err := exampleDiskService.Create( ctx, &compute.CreateDiskRequest{ Metadata: &common.ResourceMetadata{ Name: "gateway-boot-disk", ParentId: "<project_ID>", }, Spec: &compute.DiskSpec{ Size: &compute.DiskSpec_SizeGibibytes{ SizeGibibytes: 50, }, BlockSizeBytes: 4096, Type: compute.DiskSpec_NETWORK_SSD, Source: &compute.DiskSpec_SourceImageFamily{ SourceImageFamily: &compute.SourceImageFamily{ ImageFamily: "ubuntu24.04-cuda13.0", }, }, }, }, ) if err != nil { return err } if _, err = exampleDiskOperation.Wait(ctx); err != nil { return err } exampleCloudInit := fmt.Sprintf( "users:\n"+ " - name: %s\n"+ " sudo: ALL=(ALL) NOPASSWD:ALL\n"+ " shell: /bin/bash\n"+ " ssh_authorized_keys:\n"+ " - %s\n", "<username>", "<SSH_public_key>", ) exampleInstanceService := sdk.Services().Compute().V1(). Instance() exampleInstanceOperation, err := exampleInstanceService.Create( ctx, &compute.CreateInstanceRequest{ Metadata: &common.ResourceMetadata{ Name: "gateway-vm-example", ParentId: "<project_ID>", }, Spec: &compute.InstanceSpec{ Resources: &compute.ResourcesSpec{ Platform: "cpu-d3", Size: &compute.ResourcesSpec_Preset{ Preset: "4vcpu-16gb", }, }, BootDisk: &compute.AttachedDiskSpec{ AttachMode: compute.AttachedDiskSpec_READ_WRITE, Type: &compute.AttachedDiskSpec_ExistingDisk{ ExistingDisk: &compute.ExistingDisk{ Id: exampleDiskOperation.ResourceID(), }, }, DeviceId: "boot-disk", }, NetworkInterfaces: []*compute.NetworkInterfaceSpec{ &compute.NetworkInterfaceSpec{ Name: "eth0", SubnetId: "<gateway_subnet_ID>", IpAddress: &compute.IPAddress{ AllocationId: "<gateway_allocation_ID>", }, PublicIpAddress: &compute.PublicIPAddress{}, }, }, CloudInitUserData: exampleCloudInit, }, }, ) if err != nil { return err } if _, err = exampleInstanceOperation.Wait(ctx); err != nil { return err } fmt.Println(exampleInstanceOperation)
-
Get the ID of the network that contains the subnets for which you want to set up routing. List the networks in the project, with
parent_idset to the project ID:Save the network’s ID fromnat_networks_service = NetworkServiceClient(sdk) nat_networks_response = await nat_networks_service.list( ListNetworksRequest( parent_id="<project_ID>", ), ) print(nat_networks_response)metadata.id. -
Create a subnet for the gateway VM. Set
parent_idto the project ID andnetwork_idto the network ID:The code saves the subnet ID togateway_subnet_service = SubnetServiceClient(sdk) gateway_subnet_operation = await gateway_subnet_service.create( CreateSubnetRequest( metadata=ResourceMetadata( name="gateway-subnet", parent_id="<project_ID>", ), spec=SubnetSpec( network_id="<network_ID>", ), ), ) await gateway_subnet_operation.wait() gateway_subnet_id = gateway_subnet_operation.resource_id print(gateway_subnet_operation)gateway_subnet_id. -
Create a private allocation for the gateway VM. Set
parent_idto the project ID andsubnet_idto the gateway subnet ID:The code saves the allocation ID togateway_ip_service = AllocationServiceClient(sdk) gateway_ip_operation = await gateway_ip_service.create( CreateAllocationRequest( metadata=ResourceMetadata( name="gateway-allocation", parent_id="<project_ID>", ), spec=AllocationSpec( ipv4_private=IPv4PrivateAllocationSpec( subnet_id="<gateway_subnet_ID>", ), ), ), ) await gateway_ip_operation.wait() allocation_id = gateway_ip_operation.resource_id print(gateway_ip_operation)allocation_id. -
Create the gateway VM. It must have a network interface that is located in the created subnet, has the allocated private IP address and a public IP address:
In the code, set the following parameters:
gateway_instance_service = InstanceServiceClient(sdk) gateway_instance_operation = await gateway_instance_service.create( CreateInstanceRequest( metadata=ResourceMetadata( name="gateway-vm", parent_id="<project_ID>", ), spec=InstanceSpec( resources=ResourcesSpec( platform="cpu-e2", preset="2vcpu-8gb", ), boot_disk=AttachedDiskSpec( attach_mode=AttachedDiskSpec.AttachMode.READ_WRITE, existing_disk=ExistingDisk( id="<boot_disk_ID>", ), device_id="boot-device-id", ), network_interfaces=[ NetworkInterfaceSpec( name="eth0", subnet_id="<gateway_subnet_ID>", ip_address=IPAddress( allocation_id="<gateway_allocation_ID>", ), public_ip_address=PublicIPAddress(), ), ], ), ), ) await gateway_instance_operation.wait() print(gateway_instance_operation)parent_id: ID of the project.existing_disk.id: ID of the boot disk.subnet_id: ID of the gateway subnet.allocation_id: ID of the gateway allocation.
public_ip_addressgives the VM a public IP address.resources.platformandresources.presetare example values; change them if needed.For more details about VM parameters, see How to create a virtual machine in Nebius AI Cloud.Full example
The full example also creates a boot disk and adds a user through cloud-init. Set<username>and<SSH_public_key>to the username and the public SSH key for connecting to the VM.example_disk_service = DiskServiceClient(sdk) example_disk_operation = await example_disk_service.create( CreateDiskRequest( metadata=ResourceMetadata( name="gateway-boot-disk", parent_id="<project_ID>", ), spec=DiskSpec( size_gibibytes=50, block_size_bytes=4096, type=DiskSpec.DiskType.NETWORK_SSD, source_image_family=SourceImageFamily( image_family="ubuntu24.04-cuda13.0", ), ), ), ) await example_disk_operation.wait() example_cloud_init = ( "users:\n" " - name: {}\n" " sudo: ALL=(ALL) NOPASSWD:ALL\n" " shell: /bin/bash\n" " ssh_authorized_keys:\n" " - {}\n" ).format("<username>", "<SSH_public_key>") example_instance_service = InstanceServiceClient(sdk) example_instance_operation = await example_instance_service.create( CreateInstanceRequest( metadata=ResourceMetadata( name="gateway-vm-example", parent_id="<project_ID>", ), spec=InstanceSpec( resources=ResourcesSpec( platform="cpu-d3", preset="4vcpu-16gb", ), boot_disk=AttachedDiskSpec( attach_mode=AttachedDiskSpec.AttachMode.READ_WRITE, existing_disk=ExistingDisk( id=example_disk_operation.resource_id, ), device_id="boot-disk", ), network_interfaces=[ NetworkInterfaceSpec( name="eth0", subnet_id="<gateway_subnet_ID>", ip_address=IPAddress( allocation_id="<gateway_allocation_ID>", ), public_ip_address=PublicIPAddress(), ), ], cloud_init_user_data=example_cloud_init, ), ), ) await example_instance_operation.wait() print(example_instance_operation)
-
Get the ID of the network that contains the subnets for which you want to set up routing. List the networks in the project, with
parentIdset to the project ID:Save the network’s ID fromconst natNetworksService = new NetworkService(sdk); const natNetworksResponse = await natNetworksService.list( ListNetworksRequest.create({ parentId: "<project_ID>", }), ).result; console.log(natNetworksResponse);metadata.id. -
Create a subnet for the gateway VM. Set
parentIdto the project ID andnetworkIdto the network ID:The code saves the subnet ID toconst gatewaySubnetService = new SubnetService(sdk); const gatewaySubnetOperation = await gatewaySubnetService.create( CreateSubnetRequest.create({ metadata: { name: "gateway-subnet", parentId: "<project_ID>", }, spec: { networkId: "<network_ID>", }, }), ).result; await gatewaySubnetOperation.wait(); const gatewaySubnetId = gatewaySubnetOperation.resourceId(); console.log(gatewaySubnetOperation);gatewaySubnetId. -
Create a private allocation for the gateway VM. Set
parentIdto the project ID andsubnetIdto the gateway subnet ID:The code saves the allocation ID toconst gatewayIpService = new AllocationService(sdk); const gatewayIpOperation = await gatewayIpService.create( CreateAllocationRequest.create({ metadata: { name: "gateway-allocation", parentId: "<project_ID>", }, spec: { ipSpec: { "$case": "ipv4Private", "ipv4Private": { pool: { "$case": "subnetId", "subnetId": "<gateway_subnet_ID>", }, }, }, }, }), ).result; await gatewayIpOperation.wait(); const allocationId = gatewayIpOperation.resourceId(); console.log(gatewayIpOperation);allocationId. -
Create the gateway VM. It must have a network interface that is located in the created subnet, has the allocated private IP address and a public IP address:
In the code, set the following parameters:
const gatewayInstanceService = new InstanceService(sdk); const gatewayInstanceOperation = await gatewayInstanceService.create( CreateInstanceRequest.create({ metadata: { name: "gateway-vm", parentId: "<project_ID>", }, spec: { resources: { platform: "cpu-e2", size: { "$case": "preset", "preset": "2vcpu-8gb", }, }, bootDisk: { attachMode: AttachedDiskSpec_AttachMode.READ_WRITE, type: { "$case": "existingDisk", "existingDisk": { id: "<boot_disk_ID>", }, }, deviceId: "boot-device-id", }, networkInterfaces: [ { name: "eth0", subnetId: "<gateway_subnet_ID>", ipAddress: { allocationId: "<gateway_allocation_ID>", }, publicIpAddress: {}, }, ], }, }), ).result; await gatewayInstanceOperation.wait(); console.log(gatewayInstanceOperation);parentId: ID of the project.existingDisk.id: ID of the boot disk.subnetId: ID of the gateway subnet.allocationId: ID of the gateway allocation.
publicIpAddressgives the VM a public IP address.resources.platformandpresetunderresources.sizeare example values; change them if needed.For more details about VM parameters, see How to create a virtual machine in Nebius AI Cloud.Full example
The full example also creates a boot disk and adds a user through cloud-init. Set<username>and<SSH_public_key>to the username and the public SSH key for connecting to the VM.const exampleDiskService = new DiskService(sdk); const exampleDiskOperation = await exampleDiskService.create( CreateDiskRequest.create({ metadata: { name: "gateway-boot-disk", parentId: "<project_ID>", }, spec: { size: { "$case": "sizeGibibytes", "sizeGibibytes": 50, }, blockSizeBytes: 4096, type: DiskSpec_DiskType.NETWORK_SSD, source: { "$case": "sourceImageFamily", "sourceImageFamily": { imageFamily: "ubuntu24.04-cuda13.0", }, }, }, }), ).result; await exampleDiskOperation.wait(); const exampleCloudInit = [ "users:", " - name: " + "<username>", " sudo: ALL=(ALL) NOPASSWD:ALL", " shell: /bin/bash", " ssh_authorized_keys:", " - " + "<SSH_public_key>", ].join("\n"); const exampleInstanceService = new InstanceService(sdk); const exampleInstanceOperation = await exampleInstanceService.create( CreateInstanceRequest.create({ metadata: { name: "gateway-vm-example", parentId: "<project_ID>", }, spec: { resources: { platform: "cpu-d3", size: { "$case": "preset", "preset": "4vcpu-16gb", }, }, bootDisk: { attachMode: AttachedDiskSpec_AttachMode.READ_WRITE, type: { "$case": "existingDisk", "existingDisk": { id: exampleDiskOperation.resourceId(), }, }, deviceId: "boot-disk", }, networkInterfaces: [ { name: "eth0", subnetId: "<gateway_subnet_ID>", ipAddress: { allocationId: "<gateway_allocation_ID>", }, publicIpAddress: {}, }, ], cloudInitUserData: exampleCloudInit, }, }), ).result; await exampleInstanceOperation.wait(); console.log(exampleInstanceOperation);
Set up the gateway VM
-
Connect to the gateway VM by using SSH:
For more details, see How to connect to virtual machines in Nebius AI Cloud.
ssh <username>@<public_IP_address> -
Enable IPv4 routing and make this change persistent:
sudo sysctl -w net.ipv4.ip_forward=1 sudo sed -i -E 's/^(#\s?)?net.ipv4.ip_forward.*/net.ipv4.ip_forward=1/' /etc/sysctl.conf -
Enable egress NAT and make this change persistent:
The latter command asks you whether to save your current
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE sudo apt-get install iptables-persistentiptablesIPv4 and IPv6 rules to files. Answer “yes” to both. -
Disconnect from the gateway VM:
exit
Set up routing
- CLI
- Go SDK
- Python SDK
- JavaScript SDK
-
Create a routing table in the network:
Save the routing table’s ID from the
nebius vpc route-table create \ --name gateway-table \ --network-id <network_ID>.metadata.idparameter. -
Create a route in the routing table to send egress traffic to the gateway private allocation:
nebius vpc route create \ --name route-to-allocation \ --parent-id <routing_table_ID> \ --destination-cidr 0.0.0.0/0 \ --next-hop-allocation-id <gateway_allocation_ID> -
Assign the routing table to the subnets that should use the NAT gateway:
Repeat for each subnet as needed. Do not assign the routing table to the subnet of the gateway VM (
nebius vpc subnet update <subnet_ID> \ --route-table-id <routing_table_ID>gateway-subnet).
-
Create a routing table in the network. Set
Metadata.ParentIdto the project ID andSpec.NetworkIdto the network ID:The code saves the routing table ID togatewayTableService := sdk.Services().VPC().V1(). RouteTable() gatewayTableOperation, err := gatewayTableService.Create( ctx, &vpc.CreateRouteTableRequest{ Metadata: &common.ResourceMetadata{ Name: "gateway-table", ParentId: "<project_ID>", }, Spec: &vpc.RouteTableSpec{ NetworkId: "<network_ID>", }, }, ) if err != nil { return err } if _, err = gatewayTableOperation.Wait(ctx); err != nil { return err } routingTableID := gatewayTableOperation.ResourceID() fmt.Println(gatewayTableOperation)routingTableID. -
Create a route in the routing table to send egress traffic to the gateway private allocation. Set
Metadata.ParentIdto the routing table ID andIdunderSpec.NextHop.Allocationto the gateway allocation ID:gatewayRouteService := sdk.Services().VPC().V1(). Route() gatewayRouteOperation, err := gatewayRouteService.Create( ctx, &vpc.CreateRouteRequest{ Metadata: &common.ResourceMetadata{ Name: "route-to-allocation", ParentId: "<routing_table_ID>", }, Spec: &vpc.RouteSpec{ Destination: &vpc.DestinationMatch{ Cidr: "0.0.0.0/0", }, NextHop: &vpc.NextHop{ NextHop: &vpc.NextHop_Allocation{ Allocation: &vpc.AllocationNextHop{ Id: "<gateway_allocation_ID>", }, }, }, }, }, ) if err != nil { return err } if _, err = gatewayRouteOperation.Wait(ctx); err != nil { return err } fmt.Println(gatewayRouteOperation) -
Assign the routing table to the subnets that should use the NAT gateway. Set
Idto the subnet ID andSpec.RouteTableIdto the routing table ID:Repeat for each subnet as needed. Do not assign the routing table to the subnet of the gateway VM (clientSubnetService := sdk.Services().VPC().V1(). Subnet() clientSubnet, err := clientSubnetService.Get( ctx, &vpc.GetSubnetRequest{ Id: "<subnet_ID>", }, ) if err != nil { return err } clientSubnet.Spec.RouteTableId = "<routing_table_ID>" clientSubnetOperation, err := clientSubnetService.Update( ctx, &vpc.UpdateSubnetRequest{ Metadata: clientSubnet.Metadata, Spec: clientSubnet.Spec, }, ) if err != nil { return err } if _, err = clientSubnetOperation.Wait(ctx); err != nil { return err }gateway-subnet).
-
Create a routing table in the network. Set
parent_idto the project ID andnetwork_idto the network ID:The code saves the routing table ID togateway_table_service = RouteTableServiceClient(sdk) gateway_table_operation = await gateway_table_service.create( CreateRouteTableRequest( metadata=ResourceMetadata( name="gateway-table", parent_id="<project_ID>", ), spec=RouteTableSpec( network_id="<network_ID>", ), ), ) await gateway_table_operation.wait() routing_table_id = gateway_table_operation.resource_id print(gateway_table_operation)routing_table_id. -
Create a route in the routing table to send egress traffic to the gateway private allocation. Set
parent_idto the routing table ID andnext_hop.allocation.idto the gateway allocation ID:gateway_route_service = RouteServiceClient(sdk) gateway_route_operation = await gateway_route_service.create( CreateRouteRequest( metadata=ResourceMetadata( name="route-to-allocation", parent_id="<routing_table_ID>", ), spec=RouteSpec( destination=DestinationMatch( cidr="0.0.0.0/0", ), next_hop=NextHop( allocation=AllocationNextHop( id="<gateway_allocation_ID>", ), ), ), ), ) await gateway_route_operation.wait() print(gateway_route_operation) -
Assign the routing table to the subnets that should use the NAT gateway. Set
idto the subnet ID andspec.route_table_idto the routing table ID:Repeat for each subnet as needed. Do not assign the routing table to the subnet of the gateway VM (client_subnet_service = SubnetServiceClient(sdk) client_subnet = await client_subnet_service.get( GetSubnetRequest( id="<subnet_ID>", ), ) client_subnet.spec.route_table_id = "<routing_table_ID>" client_subnet_operation = await client_subnet_service.update( UpdateSubnetRequest( metadata=client_subnet.metadata, spec=client_subnet.spec, ), ) await client_subnet_operation.wait()gateway-subnet).
-
Create a routing table in the network. Set
parentIdto the project ID andnetworkIdto the network ID:The code saves the routing table ID toconst gatewayTableService = new RouteTableService(sdk); const gatewayTableOperation = await gatewayTableService.create( CreateRouteTableRequest.create({ metadata: { name: "gateway-table", parentId: "<project_ID>", }, spec: { networkId: "<network_ID>", }, }), ).result; await gatewayTableOperation.wait(); const routingTableId = gatewayTableOperation.resourceId(); console.log(gatewayTableOperation);routingTableId. -
Create a route in the routing table to send egress traffic to the gateway private allocation. Set
parentIdto the routing table ID andidundernextHop.allocationto the gateway allocation ID:const gatewayRouteService = new RouteService(sdk); const gatewayRouteOperation = await gatewayRouteService.create( CreateRouteRequest.create({ metadata: { name: "route-to-allocation", parentId: "<routing_table_ID>", }, spec: { destination: { cidr: "0.0.0.0/0", }, nextHop: { nextHop: { "$case": "allocation", "allocation": { id: "<gateway_allocation_ID>", }, }, }, }, }), ).result; await gatewayRouteOperation.wait(); console.log(gatewayRouteOperation); -
Assign the routing table to the subnets that should use the NAT gateway. Set
idto the subnet ID andspec.routeTableIdto the routing table ID:Repeat for each subnet as needed. Do not assign the routing table to the subnet of the gateway VM (const clientSubnetService = new SubnetService(sdk); const clientSubnet = await clientSubnetService.get( GetSubnetRequest.create({ id: "<subnet_ID>", }), ).result; clientSubnet.spec.routeTableId = "<routing_table_ID>"; const clientSubnetOperation = await clientSubnetService.update( UpdateSubnetRequest.create({ metadata: clientSubnet.metadata, spec: clientSubnet.spec, }), ).result; await clientSubnetOperation.wait();gateway-subnet).
Test routing
To test that the routing table works, send traffic from a VM in a subnet to which you assigned the routing table (a test VM) and check its source IP address:-
Connect to the test VM by using the gateway VM as the jump host:
ssh -J <username>@<gateway_public_IP_address> <username>@<VM_private_IP_address> -
Verify that egress traffic from the test VM to the internet is routed through the gateway VM, by using a public service that looks up the traffic’s source IP address:
The command should return the public IP address of the gateway VM. If it returns the private address of the test VM instead:
curl ifconfig.me- Check that you assigned the routing table to the subnet: run
nebius vpc route-table get <routing_table_ID>and then find the subnet in the.status.assignment.subnetsparameter. - Check that the test VM belongs to this subnet: run
nebius compute instance get <VM_ID>and then find the subnet in the.spec.network_interfaces[0].subnet_idparameter. - Check that you specified the gateway VM’s private allocation as the next hop in the route: run
nebius vpc route list --parent-id <routing_table_ID>and then find the allocation in the.spec.next_hop.allocation.idparameter.
- Check that you assigned the routing table to the subnet: run