Check Which Region and Service an AWS IP is used for (Python3)

Amazon Web Services (AWS) are vast, and use a significant range of IP blocks

Sometimes we want to try and ascertain where an AWS IP is used. Amazon publish details of their ranges and how they're used

This snippet provides a simple Python3 script to fetch AWS's listing and print details about an IP's usage

Details

Snippet

#!/usr/bin/env python3
#
# Get details of where an AWS IP is used
#
# usage: search.py [ip]

import ipaddress
import requests
import sys

IP = ipaddress.ip_address(sys.argv[1])
r = requests.get('https://ip-ranges.amazonaws.com/ip-ranges.json')
j = r.json()

# Iterate over ranges
for range in j['prefixes']:
    if IP in ipaddress.ip_network(range['ip_prefix']):
        print(range)

Usage Example

./locate_aws_ip.py 143.204.170.78
# {'ip_prefix': '143.204.0.0/16', 'region': 'GLOBAL', 'service': 'AMAZON', 'network_border_group': 'GLOBAL'}
# {'ip_prefix': '143.204.0.0/16', 'region': 'GLOBAL', 'service': 'CLOUDFRONT', 'network_border_group': 'GLOBAL'}