Assigning a public IP address to a VM
If you created a VM without a public IP address, you can assign it the IP address you reserved in Yandex Virtual Private Cloud or the one automatically selected by Compute Cloud from among available IP addresses. The reserved IP address and the VM must be in the same availability zone.
If a VM has multiple network interfaces, you can assign a public IP address to each one.
- In the management console
, select the folder the VM belongs to. - Select Compute Cloud.
- In the left-hand panel, select
Virtual machines. - Select the VM.
- In the window that opens, under Network, click
in the top-right corner of the relevant network interface section and select Add public IP address. In the window that opens:- In the Public address field, select
Autoto get an IP address automatically orListto choose a reserved address from the list. - Optionally, if you selected
Autoin the Public address field, enable DDoS protection. For more information, see Yandex DDoS Protection in Virtual Private Cloud. - If you selected
Listin the Public address field, choose the IP address you want to assign to your VM. The IP address and the VM must be in the same availability zone. - Click Add.
- In the Public address field, select
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.
To assign a public IP address to a VM, run the following CLI command:
yc compute instance add-one-to-one-nat \
--id=<VM_ID> \
--network-interface-index=<VM_network_interface_number> \
--nat-address=<IP_address>
Where:
-
--id: VM ID. You can get a list of available VM IDs in the folder using theyc compute instance listCLI command. -
--network-interface-index: VM network interface number. The default value is0. To get a list of VM network interfaces and their numbers, runyc compute instance get <VM_ID>. -
--nat-address: Public IP address to assign to the VM. This is an optional setting. If you skip it, the VM will get a public IP address automatically.You can get a list of reserved public IP addresses available in the folder using the
yc vpc address listCLI command. The IP address and the VM must be in the same availability zone.
Here is a possible use case:
yc compute instance add-one-to-one-nat \
--id=fhmsbag62taf******** \
--network-interface-index=0 \
--nat-address=51.250.*.***
Result:
id: fhmsbag62taf********
folder_id: b1gv87ssvu49********
created_at: "2022-05-06T10:41:56Z"
...
network_settings:
type: STANDARD
placement_policy: {}
For more information about the yc compute instance add-one-to-one-nat command, see the CLI reference.
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 public IP address and link it to a VM network interface, use the
yandex_vpc_addressresource and specify the address you get in thenat_ip_addressfield undernetwork_interfacefor the network interface in question in theyandex_compute_instanceresource configuration:# Creating a static IP address resource "yandex_vpc_address" "addr" { name = "vm-adress" external_ipv4_address { zone_id = "<availability_zone>" } } # Creating a VM resource "yandex_compute_instance" "vm-1" { name = "<VM_name>" platform_id = "standard-v3" resources { core_fraction = 20 cores = 2 memory = 1 } ... ## Assigning a subnet and IP address to the VM network interface under `network_interface` network_interface { subnet_id = "<VM_subnet_ID>" nat = true nat_ip_address = yandex_vpc_address.addr.external_ipv4_address[0].address } ... }Where
nat_ip_addressis the public IP address to assign to the VM network interface. Theyandex_vpc_addressresource contains a list of items, where[0]is the list's first item that contains the IP address. If you already have a reserved public IP address to assign to your VM, specify it in thenat_ip_addressfield:nat_ip_address = "<IP_address>"The IP address and the VM must be in the same availability zone.
For more information about
yandex_compute_instanceproperties, see this Terraform article. -
Create the resources:
-
In the terminal, go to the directory where you edited the configuration file.
-
Make sure the configuration file is correct using this command:
terraform validateIf the configuration is correct, you will get this message:
Success! The configuration is valid. -
Run this command:
terraform planYou will see a detailed list of resources. No changes will be made at this step. If the configuration contains any errors, Terraform will show them.
-
Apply the changes:
terraform apply -
Type
yesand press Enter to confirm the changes.
Terraform will create all the required resources. You can check the new resources in the management console
. -
To assign a public IP address to a VM network interface, use the addOneToOneNat REST API method for the Instance resource or the InstanceService/AddOneToOneNat gRPC API call.
Your VM network interface will have a public IP address assigned. You can use this IP address to connect to the VM over SSH.