Sometimes it’s handy to know what IP address ranges are used in different AWS Regions. You may want to whitelist access to your region AWS endpoints, or from global CloudFront location, or set up some special routing rules, or look up which region a certain IP belongs to.

ip-ranges.json & filter-ip-ranges

AWS publishes ip-ranges.json file with all that info. But it’s a huge JSON and not really meant for human consumption. So here I wrote a small parser: filter-ip-ranges

It’s a standalone Python program – just download it (or GIT clone the aws-utils github repository) make it executable (chmod +x filter-ip-ranges) and run.

If you just run it it will print all the ranges from all regions and services.

~/aws-utils $ ./filter-ip-ranges
# SERIAL=1545170057
[... 860 more ...]

It’s a good start, the script apparently works and can download and parse the ip-ranges.json file.

How about printing only the prefixes from Sydney region (ap-southeast-2) with some extra info (-v)?

~/aws-utils $ filter-ip-ranges -v ap-southeast-2
# SERIAL=1545170057
# 1323 prefixes found / 865 prefixes consolidated / 38 prefixes matching ap-southeast-2 AMAZON EC2 ap-southeast-2 CLOUDFRONT ap-southeast-2 CODEBUILD ap-southeast-2 AMAZON_CONNECT ap-southeast-2 AMAZON S3
[...] ap-southeast-2 AMAZON EC2 ap-southeast-2 ROUTE53_HEALTHCHECKS ap-southeast-2 ROUTE53_HEALTHCHECKS ap-southeast-2 AMAZON EC2 ap-southeast-2 AMAZON

As we can see each prefix has some tags – is it used for EC2? S3? CloudFront? Route53 Health Checks?

Let’s see what prefixes are used for S3 in the Sydney region:

~/aws-utils $ ./filter-ip-ranges -v ap-southeast-2 S3 ap-southeast-2 AMAZON S3 ap-southeast-2 AMAZON S3 ap-southeast-2 AMAZON S3 ap-southeast-2 AMAZON S3

Or maybe you’re after all Cloud Front prefixes all around the world to create a whitelist for your firewall?

~/aws-utils $ ./filter-ip-ranges -v CLOUDFRONT GLOBAL AMAZON CLOUDFRONT GLOBAL AMAZON CLOUDFRONT ap-southeast-2 CLOUDFRONT us-east-2 CLOUDFRONT ap-northeast-1 CLOUDFRONT

The GLOBAL ones are edge-locations, the ones with region name are, well, in that region.

Looking up IP addresses

Finally we can check to which region belongs a given IP address, let’s say this blog’s address:

~/aws-utils $ ./filter-ip-ranges -v
# SERIAL=1545170057
# 1323 prefixes found / 865 prefixes consolidated / 1 prefixes matching ap-southeast-2 AMAZON EC2

Not surprisingly it’s in the Sydney region as we can see from the ap-southeast-2 label.

One little issue we see here is that in ip-ranges.json quite a lot is hidden under the EC2 label – even though this blog’s IP is actually an Elastic Load Balancer the prefix tag is EC2 and not ELB. We can’t do much about it until Amazon labels the prefixes in the JSON file in a more granular way.

I hope you find this little script handy!