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.
{% list tabs group=instructions %}
-
Management console {#console}
-
In the [management console]({{ link-console-main }}), go to the folder where you need to create a subnet.
-
In the list of services, select {{ ui-key.yacloud.iam.folder.dashboard.label_vpc }}.
-
In the left-hand panel, select {{ ui-key.yacloud.vpc.switch_networks }}.
-
At the top right, click {{ ui-key.yacloud.common.create }}.
-
In the {{ ui-key.yacloud.vpc.subnetworks.create.field_name }} field, specify the subnet name. The naming requirements are as follows:
{% include name-format %}
-
(Optional) In the {{ ui-key.yacloud.vpc.subnetworks.create.field_description }} field, add a description.
-
In the {{ ui-key.yacloud.vpc.subnetworks.create.field_zone }} field, select an availability zone from the drop-down list.
-
In the {{ ui-key.yacloud.vpc.subnetworks.create.field_network }} field, specify a cloud network. Make sure to create it in advance.
-
In the {{ ui-key.yacloud.vpc.subnetworks.create.field_ip }} 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 {{ ui-key.yacloud.vpc.subnetworks.create.button_add-cidr }}.
-
(Optional) Set {{ ui-key.yacloud.vpc.subnetworks.create.section_dhcp-options }}. To do this:
- In the {{ ui-key.yacloud.vpc.subnetworks.create.field_domain-name }} field, specify a DNS domain to search for unqualified names.
- In the {{ ui-key.yacloud.vpc.subnetworks.create.field_domain-name-servers }} field, click {{ ui-key.yacloud.vpc.subnetworks.create.button_add-domain-name-server }} and enter the address of your DNS server. You can specify multiple DNS servers.
- In the {{ ui-key.yacloud.vpc.subnetworks.create.field_ntp-servers }} field, click {{ ui-key.yacloud.vpc.subnetworks.create.button_add-ntp-server }} and enter the address of your NTP server. You can specify multiple NTP servers.
-
Click {{ ui-key.yacloud.vpc.subnetworks.create.button_create }}.
-
-
CLI {#cli}
{% include include %}
{% include default-catalogue %}
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 {{ region-id }}-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:
{% include name-format %}
yc vpc subnet create \ --name test-subnet-1 \ --description "My test subnet" \ --network-name test-network-1 \ --zone {{ region-id }}-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: {{ region-id }}-a v4_cidr_blocks: - 192.168.0.0/24 ...
-
-
{{ TF }} {#tf}
{% include terraform-definition %}
{% include terraform-install %}
-
In the configuration file, describe the subnet parameters:
-
name
: Subnet name. The naming requirements are as follows:{% include name-format %}
-
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 {{ TF }}, see the [provider documentation]({{ tf-provider-resources-link }}/vpc_subnet). -
-
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, {{ TF }} 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]({{ link-console-main }}) or these CLI commands:
yc vpc subnet list
-
-
-
API {#api}
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.
{% include get-subnet-id %}
{% include get-catalog-id %}
- ID of the folder where the subnet will be placed, in the
{% endlist %}
{% 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 {{ region-id }}-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 {{ region-id }}-a \ --range 192.168.0.0/24 \ --domain-name test.domain \ --domain-name-server 192.168.0.100 \ --ntp-server 192.168.0.101
-
{{ TF }} {#tf}
-
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 = "{{ region-id }}-a" network_id = "${yandex_vpc_network.lab-net.id}" }
For more information about resource parameters in {{ TF }}, see the [provider documentation]({{ tf-provider-resources-link }}/vpc_subnet).
-
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, {{ TF }} 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]({{ link-console-main }}) or these CLI commands:
yc vpc subnet list
-
-
{% endlist %}