Created
September 7, 2018 21:34
-
-
Save cmattoon/c3fabd923f0eb9e0aa26ba0fe89641ef to your computer and use it in GitHub Desktop.
Graph AWS Route Tables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
./route_tables.py | dot -Tpng > routes.png | |
""" | |
import boto3 | |
def get_tag(obj, key, default=''): | |
for tag in obj['Tags']: | |
if tag['Key'] == key: | |
return tag['Value'] | |
return default | |
def main(): | |
ec2 = boto3.client('ec2') | |
vpcs = ec2.describe_vpcs()['Vpcs'] | |
rtbls = ec2.describe_route_tables()['RouteTables'] | |
VPCs = {} | |
tables = [] | |
lines = ["digraph VPC {"] | |
for vpc in vpcs: | |
VPCs[vpc['VpcId']] = {'vpc': vpc, 'rtb': []} | |
for rtb in rtbls: | |
VPCs[rtb['VpcId']]['rtb'].append(rtb) | |
for vpc_id in VPCs: | |
vpc = VPCs[vpc_id] | |
v = vpc['vpc'] | |
rtbls = vpc['rtb'] | |
for r in rtbls: | |
for rt in r['Routes']: | |
if rt['State'] != 'active': continue | |
if 'GatewayId' in rt: | |
dst = rt['GatewayId'] | |
elif 'VpcPeeringConnectionId' in rt: | |
dst = rt['VpcPeeringConnectionId'] | |
else: | |
dst = "|".join(rt.keys()) | |
if 'DestinationCidrBlock' in rt: | |
tgt = rt['DestinationCidrBlock'] | |
elif 'DestinationPrefixListId' in rt: | |
tgt = rt['DestinationPrefixListId'] | |
else: | |
tgt = "|".join(rt.keys()) | |
lines.append(""" "{}" -> "{}" """.format(v['CidrBlock'], tgt)) | |
lines.append("}") | |
print("\n".join(lines)) | |
if __name__ == "__main__": | |
vpcs = main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment