Skip to content

Instantly share code, notes, and snippets.

@cmattoon
Created September 7, 2018 21:34
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 cmattoon/c3fabd923f0eb9e0aa26ba0fe89641ef to your computer and use it in GitHub Desktop.
Save cmattoon/c3fabd923f0eb9e0aa26ba0fe89641ef to your computer and use it in GitHub Desktop.
Graph AWS Route Tables
#!/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