Routing through a NAT instance
Yandex Cloud lets you configure internet connections for multiple VMs via a NAT instance using static routing. In this case, only one public IP address is used: the one assigned to the VM.
To set up routing through a NAT instance:
- Before you start.
- Create a network, subnet, and test VM.
- Create and configure a NAT instance.
- Set up static routing in the cloud network.
- Test the NAT instance.
If you no longer need the NAT instance, delete it.
Before you start
Before working, you need to register in Yandex Cloud and create a billing account:
- Go to the management console. Then log in to Yandex Cloud or sign up if don't already have an account.
- On the billing page, make sure you linked a billing account, and it has the
TRIAL_ACTIVEstatus. If you don't have a billing account, create one.
If you have an active billing account, you can create or select a folder to run your VM in from the Yandex Cloud page.
Required paid resources
The cost of NAT instance support includes:
- A fee for continuously running VMs (see pricingYandex Compute Cloud).
- A fee for using a dynamic external IP address (see pricing Yandex Virtual Private Cloud).
Create a network, subnet, and test VM
- Create a cloud network with any name, such as
- In the cloud network, create a subnet for your NAT instance with any name, such as
nat-subnet. Don't assign any routing tables to it.
- Create another subnet in the cloud network, such as
- Create a test VM without a public IP and connect it to the
Create a NAT instance
Create a VM to use for internet access.
- Open your folder and click Create resource. Select Virtual machine.
- Enter a name for the VM, for example,
- Select the availability zone where the
public-subnetsubnet is located.
- Under Image/boot disk selection section, click the Cloud Marketplace tab, and select the NAT instance image.
- Under Network settings, choose the required network and subnet and assign a public IP to the NAT instance either automatically or by selecting it from the list.
- In the Access field, enter the login and SSH key to access the VM.
- Click Create VM.
Set up static routing
Set up routing between the NAT instance and test VM.
Create a route table and add to it a static route:
Open the Virtual Private Cloud section in the folder where you want to create a static route.
Click Create route table.
Enter a name for the route table, such as
- The length can be from 3 to 63 characters.
- It may contain lowercase Latin letters, numbers, and hyphens.
- The first character must be a letter. The last character can't be a hyphen.
Click Add route.
In the window that opens, enter the prefix of the destination subnet:
In the Next hop field, specify the internal IP address of the NAT instance. Click Add.
Click Create route table.
To use static routes, link the route table to the subnet where the VMs are located (in the example, it's
private-subnet). To do this:
- On the line with the test VM, click .
- In the menu that opens, select Link route table.
- In the window that opens, select the
nat-instance-routetable from the list.
- Click Link.
You can also use the created route for other subnets in the same network, except for the subnet where the NAT instance is located.
Test the NAT instance
Connect to the NAT instance via
ssh <NAT instance public IP>
Use the NAT instance to connect to the test VM in the same subnet via SSH:
ssh <VM internal IP>
Make sure the VM is connected to the internet via the public IP address of the NAT instance. Enter the following command in the terminal:
If it returns the public IP address of the NAT instance, everything is correct.
Delete the created resources
If you no longer need the NAT instance, delete the