[Yandex Cloud documentation](../../index.md) > [Yandex Virtual Private Cloud](../index.md) > [Step-by-step guides](index.md) > Subnet > Creating a subnet

# Creating a subnet


[Subnets](../concepts/network.md#subnet) are created in cloud networks. A new subnet is located in one of the availability zones. You can connect resources to a subnet if they reside in the same availability zone as the subnet itself.

{% list tabs group=instructions %}

- Management console {#console}

  1. In the [management console](https://console.yandex.cloud), select the folder where you need to create a subnet.
  1. Navigate to **Virtual Private Cloud**.
  1. In the left-hand panel, select ![subnets](../../_assets/console-icons/nodes-right.svg) **Subnets**.
  1. At the top right, click **Create**.
  1. In the **Name** field, specify the subnet name. The naming requirements are as follows:

     * Length: between 3 and 63 characters.
     * It can only contain lowercase Latin letters, numbers, and hyphens.
     * It must start with a letter and cannot end with a hyphen.

  1. (Optional) In the **Description** field, add a description.
  1. In the **Availability zone** field, select an availability zone from the drop-down list.
  1. In the **Network** field, specify a cloud network. Make sure to create it in advance.
  1. In the **CIDR** field, enter the subnet IP address and mask. 
     For more information about subnet IP address ranges, see [Cloud networks and subnets](../concepts/network.md). 
     If you need to enter more CIDRs, click **Add**.
  1. Optionally, set **DHCP settings**. Follow these steps:
      1. In the **Domain name** field, specify a DNS domain to search for unqualified names.
      1. In the **Domain name servers** field, click **Add** and enter the address of your DNS server. You can specify multiple DNS servers.
      1. In the **NTP servers** field, click **Add** and enter the address of your NTP server. You can specify multiple NTP servers.

  1. Click **Create subnet**.

- 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 create a subnet:

  1. View the description of the CLI command for creating a subnet:

      ```
      yc vpc subnet create --help
      ```

  1. Get a list of cloud networks in the required folder:

      ```
      yc vpc network list --folder-id b1g6ci08ma55********
      ```

      Result:

      ```
      +----------------------+----------------+
      |          ID          |      NAME      |
      +----------------------+----------------+
      | enpavfmgapum******** | test-network-1 |
      | enplom7a98s1******** | default        |
      +----------------------+----------------+
      ```

  1. Select `NAME` or `ID` of the cloud network you need. Create a subnet in the default folder:

      ```
      yc vpc subnet create \
        --name test-subnet-1 \
        --description "My test subnet" \
        --network-id enplom7a98s1******** \
        --zone ru-central1-a \
        --range 192.168.0.0/24
      ```

      Where:

      * `--network-id`: Cloud network ID. You can also select a cloud network by specifying its name via the `--network-name` parameter. Specify the name of the cloud network to create the subnet in and the CIDR.
      * `--zone`: Subnet availability zone. If this parameter is not set, the subnet is created in the default availability zone.
      * `--range`: List of internal IPv4 addresses specified for this subnet, such as `10.0.0.0/22` or `192.168.0.0/16`. Make sure the addresses are unique within the network. The minimum subnet size is /28, and the maximum subnet size is /16. Only IPv4 is supported.

      The subnet naming requirements are as follows:

      * Length: between 3 and 63 characters.
      * It can only contain lowercase Latin letters, numbers, and hyphens.
      * It must start with a letter and cannot end with a hyphen.

      ```
      yc vpc subnet create \
        --name test-subnet-1 \
        --description "My test subnet" \
        --network-name test-network-1 \
        --zone ru-central1-a \
        --range 192.168.0.0/24
      ```

      The `--name` and `--description` parameters are optional: you can create a subnet without any name or description and access it by ID.

  1. Get a list of all subnets in the default folder:

      ```
      yc vpc subnet list
      ```

      Result:

      ```
      +----------------------+-----------------------+------------------------+
      |          ID          |         NAME          | ... |       RANGE      |
      +----------------------+-----------------------+------------------------+
      ...
      | e2l0psbfoloe******** | test-subnet-1         | ... | [192.168.0.0/24] |
      ...
      +----------------------+-----------------------+-----+------------------+
      ```

      Get the same list with more details in YAML format:

      ```
      yc vpc subnet list --format yaml
      ```

      Result:
      
      ```
      ...

      - id: e2l0psbfoloe********
        folder_id: b1g6ci08ma55********
        created_at: "2018-10-24T12:25:58Z"
        name: test-subnet-1
        description: My test subnet
        network_id: enplom7a98s1********
        zone_id: ru-central1-a
        v4_cidr_blocks:
        - 192.168.0.0/24

      ...
      ```

- 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. In the configuration file, describe the subnet parameters:

     * `name`: Subnet name. The naming requirements are as follows:

        * Length: between 3 and 63 characters.
        * It can only contain lowercase Latin letters, numbers, and hyphens.
        * It must start with a letter and cannot end with a hyphen.

     * `description`: Subnet description.
     * `v4_cidr_blocks`: List of IPv4 addresses for incoming or outgoing traffic, e.g., `10.0.0.0/22` or `192.168.0.0/16`. Make sure the addresses are unique within the network. The minimum subnet size is `/28`, while the maximum subnet size is `/16`. Only IPv4 is supported.
     * `zone`: [Availability zone](../../overview/concepts/geo-scope.md).
     * `network_id`: ID of the network you are creating the subnet in.

     Here is an example of the configuration file structure:

     ```hcl
     resource "yandex_vpc_subnet" "lab-subnet-a" {
       name           = "<subnet_name>"
	   description    = "<subnet_description>"
       v4_cidr_blocks = ["<IPv4_address>"]
       zone           = "<availability_zone>"
       network_id     = "<network_ID>"
     }
     ```

     To add, update, or delete a subnet, use the `yandex_vpc_subnet` resource indicating the network in the `network_id` field (see [this example](#examples)).

     For more information about the `yandex_vpc_subnet` properties in Terraform, see [this provider guide](../../terraform/resources/vpc_subnet.md).

  1. Make sure the configuration files are correct.

     1. In the terminal, navigate to the directory where you created your configuration file.
     1. Run a check using this command:

        ```
        terraform plan
        ```

     If the configuration is correct, the terminal will display a list of the resources and their settings. Otherwise, Terraform will show any detected errors. 

  1. Deploy the cloud resources.

     1. If the configuration is correct, run this command:

        ```
        terraform apply
        ```

     1. Confirm creating the resources by typing `yes` and pressing **Enter**.

        This will create all the resources you need in the specified folder. You can check the new resources and their settings using the [management console](https://console.yandex.cloud) or these [CLI](../../cli/quickstart.md) commands:

        ```
        yc vpc subnet list
        ```

- API {#api}

  To create a subnet, use the [create](../api-ref/Subnet/create.md) REST API method for the [Subnet](../api-ref/Subnet/index.md) resource or the [SubnetService/Create](../api-ref/grpc/Subnet/create.md) gRPC API call, and provide the following in the request:

    * ID of the folder the subnet will reside in, in the `folderId` parameter.
    * ID of the network the subnet will be placed in, in the `networkId` parameter.
    * ID of the availability zone the subnet will be placed in, in the `zoneId` parameter.
    * List of internal IPv4 addresses specified for this subnet, in the `v4CidrBlocks[]` array, e.g., `10.0.0.0/22` or `192.168.0.0/16`. Make sure the addresses are unique within the network. The minimum subnet size is `/28`, while the maximum subnet size is `/16`. Only IPv4 is supported.

  To get the subnet ID, use the [list](../api-ref/Subnet/list.md) REST API method for the [Subnet](../api-ref/Subnet/index.md) resource or the [SubnetService/List](../api-ref/grpc/Subnet/list.md) gRPC API call and provide the folder ID in the `folderId` request parameter.

  To learn how to find out the folder ID, see [Getting the folder ID](../../resource-manager/operations/folder/get-id.md).

{% endlist %}

## Examples {#examples}

{% list tabs group=instructions %}

- CLI {#cli}

  Create a subnet with a name and description in the selected folder:

    ```
    yc vpc subnet create \
      --name test-subnet-1 \
      --description "My test subnet" \
      --folder-id b1g6ci08ma55******** \
      --network-id enplom7a98s1******** \
      --zone ru-central1-a \
      --range 192.168.0.0/24
    ```

    Create a subnet with DHCP settings:
    ```
    yc vpc subnet create \
      --name test-subnet-1 \
      --description "My test subnet" \
      --folder-id b1g6ci08ma55******** \
      --network-id enplom7a98s1******** \
      --zone ru-central1-a \
      --range 192.168.0.0/24 \
      --domain-name test.domain \
      --domain-name-server 192.168.0.100 \
      --ntp-server 192.168.0.101
    ```

- Terraform {#tf}

  1. Specify the `yandex_vpc_subnet` properties in the configuration file:

     ```hcl
     resource "yandex_vpc_network" "lab-net" {
       name        = "network-1"
       description = "My first network"
     }

     resource "yandex_vpc_subnet" "lab-subnet-a" {
       name           = "subnet-1"
       description    = "My first subnet"
       v4_cidr_blocks = ["10.2.0.0/16"]
       zone           = "ru-central1-a"
       network_id     = "${yandex_vpc_network.lab-net.id}"
     }
     ```

     For more information about resource properties in Terraform, see the [relevant Terraform documentation](../../terraform/resources/vpc_subnet.md).

  1. Make sure the configuration files are correct.

     1. In the terminal, navigate to the directory where you created your configuration file.
     1. Run a check using this command:

        ```
        terraform plan
        ```

     If the configuration is correct, the terminal will display a list of the resources and their settings. Otherwise, Terraform will show any detected errors. 

  1. Deploy the cloud resources.

     1. If the configuration is correct, run this command:

        ```
        terraform apply
        ```

     1. Confirm creating the resources by typing `yes` and pressing **Enter**.

        This will create all the resources you need in the specified folder. You can check the new resources and their settings using the [management console](https://console.yandex.cloud) or these [CLI](../../cli/quickstart.md) commands:

        ```
        yc vpc subnet list
        ```

{% endlist %}