Example 1: Creating a VPC and Subnet for an ECS
Scenarios
This section describes how to create a VPC and subnet for an ECS by calling APIs.
Prerequisites
If you use a token for authentication, you must obtain the token and add X-Auth-Token to the request header when making an API call. Obtain the token by referring to Authentication.

The token obtained from IAM is valid for only 24 hours. If you want to use a token for authentication, you can cache it to avoid frequent calling.
Procedure
- Create a VPC.
- Send POST https://VPC endpoint/v1/{project_id}/vpcs. Parameter project_id indicates the project ID.
- Add X-Auth-Token to the request header.
- Set the following parameters in the request body. Plan the value of the cidr field in advance.
{ "vpc": { "name": "vpc", //VPC name "cidr": "192.168.0.0/16" //Available subnet in the VPC } }
- Check the response message.
- The request is successful if the following response is displayed. In the response, id indicates the VPC ID.
{ "vpc": { "id": "b6684a27-b049-407d-90b4-c9551f2390e1", "name": "vpc", "cidr": "192.168.0.0/16", "status": "CREATING", "routes": [] } }
- For details about the error codes displayed if the request fails, see section Error Codes.
- The request is successful if the following response is displayed. In the response, id indicates the VPC ID.
- View details of the VPC.
- Send GET https://VPC endpoint/v1/{project_id}/vpcs/{vpc_id}. Parameter project_id indicates the project ID.
- Add X-Auth-Token to the request header.
- Check the response message.
- The request is successful if the following response is displayed. In the response, id indicates the VPC ID.
{ "vpc": { "id": "b6684a27-b049-407d-90b4-c9551f2390e1", "name": "vpc", "description": "", "cidr": "192.168.0.0/16", "status": "OK", "enterprise_project_id": "0" , "routes": [] } }
- For details about the error codes displayed if the request fails, see section Error Codes.
- The request is successful if the following response is displayed. In the response, id indicates the VPC ID.
- Create a subnet in the VPC.
- Send POST https://VPC endpoint/v1/{project_id}/subnets. Parameter project_id indicates the project ID.
- Add X-Auth-Token to the request header.
- Set the following parameters in the request body. For details about the values of dnsList, see What Are the Private DNS Server Addresses Provided by the DNS Service?
{ "subnet": { "name": "subnet", "description": "", "cidr": "192.168.0.0/24", "gateway_ip": "192.168.0.1", "dhcp_enable": true, "dnsList": ["114.xx.xx.114", "114.xx.xx.115"], "availability_zone": "aa-bb-cc", "vpc_id": "b6684a27-b049-407d-90b4-c9551f2390e1" } }
- Check the response message.
- The request is successful if the following response is displayed.
{ "subnet": { "id": "4779ab1c-7c1a-44b1-a02e-93dfc361b32d", "name": "subnet", "description": "", "cidr": "192.168.0.0/24", "dnsList": ["114.xx.xx.114", "1114.xx.xx.115"], "status": "UNKNOWN", "vpc_id": "b6684a27-b049-407d-90b4-c9551f2390e1", "gateway_ip": "192.168.0.1", "dhcp_enable": true, "primary_dns": "114.xx.xx.114", "secondary_dns": "114.xx.xx.115", "availability_zone": "aa-bb-cc", "neutron_network_id": "4779ab1c-7c1a-44b1-a02e-93dfc361b32d", "neutron_subnet_id": "213cb9d-3122-2ac1-1a29-91ffc1231a12", "extra_dhcp_opts": [] } }
- For details about the error codes displayed if the request fails, see section Error Codes.
- The request is successful if the following response is displayed.
- View details of the subnet.
- Send GET https://VPC endpoint/v1/{project_id}/subnets/{subnet_id}. Parameter project_id indicates the project ID.
- Add X-Auth-Token to the request header.
- Check the response message.
{ "subnet": { "id": "4779ab1c-7c1a-44b1-a02e-93dfc361b32d", "name": "subnet", "description": "", "cidr": "192.168.20.0/24", "dnsList": ["114.xx.xx.114", "114.xx.xx.115"], "status": "ACTIVE", "vpc_id": "b6684a27-b049-407d-90b4-c9551f2390e1", "gateway_ip": "192.168.20.1", "ipv6_enable": false, "dhcp_enable": true, "primary_dns": "114.xx.xx.114", "secondary_dns": "114.xx.xx.115", "availability_zone": "aa-bb-cc", "neutron_network_id": "4779ab1c-7c1a-44b1-a02e-93dfc361b32d", "neutron_subnet_id": "213cb9d-3122-2ac1-1a29-91ffc1231a12", "extra_dhcp_opts": [] } }
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.