Python boto - 如何获取弹性IP的分配ID

3 投票
1 回答
2973 浏览
提问于 2025-04-18 09:05

当我使用boto(一个用于AWS的Python工具包)给我的VPC实例分配弹性IP时,出现了以下错误:

    <Response>
      <Errors>
         <Error>
           <Code>InvalidParameterCombination</Code>
           <Message>You must specify an allocation id when mapping an address to a network interface</Message>
    </Error>
    </Errors>
    <RequestID>d0f95756-c36f-417a-9fa9-1158c4aa19e9</RequestID>
</Response>

但是我在ec2-medata中没有找到任何关于分配ID的信息。

有没有人知道我该如何通过API获取分配ID?

我已经在控制台中看到了它:

try: conn = boto.ec2.connect_to_region(zone, aws_access_key_id='a-id', aws_secret_access_key='s-id',debug=2) conn.associate_address(allow_reassociation=True, public_ip=kw['ip'], network_interface_id=nid, dry_run=True)

1 个回答

2

如果你使用 get_all_addresses 这个功能,它会返回一个包含 Address 对象的列表。这些对象都有一个叫 allocation_id 的属性。只要是和 VPC(虚拟私有云)有关的地址,它们的 allocation_id 就不会是空的。

import boto.ec2
c = boto.ec2.connect_to_region('us-east-1')
addresses = c.get_all_addresses()
for addr in addresses:
    print('%s - %s' % (addr.public_ip, addr.allocation_id)

这样解释清楚了吗?

撰写回答