Creating a subnet
Subnets are created in cloud networks. A new subnet is located in one of the availability zones. Resources can be connected to a subnet if they reside in the same availability zone as the subnet itself.
-
In the management console
, go to the folder where you need to create a subnet. -
In the list of services, select Virtual Private Cloud.
-
In the left-hand panel, select
Subnets. -
At the top right, click Create.
-
In the Name field, specify the subnet name. The naming requirements are as follows:
- The name must be from 3 to 63 characters long.
- It may contain lowercase Latin letters, numbers, and hyphens.
- The first character must be a letter and the last character cannot be a hyphen.
-
(Optional) In the Description field, add a description.
-
In the Zone field, select an availability zone from the drop-down list.
-
In the Network field, specify a cloud network. Make sure to create it in advance.
-
In the CIDR field, enter the subnet IP address and mask.
For more information about subnet IP address ranges, see Cloud networks and subnets.
If you need to enter more CIDRs, click Add CIDR. -
(Optional) Set DHCP settings. To do this:
- In the Domain name field, specify a DNS domain to search for unqualified names.
- In the Domain name servers field, click Add domain name server and enter the address of your DNS server. You can specify multiple DNS servers.
- In the NTP servers field, click Add NTP server and enter the address of your NTP server. You can specify multiple NTP servers.
-
Click Create subnet.
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 create a subnet:
-
See the description of the CLI command for creating a subnet:
yc vpc subnet create --help
-
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 | +----------------------+----------------+
-
Select the
NAME
orID
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
flag. Specify the name of the cloud network to create the subnet in and the CIDR.--zone
: Availability zone where the subnet is created. If this flag is not set, the subnet is created in the default availability zone.--range
: List of internal IPv4 addresses defined for this subnet, e.g.,10.0.0.0/22
or192.168.0.0/16
. Make sure the addresses are unique within the network. The minimum subnet size is /28, the maximum subnet size is /16. Only IPv4 is supported.
The subnet naming requirements are as follows:
- The name must be from 3 to 63 characters long.
- It may contain lowercase Latin letters, numbers, and hyphens.
- The first character must be a letter and the last character cannot be 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
flags are optional: you can create a subnet without any name or description and access it by ID. -
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
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.
-
In the configuration file, describe the subnet parameters:
-
name
: Subnet name. The naming requirements are as follows:- The name must be from 3 to 63 characters long.
- It may contain lowercase Latin letters, numbers, and hyphens.
- The first character must be a letter and the last character cannot be a hyphen.
-
description
: Description of the subnet. -
v4_cidr_blocks
: List of IPv4 addresses to deal with outgoing and incoming traffic, e.g.,10.0.0.0/22
or192.168.0.0/16
. Make sure the addresses are unique within the network. The minimum subnet size is/28
, the maximum subnet size is/16
. Only IPv4 is supported. -
zone
: Availability zone. -
network_id
: ID of the network where the subnet is created.
Here is an example of the configuration file structure:
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 with the network specified in thenetwork_id
field (see an example).For more information about the parameters of the
yandex_vpc_subnet
resource in Terraform, see the provider documentation . -
-
Make sure the configuration files are correct.
-
In the command line, go to the directory where you created the configuration file.
-
Run a check using this command:
terraform plan
If the configuration is described correctly, the terminal will display a list of created resources and their parameters. If the configuration contains any errors, Terraform will point them out.
-
-
Deploy cloud resources.
-
If the configuration does not contain any errors, run this command:
terraform apply
-
Confirm creating the resources: type
yes
in the terminal and press Enter.All the resources you need will then be created in the specified folder. You can check the new resources and their configuration using the management console
or these CLI commands:yc vpc subnet list
-
To create a subnet, use the create REST API method for the Subnet resource or the SubnetService/Create gRPC API call, and provide the following in the request:
- ID of the folder where the subnet will be placed, in the
folderId
parameter. - ID of the network where the subnet will be placed, in the
networkId
parameter. - ID of the availability zone where the subnet will be placed, in the
zoneId
parameter. - List of internal IPv4 addresses defined for this subnet, in the
v4CidrBlocks[]
array, e.g.,10.0.0.0/22
or192.168.0.0/16
. Make sure the addresses are unique within the network. The minimum subnet size is/28
, the maximum subnet size is/16
. Only IPv4 is supported.
To get the subnet ID, use the list REST API method for the Subnet resource or the SubnetService/List 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.
Examples
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
-
Describe the properties of the
yandex_vpc_subnet
resource in a configuration file: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 parameters in Terraform, see the provider documentation
. -
Make sure the configuration files are correct.
-
In the command line, go to the directory where you created the configuration file.
-
Run a check using this command:
terraform plan
If the configuration is described correctly, the terminal will display a list of created resources and their parameters. If the configuration contains any errors, Terraform will point them out.
-
-
Deploy cloud resources.
-
If the configuration does not contain any errors, run this command:
terraform apply
-
Confirm creating the resources: type
yes
in the terminal and press Enter.All the resources you need will then be created in the specified folder. You can check the new resources and their configuration using the management console
or these CLI commands:yc vpc subnet list
-