Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obtain all available Elastic IP addresses in boto3

What is the boto3 equivalent to:

import boto

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

(returning all Elastic IP addresses)

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

I am a little bit confused by the generalization that seem to apply to VPC setups as well.


What I found so far is following:

import boto3

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

This response does not seem to contain the association status.

like image 784
Falk Schuetzenmeister Avatar asked Aug 25 '15 20:08

Falk Schuetzenmeister


People also ask

How many elastic IPs are there?

All AWS accounts are limited to five Elastic IP addresses per Region.

How do I find my Elastic IP?

To describe your Elastic IP addressesOpen the Amazon EC2 console at https://console.aws.amazon.com/ec2/ . In the navigation pane, choose Elastic IPs. Select the Elastic IP address to view and choose Actions, View details.

How many elastic IPs can be connected to an instance?

You're limited to five Elastic IP addresses. To help conserve them, you can use a NAT device. For more information, see Connect to the internet or other networks using NAT devices.

How many Elastic IP addresses can you have per region by default?

As the IPv4 public IP addresses are a scarce resource nowadays, by default, all AWS accounts are limited to 5 (five) Elastic IP addresses per region.


2 Answers

Here's a simple example that prints all Elastic IP public IP addresses in the current account/region:

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

For more, see the EC2.Client.describe_addresses reference documentation.

like image 199
jarmod Avatar answered Dec 24 '22 15:12

jarmod


This may help:

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

like image 28
sailinnthu Avatar answered Dec 24 '22 17:12

sailinnthu