如何在boto3中获取所有可用的弹性IP地址

How to obtain all available Elastic IP addresses in boto3

boto3 等价于什么:

import boto

conn = boto.connect_ec2()
addresses = conn.get_all_addresses()

(返回所有弹性 IP 地址)

import boto3
ec2 = boto3.resource('ec2')
addresses = ec2.????

我对似乎也适用于 VPC 设置的概括感到有点困惑。


目前我发现的如下:

import boto3

client = boto3.client('ec2')
print client.describe_addresses()

此响应似乎不包含关联状态。

这是一个简单的例子,打印当前 account/region 中的所有弹性 IP public IP 地址:

import boto3
client = boto3.client('ec2')
addresses_dict = client.describe_addresses()
for eip_dict in addresses_dict['Addresses']:
    print(eip_dict['PublicIp'])

有关更多信息,请参阅 EC2.Client.describe_addresses reference documentation

这可能有帮助:

import boto3
ec2 = boto3.resource('ec2', region_name="ap-southeast-1")
client = boto3.client('ec2', region_name="ap-southeast-1")
# create 3 x Elastic IP Addresses.  Set to Domain='vpc' to allocate the address for use with instances in a VPC.
eip1 = client.allocate_address(Domain='vpc')
eip2 = client.allocate_address(Domain='vpc')
eip3 = client.allocate_address(Domain='vpc')

# A collection of VpcAddresses resources "vpc_addresses.all()"
print eip = list(ec2.vpc_addresses.all())
[ec2.VpcAddress(allocation_id='eipalloc-3f693f5a'), ec2.VpcAddress(allocation_id='eipalloc-7896c01d'), 
ec2.VpcAddress(allocation_id='eipalloc-9997c1fc')]

Reference Link 1

Reference Link 2