Skip to content

Instantly share code, notes, and snippets.

@Justintime50
Created September 1, 2021 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Justintime50/56326269c5afefe461ae229bf22da829 to your computer and use it in GitHub Desktop.
Save Justintime50/56326269c5afefe461ae229bf22da829 to your computer and use it in GitHub Desktop.
Gets the complete list of AWS IP ranges and parses the results
import requests
# Gets the complete list of AWS IP ranges and parses the results
# Usage: venv/bin/python aws_ip_range_parser.py
AWS_IP_URL = 'https://ip-ranges.amazonaws.com/ip-ranges.json'
REGIONS_TO_FILTER = [
'us-east-2',
'us-west-2',
]
SERVICES_TO_FILTER = [
'EC2',
'EC2_INSTANCE_CONNECT',
]
# POSSIBLE_SERVICES = [
# 'AMAZON_APPFLOW',
# 'AMAZON_CONNECT',
# 'AMAZON',
# 'API_GATEWAY',
# 'CHIME_MEETINGS',
# 'CHIME_VOICECONNECTOR',
# 'CLOUD9',
# 'CLOUDFRONT',
# 'CODEBUILD',
# 'DYNAMODB',
# 'EBS',
# 'EC2_INSTANCE_CONNECT',
# 'EC2',
# 'GLOBALACCELERATOR',
# 'KINESIS_VIDEO_STREAMS',
# 'ROUTE53_HEALTHCHECKS_PUBLISHING',
# 'ROUTE53_HEALTHCHECKS',
# 'ROUTE53_RESOLVER',
# 'ROUTE53',
# 'S3',
# 'WORKSPACES_GATEWAYS',
# ]
# POSSIBLE_REGIONS = [
# 'af-south-1',
# 'ap-east-1',
# 'ap-northeast-1',
# 'ap-northeast-2',
# 'ap-northeast-3',
# 'ap-south-1',
# 'ap-south-2',
# 'ap-southeast-1',
# 'ap-southeast-2',
# 'ap-southeast-3',
# 'ap-southeast-4',
# 'ca-central-1',
# 'cn-north-1',
# 'cn-northwest-1',
# 'eu-central-1',
# 'eu-central-2',
# 'eu-north-1',
# 'eu-south-1',
# 'eu-south-2',
# 'eu-west-1',
# 'eu-west-2',
# 'eu-west-3',
# 'GLOBAL',
# 'me-central-1',
# 'me-south-1',
# 'sa-east-1',
# 'us-east-1',
# 'us-east-2',
# 'us-gov-east-1',
# 'us-gov-west-1',
# 'us-west-1',
# 'us-west-2',
# ]
def main():
# Records in the response look like the following:
# {
# "ip_prefix": "3.91.171.128/25",
# "region": "us-east-1",
# "service": "AMAZON",
# "network_border_group": "us-east-1"
# },
response = requests.get(AWS_IP_URL)
response_json = response.json()
ip_addresses = response_json['prefixes']
final_list = []
for ip_address in ip_addresses:
if ip_address['region'] in REGIONS_TO_FILTER and ip_address['service'] in SERVICES_TO_FILTER:
final_list.append(ip_address['ip_prefix'])
for item in sorted(final_list):
print(item)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment