Associating a public IP with a virtual machine
If you created a VM without a public IP, you can associate it with an IP you reserved in Yandex Virtual Private Cloud or the one automatically selected by Compute Cloud from among available IPs. The reserved IP address and the VM must be in the same availability zone.
If a VM has multiple network interfaces, you can associate a public IP address with 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 appropriate 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:- Under Public address, select
Auto
to get an IP automatically orList
to choose a reserved address from the list. - (Optional) If you selected
Auto
under Public address, enable the DDoS protection option. For more information, see Yandex DDoS Protection in Virtual Private Cloud. - If you selected
List
in the Public address field, select the IP address you want to associate with your VM. The IP address and the VM must be in the same availability zone. - Click Add.
- Under Public address, select
If you do not have the Yandex Cloud command line interface yet, install and initialize it.
The folder specified in the CLI profile is used by default. You can specify a different folder using the --folder-name
or --folder-id
parameter.
To associate a public IP address with a VM, run the following CLI command:
yc compute instance add-one-to-one-nat \
--id=<VM_ID> \
--network-interface-index=<VM's_network_interface_number> \
--nat-address=<IP_address>
Where:
-
--id
: VM ID. You can get a list of VM IDs in a folder using theyc compute instance list
CLI command. -
--network-interface-index
: VM's network interface number. The default value is0
. To get a list of VM's network interfaces and their numbers, run theyc compute instance get <VM_ID>
command. -
--nat-address
: Public IP address to assign to the VM. This is an optional parameter. If you do not specify the--nat-address
parameter, a public IP address will be assigned to your VM automatically.You can get a list of reserved public IP addresses in a folder using the
yc vpc address list
CLI command. The IP address and the VM must be in the same availability zone.
Usage example:
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.
Terraform
For more information about the provider resources, see the documentation on the Terraform
If you change the configuration files, Terraform automatically detects which part of your configuration is already deployed, and what should be added or removed.
If you don't have Terraform, install it and configure the Yandex Cloud provider.
-
To create a public IP address and associate it with a VM's network interface, use the
yandex_vpc_address
resource and specify the address you get in thenat_ip_address
field undernetwork_interface
for the network interface in question in theyandex_compute_instance
resource 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's network interface in the network_interface section. network_interface { subnet_id = "<VM_subnet_ID>" nat = true nat_ip_address = yandex_vpc_address.addr.external_ipv4_address[0].address } ... }
Where
nat_ip_address
is the public IP address to assign to the VM's network interface. Theyandex_vpc_address
resource 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_address
field:nat_ip_address = "<IP_address>"
The IP address and the VM must be in the same availability zone.
For more information about the
yandex_compute_instance
resource parameters, see the provider documentation . -
Create resources:
-
In the terminal, change to the folder where you edited the configuration file.
-
Make sure the configuration file is correct using the command:
terraform validate
If the configuration is correct, the following message is returned:
Success! The configuration is valid.
-
Run the command:
terraform plan
The terminal will display a list of resources with parameters. No changes are made at this step. If the configuration contains errors, Terraform will point them out.
-
Apply the configuration changes:
terraform apply
-
Confirm the changes: type
yes
in the terminal and press Enter.
Terraform will create all the required resources. You can check the new resources using the management console
. -
To associate a public IP address with a VM's network interface, use the addOneToOneNat REST API method for the Instance resource or the InstanceService/AddOneToOneNat gRPC API call.
Your VM's network interface will have a public IP address assigned. You can use this IP address to connect to the VM via SSH.