Updated on 2026-08-14 GMT+08:00

Attaching a NIC to an ECS

Scenarios

If an ECS requires multiple NICs, you can call the API for creating NICs and attach them to the ECS.

A NIC can be attached by setting the nics parameter during ECS creation or after the ECS is created. This section describes how to attach a NIC to a created ECS.

Procedure

  1. Create a NIC.

    1. Create a network.
      • API

        URI format: POST /v1/{project_id}/vpcs

        For details, see Creating a VPC.

      • Example Request

        POST https://{Endpoint}/v1/{project_id}/vpcs

        Obtain {endpoint} from Regions and Endpoints.

        Body:

        {
            "vpc": {
                "name": "vpc",
                "description": "test",
                "cidr": "192.168.0.0/16"
            }
        }
      • Example Response
        {
        	"vpc": {
        		"id": "99d9d709-8478-4b46-9f3f-2206b1023fd3",
        		"name": "vpc",
        		"description": "test",
        		"cidr": "192.168.0.0/16",
        		"status": "CREATING",
        		"routes": []
        	}
        }
    2. Record the vpc ID in the response.
    3. Create a subnet.
      • API

        URI format: POST /v1/{project_id}/subnets

        For details, see Creating a Subnet.

      • Example Request

        POST https://{endpoint}/v1/{project_id}/subnets

        Obtain {endpoint} from Regions and Endpoints.

        Body:

        {
            "subnet": {
                "name": "subnet",
                "description": "",
                "cidr": "192.168.20.0/24",
                "gateway_ip": "192.168.20.1",
                "ipv6_enable": true,
                "dhcp_enable": true,
                "primary_dns": "114.xx.xx.114",
                "secondary_dns": "114.xx.xx.115",
                "dnsList": [
                    "114.xx.xx.114",
                    "114.xx.xx.115"
                ],
                "availability_zone": "aa-bb-cc",
                "enable_network_address_usage_metrics": false,
                "vpc_id": "3ec3b33f-ac1c-4630-ad1c-7dba1ed79d85",
                "extra_dhcp_opts": [
                    {
                        "opt_value": "10.100.0.33,10.100.0.34",
                        "opt_name": "ntp"
                    },
                    {
                        "opt_value": "24h",
                        "opt_name": "addresstime"
                    },
                    {
                        "opt_value": "2h",
                        "opt_name": "ipv6_addresstime"
                    }
                ]
            }
        }
      • Example Response
        {
            "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": "UNKNOWN",
                "vpc_id": "3ec3b33f-ac1c-4630-ad1c-7dba1ed79d85",
                "gateway_ip": "192.168.20.1",
                "ipv6_enable": true, 
                "cidr_v6": "2001:db8:a583::/64",
                "gateway_ip_v6": "2001:db8:a583::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",
                "neutron_subnet_id_v6": "e0fa7de1-a6e2-44c9-b052-b9d8cebe93c4",  
                "enable_network_address_usage_metrics": false,
                "available_ip_address_count": 251,
                "extra_dhcp_opts": [
                    {
                        "opt_value": "10.100.0.33,10.100.0.34",
                        "opt_name": "ntp"
                    },
                    {
                        "opt_value": "24h",
                        "opt_name": "addresstime"
                    },
                    {
                        "opt_value": "2h",
                        "opt_name": "ipv6_addresstime"
                    }
                ],
                "tenant_id": "087679f0aa80d32a2f4ec0172f5e902b",
                "created_at": "2022-12-15T02:42:07",
                "updated_at": "2022-12-15T02:42:07"
            }
        }

    4. Record the subnet ID in the response.
    5. Create a port.
      • API

        URI format: POST /v1/{project_id}/ports

        For details, see Creating a Port.

      • Example Request

        POST https://{endpoint}/v1/{project_id}/ports

        Obtain {endpoint} from Regions and Endpoints.

        Body:

        {
            "port": {
                "fixed_ips": [
                    {
                        "ip_address": "192.168.0.38",
                        "subnet_id": "06bc2359-d75e-4f96-82f4-313e39c7148c"
                    }
                ],
                "network_id": "28a1c93c-9a5e-4a9f-813b-e495bdef7d34",
                "security_groups": [
                    "f2c5b3fc-b971-4a86-87b9-032586260e3e"
                ]
            }
        }
      • Example Response
        {
            "port": {
                "id": "d00f9c13-412f-4855-8af3-de5d8c24cd60",
                "name": "test",
                "status": "DOWN",
                "admin_state_up": "true",
                "fixed_ips": [
                    {
                        "subnet_id": "70f2e74b-e660-410a-b754-0ca46744348a",
                        "ip_address": "10.128.1.10"
                    }
                ],
                "dns_name": "",
                "mac_address": "fa:16:3e:d7:f2:6c",
                "network_id": "5b808927-13c9-4e60-a4f4-ed6ffe225167",
                "tenant_id": "43f2d1cca56a40729dcb17212482f34d",
                "device_id": "",
                "device_owner": "",
                "security_groups": [
                    "02b4e8ee-74fa-4a31-802e-5490df11245e"
                ],
                "extra_dhcp_opts": [],
                "allowed_address_pairs": [],
                "binding:vnic_type": "normal",
                "enable_efi": false
            }
        }

    6. Record the port ID in the response.

  2. Attach the NIC to the ECS.

    • API

      URI format: POST /v1/{project_id}/cloudservers/{server_id}/nics

      For details, see Adding NICs to an ECS in a Batch.

    • Example Request

      POST https://{endpoint}/v1/74610f3a5ad941998e91f076297ecf27/cloudservers/9f4d9281-95e7-4915-a126-1ee597101e2e/nics

      Obtain {endpoint} from Regions and Endpoints.

      Body:

      {
          "nics": [
              {
                  "subnet_id": "877b5567-e8c6-4a0d-aabf-0f13da225fe5" 
              }
          ]
      }
    • Example Response
      {      
          "job_id": "ff80808288d41e1b018990260955686a" 
      } 

  3. Query task status.

    • API

      URI format: GET /v1/{project_id}/jobs/{job_id}

      For details, see Querying Job Execution Status.

    • Example Request

      GET https://{endpoint}/v1/74610f3a5ad941998e91f076297ecf27/jobs/ff80808288d41e1b018990260955686a

      Obtain {endpoint} from Regions and Endpoints.

    • Example Response
      {
          "job_id": "ff80808288d41e1b018990260955686a",
          "job_type": "addNicsForServer",
          "begin_time": "2025-02-14T08:43:05.181Z",
          "end_time": "2025-02-14T08:43:17.075Z",
          "status": "SUCCESS",
          "error_code": null,
          "fail_reason": null,
          "entities": {
              "sub_jobs_total": 1,
              "sub_jobs": [
                  {
                      "job_id": "ff80808288d41e1b018990260955686a",
                      "job_type": "addNic",
                      "begin_time": "2025-02-14T08:43:06.423Z",
                      "end_time": "2025-02-14T08:43:12.380Z",
                      "status": "SUCCESS",
                      "error_code": null,
                      "fail_reason": null,
                      "entities": {
                          "nic_id": "877b5567-e8c6-4a0d-aabf-0f13da225fe5"
                      }
                  }
              ]
          }
      }