pythonamazon-web-serviceseip

How to get list of all latest EIP of AWS for all regions


I would like to know how can I list out all the EIP or public IP of aws for all regions?

Maybe via aws cli or python.


Solution

  • I have waited but nobody answered, hence after waiting for 2 mins now I am going to answer my own question.

    Check the below python code, it will list out number of EIP in each region aws have

    import requests, ipaddress, boto3
    
    client = boto3.client('ec2')
    awsRegions = [region['RegionName'] for region in client.describe_regions()['Regions']]
    
    resp = requests.get('https://ip-ranges.amazonaws.com/ip-ranges.json')
    
    for eachRegion in awsRegions:
          ip_pool = []
          for prefix in resp.json()['prefixes']:
                if prefix['region'] == eachRegion and prefix['service'] == 'EC2':
                      ip_pool.append(prefix['ip_prefix'])
          total_ip = []
          for eachCIDR in ip_pool:
                net = ipaddress.ip_network(eachCIDR)
                total_ip.append(int(net.num_addresses))
    
          print (f'[{eachRegion}] total IP ==> {sum(total_ip)}')
    
    exit()
    

    Thanks for all the votes in advance.