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

2024-06-13 11:33:07 发布

您现在位置:Python中文网/ 问答频道 /正文

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()

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


Tags: importclientgetaddressesconnectallboto3ec2
2条回答

下面是一个简单的示例,它打印当前帐户/区域中的所有弹性IP公用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

相关问题 更多 >