3

I am trying to determine the AWS Region that my heroku database is stored on. All I have is the IP address.

How can i determine the AWS region?

1

1 Answer 1

11

Download the JSON file AWS IP ranges: AWS IP Address Ranges

It contains the CIDRs. You may want to write a simple script and check if the IP falls in any of the CIDRs and then retrieve the corresponding region. This is how JSON looks:

{
  "ip_prefix": "13.56.0.0/16",
  "region": "us-west-1",
  "service": "AMAZON"
},

Here is Python3 code to find the region, given an IP. Assumes the ip-ranges.json file downloaded from AWS is in the current directory. Will not work in Python 2.7

from ipaddress import ip_network, ip_address
import json

def find_aws_region(ip):
  ip_json = json.load(open('ip-ranges.json'))
  prefixes = ip_json['prefixes']
  my_ip = ip_address(ip)
  region = 'Unknown'
  for prefix in prefixes:
    if my_ip in ip_network(prefix['ip_prefix']):
      region = prefix['region']
      break
  return region

Test

>>> find_aws_region('54.153.41.72')
'us-west-1'
>>> find_aws_region('54.250.58.207')
'ap-northeast-1'
>>> find_aws_region('154.250.58.207')
'Unknown'
1
  • FWIW, if you get ipaddress.AddressValueError:...Did you pass in a bytes (str in Python 2) instead of a unicode object?, then wrap the string argument in unicode() Commented Jan 15, 2020 at 1:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.