> For the complete documentation index, see [llms.txt](https://docs.console.zenlayer.com/api-reference/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.console.zenlayer.com/api-reference/cn/compute/zec/elastic-ip/associateeipaddress.md).

# AssociateEipAddress

## 1. 接口描述

本接口(AssociateEipAddress)用于批量将弹性公网IP（EIP）绑定到同地域的云产品实例上。

{% hint style="info" %}
**注意事项**

* 你可以通过 DescribeLoadBalancers 接口来查看支持绑定负载均衡的实例。
* 你可以通过 DescribeNetworkInterfaces 接口来查看支持绑定的虚拟网卡。
* 你可以通过 DescribeNatGateways 接口来查看支持绑定的NAT网关。
* 你可以通过 DescribeHaVips 接口来查看支持绑定的高可用虚拟IP。
* 参数`loadBalancerId`、`nicId`、`natId`、`haVipId`必须传1个。如果多个字段同时传入，则按`loadBalancerId`、`nicId`、`natId`、`haVipId`顺序优先使用。
* 通过`haVipId`绑定高可用虚拟IP时，`bindType`只能为`FullNat`（普通NAT模式），传其他值本次请求的EIP将全部绑定失败。
* 如果遇到绑定数量超过了限制，可以联系Support来提高配额。
* 接口会返回绑定失败的eip id集合，如果是空的则说明全部成功。
  {% endhint %}

## 2. 请求参数

以下请求参数列表仅列出了接口中需要的请求参数

| 参数名称           | 必选 | 类型                                                                  | 描述                                                                                                                       |
| -------------- | -- | ------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------ |
| eipIds         | 是  | Array of String                                                     | <p>要绑定的EIP的ID。</p><p>EIP 必须是未绑定状态。</p>                                                                                   |
| loadBalancerId | 否  | String                                                              | 负载均衡实例的ID。                                                                                                               |
| nicId          | 否  | String                                                              | 虚拟网卡的ID。                                                                                                                 |
| lanIp          | 否  | String                                                              | <p>要绑定的网卡上的内网IP地址。</p><p>当IP绑定的是网卡, 则该字段不能为空。</p>                                                                        |
| natId          | 否  | String                                                              | NAT网关的ID。                                                                                                                |
| haVipId        | 否  | String                                                              | 高可用虚拟IP的ID。                                                                                                              |
| bindType       | 否  | [BindType](/api-reference/cn/compute/zec/datastructure.md#bindtype) | <p>绑定类型。</p><p>绑定网卡时生效，默认为普通NAT模式。</p><p>绑定高可用虚拟IP(haVip)时仅支持FullNat（普通NAT）模式，传其他值本次请求的EIP将全部绑定失败。</p><p>默认值：FullNat</p> |

## 3. 响应结果

| 参数名称         | 类型              | 描述                                                       |
| ------------ | --------------- | -------------------------------------------------------- |
| requestId    | String          | <p>唯一请求 ID。</p><p>每次请求都会返回。定位问题时需要提供该次请求的 requestId。</p> |
| failedEipIds | Array of String | 绑定失败的IP。                                                 |

## 4. 代码示例

{% tabs %}
{% tab title="示例" %}
**1. 将多个弹性公网IP（EIP）绑定到指定网卡上， 并指定方式为可见模式。**

```json
POST /api/v2/zec HTTP/1.1
Host: console.zenlayer.com
Content-Type: application/json
X-ZC-Action: AssociateEipAddress
<Common Request Params>

Request：
{
  "nicId": "<nicId>",
  "lanIp": "10.0.0.2",
  "eipIds": ["<eipId1>","<eipId2>"],
  "bindType": "Passthrough"
}

Response：
{
  "requestId": "T05992D0C-7E8B-4047-B0C0-780F2CD549D3",
  "response": {
    "requestId": "T05992D0C-7E8B-4047-B0C0-780F2CD549D3"
  }
}
```

**2. 将多个弹性公网IP（EIP）绑定到指定负载均衡时实例上。**

```json
POST /api/v2/zec HTTP/1.1
Host: console.zenlayer.com
Content-Type: application/json
X-ZC-Action: AssociateEipAddress
<Common Request Params>

Request：
{
  "loadBalancerId": "<loadBalancerId>",
  "eipIds": ["<eipId1>","<eipId2>"]
}

Response：
{
  "requestId": "T05992D0C-7E8B-4047-B0C0-780F2CD549D3",
  "response": {
    "requestId": "T05992D0C-7E8B-4047-B0C0-780F2CD549D3"
  }
}
```

**3. 将多个弹性公网IP（EIP）绑定到指定NAT网关上。**

```json
POST /api/v2/zec HTTP/1.1
Host: console.zenlayer.com
Content-Type: application/json
X-ZC-Action: AssociateEipAddress
<Common Request Params>

Request：
{
  "natId": "<natId>",
  "eipIds": ["<eipId1>","<eipId2>"]
}

Response：
{
  "requestId": "T05992D0C-7E8B-4047-B0C0-780F2CD549D3",
  "response": {
    "requestId": "T05992D0C-7E8B-4047-B0C0-780F2CD549D3"
  }
}
```

**4. 将一个弹性公网IP（EIP）绑定到高可用虚拟IP（HaVip）上。**

```json
POST /api/v2/zec HTTP/1.1
Host: console.zenlayer.com
Content-Type: application/json
X-ZC-Action: AssociateEipAddress
<Common Request Params>

Request：
{
  "haVipId": "<haVipId>",
  "eipIds": ["<eipId1>"]
}

Response：
{
  "requestId": "T05992D0C-7E8B-4047-B0C0-780F2CD549D3",
  "response": {
    "requestId": "T05992D0C-7E8B-4047-B0C0-780F2CD549D3"
  }
}
```

**5. 将多个弹性公网IP（EIP）同时绑定到高可用虚拟IP（HaVip）上。**

```json
POST /api/v2/zec HTTP/1.1
Host: console.zenlayer.com
Content-Type: application/json
X-ZC-Action: AssociateEipAddress
<Common Request Params>

Request：
{
  "haVipId": "<haVipId>",
  "eipIds": ["<eipId1>", "<eipId2>"]
}

Response：
{
  "requestId": "T05992D0C-7E8B-4047-B0C0-780F2CD549D3",
  "response": {
    "requestId": "T05992D0C-7E8B-4047-B0C0-780F2CD549D3",
    "failedEipIds": []
  }
}
```

{% endtab %}
{% endtabs %}

## 5. 开发者工具

Zenlayer Cloud API 2.0 提供了配套的[开发工具集（SDK）](/api-reference/cn/api-introduction/toolkit.md)，未来会陆续支持更多开发语言，方便快速接入和使用Zenlayer的产品和服务。

## 6. 错误码

下面包含业务逻辑中遇到的错误码，其他错误码见[公共错误码](/api-reference/cn/api-introduction/instruction/commonerrorcode.md)

| HTTP状态码 | 错误码                                               | 说明               |
| ------- | ------------------------------------------------- | ---------------- |
| 404     | INVALID\_LB\_NOT\_FOUND                           | 负载均衡器不存在。        |
| 400     | INVALID\_LB\_STATUS                               | 当前负载均衡器状态无法进行操作。 |
| 400     | INVALID\_REGION\_MISMATCH                         | 区域不匹配。           |
| 404     | INVALID\_HAVIP\_NOT\_FOUND                        | 高可用虚拟IP不存在。      |
| 400     | OPERATION\_DENIED\_EIP\_ASSIGNED\_LIMIT\_EXCEEDED | EIP绑定超过数量限制。     |
