|
| 1 | +--- |
| 2 | +date: '2026-04-01' |
| 3 | +title: Check If an IP Address Belongs to AWS |
| 4 | +description: A quick tip to check whether an IP address belongs to AWS, including which service and region it's in. |
| 5 | +--- |
| 6 | +## Check If an IP Address Belongs to AWS |
| 7 | + |
| 8 | +At work, all of our services run in AWS, so when I'm looking at logs or investigating traffic I often need to know whether an IP address belongs to AWS. We do have all of our public egress IPs documented, but sometimes you just want a quick way to check an arbitrary IP without digging through internal docs. AWS publishes their complete list of IP ranges as a [public JSON file](https://ip-ranges.amazonaws.com/ip-ranges.json), and with a bit of Python you can check any IP against it in seconds. |
| 9 | + |
| 10 | +```bash |
| 11 | +curl -s https://ip-ranges.amazonaws.com/ip-ranges.json | python3 -c " |
| 12 | +import json, sys, ipaddress |
| 13 | +data = json.load(sys.stdin) |
| 14 | +targets = ['1.2.3.4', '5.6.7.8'] |
| 15 | +for ip in targets: |
| 16 | + addr = ipaddress.ip_address(ip) |
| 17 | + matches = [] |
| 18 | + for prefix in data['prefixes']: |
| 19 | + if addr in ipaddress.ip_network(prefix['ip_prefix']): |
| 20 | + matches.append(f\" {prefix['ip_prefix']} - {prefix['service']} ({prefix['region']})\" ) |
| 21 | + if matches: |
| 22 | + print(f'{ip}: AWS IP') |
| 23 | + for m in matches: |
| 24 | + print(m) |
| 25 | + else: |
| 26 | + print(f'{ip}: NOT an AWS IP') |
| 27 | + print() |
| 28 | +" |
| 29 | +``` |
| 30 | + |
| 31 | +Replace the IPs in the `targets` list with whatever you want to check. For each match, it tells you which CIDR block the IP falls in, what AWS service uses it, and which region it's in. This has no dependencies beyond Python 3 and curl. |
| 32 | + |
| 33 | +If you find yourself reaching for this often, you can wrap it in a shell function and add it to your `~/.bashrc` or `~/.zshrc`: |
| 34 | + |
| 35 | +```bash |
| 36 | +awsip() { |
| 37 | + curl -s https://ip-ranges.amazonaws.com/ip-ranges.json | python3 -c " |
| 38 | +import json, sys, ipaddress |
| 39 | +data = json.load(sys.stdin) |
| 40 | +for ip in sys.argv[1:]: |
| 41 | + addr = ipaddress.ip_address(ip) |
| 42 | + matches = [f' {p[\"ip_prefix\"]} - {p[\"service\"]} ({p[\"region\"]})' for p in data['prefixes'] if addr in ipaddress.ip_network(p['ip_prefix'])] |
| 43 | + print(f'{ip}: AWS IP' if matches else f'{ip}: NOT an AWS IP') |
| 44 | + for m in matches: print(m) |
| 45 | + print() |
| 46 | +" "$@" |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +Then you can just run `awsip 1.2.3.4 5.6.7.8` from your terminal. |
0 commit comments