Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Client IP Address to Closest AWS Region

Question

I would like to upload some data to AWS from a client device, but I'd like to upload to the closest AWS Region's S3 Bucket.

Similarly, I'd like to be able to download from the nearest region.

Of course, I'd set up a bucket in each region

Is there a system that I can use that maybe takes the IP Address of the client, then works out whether it's us-west-1, eu-west-1, eu-central-1, ap-northeast-1 etc?

The crux of the problem is this. The data i'm uploading is useful only to one person and it needs to get to that one person as quickly as possible.

So if I'm in England, I upload a file and my intended recipient is currently in Japan (as they could be on the move) - Uploading to Londons AWS region would have a higher ping time, than of a region closer to Japan.

like image 761
Chris Avatar asked Apr 19 '16 16:04

Chris


1 Answers

Find client's location from IP

Use geoip

pip install python-geoip
pip install python-geoip-geolite2

Then your code will look something like this.

from geoip import geolite2
match = geolite2.lookup('8.8.8.8')

print match.location

This produces, (37.386, -122.0838)

Find locations of all AWS centers

The information is available from: http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/LocationsOfEdgeServers.html you need to find the geolocations for them. That can be done with geopy

pip install geopy

Then

from geopy.geocoders import Nominatim
geolocator = Nominatim()
location = geolocator.geocode("Singapore")
print location.latitude, location.longitude

Which gives

1.2904527 103.852038

You need to do this for all your locations and save the data somehwere. Possibly in an RDBMS (if you are doing that consider using django, django has excellent support for geolocation searching using GeoDjango)

Finally finding the distance

Having found the client location, let us call it l1, and having found the data center locations, it's time to find the distance

from geopy.distance import great_circle
great_circle(l1.point, l2.point)

And there you have the distance

Finding the closest distance

You could loop through all your saved locations and find the closest distance, or if you saved your data in an RDBMS that supports geospatial data (postgis immidiately comes to mind) you can use the ST_Distance function to do the distance compaison quickly and effectively with very little code. As mentioned earlier, django has excellent support for geospatial queries.

If you were to use Postgis/Django , the loop involving great_circle would be replaced by a call to st_distance.

like image 78
e4c5 Avatar answered Sep 18 '22 20:09

e4c5