# Assigning a public IP address to a VM

If you created a [VM](../../concepts/vm.md) without a [public IP address](../../../vpc/concepts/address.md#public-addresses), you can assign it the IP address [you reserved](../../../vpc/operations/get-static-ip.md) in [Yandex Virtual Private Cloud](../../../vpc/index.md) 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](../../../overview/concepts/geo-scope.md).

If a VM has multiple [network interfaces](../../concepts/network.md), you can assign a public IP address to each one.

{% list tabs group=instructions %}

- Management console {#console}

  1. In the [management console](https://console.yandex.cloud), select the [folder](../../../resource-manager/concepts/resources-hierarchy.md#folder) the VM belongs to.
  1. Navigate to **Compute Cloud**.
  1. In the left-hand panel, select ![image](../../../_assets/console-icons/server.svg) **Virtual machines**.
  1. Select the VM.
  1. In the window that opens, under **Network**, click ![image](../../../_assets/console-icons/ellipsis.svg) 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 `Auto` to get an IP address automatically or `List` to choose a reserved address from the list.
      * Optionally, if you selected `Auto` in the **Public address** field, enable **DDoS protection**. For more information, see [Yandex DDoS Protection in Virtual Private Cloud](../../../vpc/ddos-protection/index.md).
      * If you selected `List` in 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**.

- CLI {#cli}

  If you do not have the Yandex Cloud CLI yet, [install and initialize it](../../../cli/quickstart.md#install).

  The folder used by default is the one specified when [creating](../../../cli/operations/profile/profile-create.md) the CLI profile. To change the default folder, use the `yc config set folder-id <folder_ID>` command. You can also specify a different folder for any command using `--folder-name` or `--folder-id`. If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.

  To assign a public IP address to a VM, run the following [CLI](../../../cli/index.md) command:

  ```bash
  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](../../../resource-manager/concepts/resources-hierarchy.md#folder) using the `yc compute instance list` [CLI command](../../../cli/cli-ref/compute/cli-ref/instance/list.md).
  * `--network-interface-index`: VM network interface number. The default value is `0`. To get a list of VM network interfaces and their numbers, run `yc 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 list` [CLI command](../../../cli/cli-ref/vpc/cli-ref/address/list.md). The IP address and the VM must be in the same availability zone.

  Here is a possible use case:

  ```bash
  yc compute instance add-one-to-one-nat \
    --id=fhmsbag62taf******** \
    --network-interface-index=0 \
    --nat-address=51.250.*.***
  ```

  Result:

  ```text
  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](../../../cli/cli-ref/compute/cli-ref/instance/add-one-to-one-nat.md).

- Terraform {#tf}

  With [Terraform](https://www.terraform.io/), you can quickly create a cloud infrastructure in Yandex Cloud and manage it using configuration files. These files store the infrastructure description written in HashiCorp Configuration Language (HCL). If you change the configuration files, Terraform automatically detects which part of your configuration is already deployed, and what should be added or removed.
  
  Terraform is distributed under the [Business Source License](https://github.com/hashicorp/terraform/blob/main/LICENSE). The [Yandex Cloud provider for Terraform](https://github.com/yandex-cloud/terraform-provider-yandex) is distributed under the [MPL-2.0](https://www.mozilla.org/en-US/MPL/2.0/) license.
  
  For more information about the provider resources, see the relevant documentation on the [Terraform](https://www.terraform.io/docs/providers/yandex/index.html) website or [its mirror](../../../terraform/index.md).

  If you do not have Terraform yet, [install it and configure the Yandex Cloud provider](../../../tutorials/infrastructure-management/terraform-quickstart.md#install-terraform).
  
  
  To manage infrastructure using Terraform under a service account or user accounts (a Yandex account, a federated account, or a local user), [authenticate](../../../terraform/authentication.md) using the appropriate method.

  1. To create a public IP address and link it to a VM network interface, use the `yandex_vpc_address` resource and specify the address you get in the `nat_ip_address` field under `network_interface` for the network interface in question in the `yandex_compute_instance` resource configuration:

     ```hcl
     # 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_address` is the public IP address to assign to the VM network interface. The `yandex_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 the `nat_ip_address` field:

     ```hcl
     nat_ip_address = "<IP_address>"
     ```

     The IP address and the VM must be in the same availability zone.

     For more information about `yandex_compute_instance` properties, see [this provider guide](../../../terraform/resources/compute_instance.md).
  1. Create the resources:

     1. In the terminal, navigate to the configuration file directory.
     1. Make sure the configuration is correct using this command:
     
        ```bash
        terraform validate
        ```
     
        If the configuration is valid, you will get this message:
     
        ```bash
        Success! The configuration is valid.
        ```
     
     1. Run this command:
     
        ```bash
        terraform plan
        ```
     
        You will see a list of resources and their properties. No changes will be made at this step. Terraform will show any errors in the configuration.
     1. Apply the configuration changes:
     
        ```bash
        terraform apply
        ```
     
     1. Type `yes` and press **Enter** to confirm the changes.

     Terraform will create all the required resources. You can check the new resources in the [management console](https://console.yandex.cloud).

- API {#api}

  To assign a public IP address to a VM network interface, use the [addOneToOneNat](../../api-ref/Instance/addOneToOneNat.md) REST API method for the [Instance](../../api-ref/Instance/index.md) resource or the [InstanceService/AddOneToOneNat](../../api-ref/grpc/Instance/addOneToOneNat.md) gRPC API call.

{% endlist %}

Your VM network interface will have a public IP address assigned. You can use this IP address to [connect](../vm-connect/ssh.md#vm-connect) to the VM over SSH.

#### See also {#see-also}

* [Making a VM public IP address static](vm-set-static-ip.md)
* [Converting a dynamic public IP address to static](../../../vpc/operations/set-static-ip.md)
* [Adding another network interface to a virtual machine](attach-network-interface.md)