全部产品
Search
文档中心

云服务器 ECS:AllocatePublicIpAddress - 分配公网IP

更新时间:Aug 12, 2026

为一台ECS实例分配一个公网IP地址。推荐您使用接口ModifyInstanceNetworkSpec分配公网IP。

接口说明

使用须知

  • 分配公网 IP 地址之前,实例必须处于运行中Running)或者已停止Stopped)状态。

  • 安全控制的实例的OperationLocks中标记了"LockReason" : "security"时,不能分配公网 IP 地址。

  • 调用该接口时请确保实例带宽大于 0,您可以调用 ModifyInstanceNetworkSpec 接口调整带宽。

注意事项

  • 仅白名单用户支持指定公网 IP 参数IpAddress,若公网带宽大于 0,您可以设置InstanceId进行随机分配;若公网带宽为 0,您需要调用 ModifyInstanceNetworkSpec 调整带宽之后再通过该接口分配。

  • 一台实例只能分配一个公网 IP 地址。如果实例已经拥有一个公网 IP 地址,将返回已绑定的公网 IP 地址IpAddress

  • 重启实例( RebootInstance )或者启动实例( StartInstance )后,新的公网 IP 地址生效。

除分配公网 IP 之外,您还可以给 VPC 类型 ECS 实例绑定弹性公网 IP(EIP)。更多信息,请参见 AssociateEipAddress

调试

您可以在OpenAPI Explorer中直接运行该接口,免去您计算签名的困扰。运行成功后,OpenAPI Explorer可以自动生成SDK代码示例。

调试

授权信息

下表是API对应的授权信息,可以在RAM权限策略语句的Action元素中使用,用来给RAM用户或RAM角色授予调用此API的权限。具体说明如下:

  • 操作:是指具体的权限点。

  • 访问级别:是指每个操作的访问级别,取值为写入(Write)、读取(Read)或列出(List)。

  • 资源类型:是指操作中支持授权的资源类型。具体说明如下:

    • 对于必选的资源类型,用前面加 * 表示。

    • 对于不支持资源级授权的操作,用全部资源表示。

  • 条件关键字:是指云产品自身定义的条件关键字。

  • 关联操作:是指成功执行操作所需要的其他权限。操作者必须同时具备关联操作的权限,操作才能成功。

操作

访问级别

资源类型

条件关键字

关联操作

ecs:AllocatePublicIpAddress

create

*Instance

acs:ecs:{#regionId}:{#accountId}:instance/{#instanceId}

请求参数

名称

类型

必填

描述

示例值

InstanceId

string

待分配 IP 地址的实例 ID。

说明

该实例带宽必须大于 0。

i-bp1gtjxuuvwj17zr****

IpAddress

string

实例的公网 IP 地址。如果该参数值为空,则默认由系统随机分配 IP 地址。

说明

该参数只支持白名单用户指定,普通用户不支持指定 IP 地址。

112.124.**.**

VlanId

string

实例的 VLAN ID。

说明

该参数即将被弃用,为提高兼容性,请尽量使用其他参数。

720

返回参数

名称

类型

描述

示例值

object

IpAddress

string

实例的公网 IP 地址。

112.124.**.**

RequestId

string

请求 ID。

473469C7-AA6F-4DC5-B3DB-A3DC0DE3C83E

示例

正常返回示例

JSON格式

{
  "IpAddress": "112.124.**.**",
  "RequestId": "473469C7-AA6F-4DC5-B3DB-A3DC0DE3C83E"
}

异常返回示例

JSON格式

{
    "RequestId": "F2EF6A3B-E345-46B9-931E-0EA094818567",
    "IpAddress": "10.1.149.159"
}

错误码

HTTP status code

错误码

错误信息

描述

400 InvalidOperation.AllocateFailed The current instance failed to allocate public IP, please try again later.
400 InvalidIpAddress.Malformed The specified parameter "IpAddress" is not valid.
400 OperationDenied Specified operation is denied as your instance is in VPC. 由于实例存在于 VPC 中,指定的操作不合法。
400 InsufficientPublicIp Ip address not found.
400 AllocateIpInvalidInstanceBandwidth OperationDenied The InternetMaxBandwidthOut of the specified instance cannot be less than 0. 当前操作无效,请保证最大输出带宽大于0。
400 OperationDenied.CloudboxNotSupport This operation is denied, because the specified ECS instance which is in the Cloudbox dose not support PublicIp or Eip.
400 OperationDenied.DedicatedRegionNotSupported This operation is denied, because the specified ECS instance which is in the dedicated region dose not support PublicIp or Eip. 专属云不支持的api的报错
500 InternalError The request processing has failed due to some unknown error.
500 InvalidOperation.AllocateIpFailed Failed to allocate the specific IP, please ensure the IP is available. 分配特定IP失败,请确保该IP可用。
403 IncorrectInstanceStatus The current status of the resource does not support this operation.
403 InstanceLockedForSecurity The specified operation is denied as your instance is locked for security reasons. 实例被安全锁定。
403 InstanceExpiredOrInArrears The specified operation is denied as your prepay instance is expired (prepay mode) or in arrears (afterpay mode).
403 IpInUse The specified IP is already in use. 使用指定IP进行绑定时,IP已经被使用在别的机器上。
403 AllocatedAlready There is an IpAddress allocated already for the specified instance. 使用指定 IP 进行绑定时,该实例已经被分配了别的 IP 地址。
403 AllocateIp.Fail The specified instance fails to allocate ip, please try again.
403 NAT_PUBLIC_IP_BINDING_FAILED Binding nat public ip failed.
403 NAT_PUBLIC_IP_ALLOCATE_FAILED Nat public ip binding failed. 公网 IP 绑定失败。
403 InvalidIpAddress.NotPublic The specified IP is not public address.
404 InvalidInstanceId.NotFound The specified InstanceId does not exist. 指定的实例ID无效。
404 InvalidVlanId.NotFound The VlanId provided does not exist in our records. 指定的虚拟局域网 ID 不存在。
404 InvalidIpAddress.NotFound The specified IP is not in the specified vlan. 指定的 IP 不在指定的虚拟局域网内。
409 OperationConflict Request was denied due to conflict with a previous request,please try again later. 请求操作的资源与之前的请求冲突
409 TokenProcessing The previous token request is still processing. Please try again later.

访问错误中心查看更多错误码。

变更历史

更多信息,参考变更详情