# CreateSubnet

## 1. API Description

This API (CreateSubnet) is used to create a subnet under a VPC.

{% hint style="info" %} <mark style="color:blue;">**Note**</mark>

* <mark style="color:blue;">The quantity of subnets you can create under one VPC is limited. The IP range of the subnet under a VPC should be a subset of the VPC IP range.</mark>
* <mark style="color:blue;">IP ranges of subnets under the same VPC cannot be overlapped.</mark>
* <mark style="color:blue;">Call</mark> [<mark style="color:purple;">**`DescribeSubnetRegions`**</mark>](https://docs.console.zenlayer.com/api-reference/compute/zec/vpc-network/describesubnetregions) <mark style="color:blue;">to query regions supporting subnet creation.</mark>
  {% endhint %}

## 2. Input Parameters

The following request parameter list only provides API request parameters.

| Parameter Name   | Required | Type                                                                                                     | Description                                                                                                                                                                                                                                                                                                                                                                                                    |
| ---------------- | -------- | -------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| vpcId            | Yes      | String                                                                                                   | VPC ID.                                                                                                                                                                                                                                                                                                                                                                                                        |
| name             | Yes      | String                                                                                                   | <p>Subnet name to be displayed.</p><p>This parameter must start and end with a number or a letter, containing 2 to 63 characters. Only letters, numbers, - and periods (.) are supported.</p>                                                                                                                                                                                                                  |
| regionId         | Yes      | String                                                                                                   | Region ID.                                                                                                                                                                                                                                                                                                                                                                                                     |
| stackType        | Yes      | [SubnetStackType](https://docs.console.zenlayer.com/api-reference/compute/datastructure#subnetstacktype) | Subnet IP stack type.                                                                                                                                                                                                                                                                                                                                                                                          |
| cidrBlock        | No       | String                                                                                                   | <p>CIDR block of the subnet.</p><p>If the specified stack type ( stackType ) includes IPv4, this field is required. The specified CIDR block must fall within the VPC’s CIDR range.</p>                                                                                                                                                                                                                        |
| ipv6Type         | No       | [IPv6Type](https://docs.console.zenlayer.com/api-reference/compute/datastructure#ipv6type)               | <p>Subnet IPv6 type.</p><p>If the specified stack type ( stackType ) includes IPv6, this field is required.</p>                                                                                                                                                                                                                                                                                                |
| dhcpOptionsSetId | No       | String                                                                                                   | DHCP options set to be associated.                                                                                                                                                                                                                                                                                                                                                                             |
| ipv6CidrBlockId  | No       | String                                                                                                   | <p>ID of the public IPv6 CIDR block.</p><p>This field can only be specified when <code>ipv6Type</code> is <code>Public</code>.</p><p>If not specified, it will be allocated from the system's default IP pool.</p>                                                                                                                                                                                             |
| ipv6MaskLength   | No       | Integer                                                                                                  | <p>The size of the IPv6 CIDR prefix assigned to the instance.</p><p>This parameter must be used together with the <code>ipv6CidrBlockId</code> parameter.</p><p>The default value is 96 if not explicitly provided.</p><p>The minimum value must be greater than or equal to the prefix of the specified <code>ipv6CidrBlockId</code>, and the maximum value cannot exceed 96.</p><p>Value range: \[-, 96]</p> |

## 3. Output Parameters

| Parameter Name | Type   | Description                                                                                                           |
| -------------- | ------ | --------------------------------------------------------------------------------------------------------------------- |
| requestId      | String | <p>The unique request ID, which is returned for each request.</p><p>RequestId is required for locating a problem.</p> |
| subnetId       | String | Subnet ID.                                                                                                            |

## 4. Code Example

{% tabs %}
{% tab title="Example" %}
**Create a subnet under a VPC.**

<pre class="language-json"><code class="lang-json"><strong>POST /api/v2/zec HTTP/1.1
</strong>Host: console.zenlayer.com
Content-Type: application/json
X-ZC-Action: CreateSubnet


Request:
{
  "vpcId": "xxxx",
  "regionId": "xxx",
  "stackType": "IPv4_IPv6",
  "ipv6Type": "Public",
  "name": "XXX",
  "cidrBlock": "10.0.0.0/9"
}

Response:
{
  "requestId": "TBFC01FCF-6439-4530-ADBC-16809F0C3E8F",
  "response": {
    "requestId": "TBFC01FCF-6439-4530-ADBC-16809F0C3E8F",
    "subnetId": "xxxx"
  }
}
</code></pre>

{% endtab %}
{% endtabs %}

## 5. Developer Resources

Zenlayer Cloud API 2.0 integrates [SDKs](https://docs.console.zenlayer.com/api-reference/api-introduction/toolkit/api-sdk)，to make it easier for you to call APIs. More programming languages will be supported.

## 6. Error Codes

The following only lists the error codes related to the API business logic. For other error codes, see [Common Error Codes](https://docs.console.zenlayer.com/api-reference/api-introduction/instruction/commonerrorcode).

| HTTP Status Code | Error Code                                         | Description                                                                      |
| ---------------- | -------------------------------------------------- | -------------------------------------------------------------------------------- |
| 400              | INVALID\_CIDR\_BLOCK\_FORMAT                       | Invalid CIDR block format.                                                       |
| 400              | INVALID\_CIDR\_BLOCK\_NOT\_EMPTY                   | CIDR block cannot be empty.                                                      |
| 400              | INVALID\_CIDR\_BLOCK\_NOT\_LAN\_IP                 | CIDR block must be a private IP range.                                           |
| 400              | INVALID\_CIDR\_BLOCK\_STATUS                       | The CIDR block state is invalid.                                                 |
| 404              | INVALID\_CIDR\_IPV6\_NOT\_FOUND                    | IPv6 CIDR block does not exist                                                   |
| 400              | INVALID\_CIDR\_IPV6\_ONLY                          | IPv6 stack type is specified only.                                               |
| 400              | INVALID\_CIDR\_REGION\_MISMATCH                    | The subnet region does not match the IPv6 CIDR block region.                     |
| 404              | INVALID\_DHCP\_OPTIONS\_SET\_NOT\_FOUND            | The DHCP options set is not found.                                               |
| 400              | INVALID\_IP\_BROADCAST\_ADDRESS                    | The IP address cannot be a broadcast address.                                    |
| 400              | INVALID\_MAST\_LENGTH                              | The IPv6 address mask length must be greater than the subnet IPv6 prefix length. |
| 400              | INVALID\_PARAMETER\_SUBNET\_CIDR\_NOT\_BELONG\_VPC | The subnet does not belong to the specified VPC.                                 |
| 400              | INVALID\_PARAMETER\_SUBNET\_IPV6\_TYPE\_NOT\_FOUND | ipv6Type is not found.                                                           |
| 404              | INVALID\_REGION\_NOT\_FOUND                        | Specified availability zone does not exist.                                      |
| 400              | INVALID\_REGION\_NOT\_ON\_SALE                     | Subnet is not available for sale in the specified region.                        |
| 400              | INVALID\_SUBNET\_NETMASK\_MAX\_RANGE               | Subnet mask length exceeds the maximum value.                                    |
| 404              | INVALID\_VPC\_NOT\_FOUND                           | The global VPC does not exist.                                                   |
| 400              | OPERATION\_DENIED\_CIDR\_BLOCK\_IS\_EXIST          | CIDR block already exists.                                                       |
| 400              | OPERATION\_DENIED\_NO\_AVAILABLE\_CIDR             | No available CIDR block.                                                         |
| 400              | OPERATION\_DENIED\_REGION\_NOT\_SUPPORT\_PUB\_IPV6 | Public IPv6 is not supported in the specified region.                            |
| 400              | OPERATION\_DENIED\_SUBNET\_CIDR\_CONFLICT          | Subnet CIDR ranges conflict.                                                     |
| 400              | OPERATION\_DENIED\_VPC\_NOT\_SUPPORT\_PRI\_IPV6    | Private IPv6 is not supported for the specified global VPC.                      |
