Skip to content

Instantly share code, notes, and snippets.

@jezhumble
Last active October 9, 2019 05:52
Show Gist options
  • Save jezhumble/03aa9be710a5c93f6809f55dd0303e12 to your computer and use it in GitHub Desktop.
Save jezhumble/03aa9be710a5c93f6809f55dd0303e12 to your computer and use it in GitHub Desktop.

Revisions

  1. jezhumble revised this gist Dec 20, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion inventory_vpcs.py
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,7 @@ def append(my_list, my_key, my_value):
    else:
    my_list[my_key].append(my_value)

    parser = argparse.ArgumentParser(description='Delete all internet gateways, subnets, and unoccupied vpcs in an AWS account. Use with care.')
    parser = argparse.ArgumentParser(description='List EC2, RDS and ElastiCache instances, grouped by VPC.')
    parser.add_argument('--aws-key', dest='aws_key', help='AWS Key')
    parser.add_argument('--aws-secret-key', dest='aws_secret_key', help='AWS Secret Key')
    parser.add_argument('--region', dest='region', help='Limit to a single region')
  2. jezhumble revised this gist Dec 20, 2016. 1 changed file with 32 additions and 15 deletions.
    47 changes: 32 additions & 15 deletions inventory_vpcs.py
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,22 @@
    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-

    import argparse, sys, boto3, pprint
    import argparse, sys, boto3
    from colorama import Fore, Style

    def count(my_list, my_key):
    if my_key not in my_list:
    return '0'
    else:
    return str(len(my_list[my_key]))

    parser = argparse.ArgumentParser(description='Show all EC2 and RDS instances in your AWS account, grouped by VPC.')
    def append(my_list, my_key, my_value):
    if my_key not in my_list:
    my_list[my_key] = [ my_value ]
    else:
    my_list[my_key].append(my_value)

    parser = argparse.ArgumentParser(description='Delete all internet gateways, subnets, and unoccupied vpcs in an AWS account. Use with care.')
    parser.add_argument('--aws-key', dest='aws_key', help='AWS Key')
    parser.add_argument('--aws-secret-key', dest='aws_secret_key', help='AWS Secret Key')
    parser.add_argument('--region', dest='region', help='Limit to a single region')
    @@ -27,30 +34,37 @@ def count(my_list, my_key):
    if (not args.region) or (args.region == region):
    ec2client = session.client('ec2', region)
    rdsclient = session.client('rds', region)
    cacheclient = session.client('elasticache', region)

    instances = {}
    dbs = {}
    caches = {}
    cache_subnets = {}

    ec2_instances = ec2client.describe_instances(Filters=[ { 'Name': 'instance-state-name', 'Values': [ 'running' ] } ])
    for reservation in ec2_instances['Reservations']:
    for instance in reservation['Instances']:
    instance_name = instance['InstanceId']
    instance_name = instance['InstanceId']
    for tag in instance['Tags']:
    if tag['Key'] == 'Name':
    instance_name = instance['InstanceId'] + ' (' + tag['Value'] + ')'
    if instance['VpcId'] in instances:
    instances[instance['VpcId']].append(instance_name)
    else:
    instances[instance['VpcId']] = [ instance_name ]
    instance_name = instance['InstanceId'] + ' (' + tag['Value'] + ')'
    append(instances, instance['VpcId'], instance_name)

    db_instances = rdsclient.describe_db_instances()
    for db_instance in db_instances['DBInstances']:
    if 'DBSubnetGroup' in db_instance:
    db_vpc = db_instance['DBSubnetGroup']['VpcId']
    if db_vpc in dbs:
    dbs[db_vpc].append(db_instance['DBInstanceIdentifier'])
    else:
    dbs[db_vpc] = [ db_instance['DBInstanceIdentifier'] ]
    append(dbs, db_vpc, db_instance['DBInstanceIdentifier'])

    aws_cache_subnets = cacheclient.describe_cache_subnet_groups()
    for cache_subnet in aws_cache_subnets['CacheSubnetGroups']:
    cache_subnets[cache_subnet['CacheSubnetGroupName']] = cache_subnet['VpcId']

    cache_clusters = cacheclient.describe_cache_clusters()
    for cache_cluster in cache_clusters['CacheClusters']:
    cache_name = cache_cluster['CacheSubnetGroupName']
    cache_vpc = cache_subnets[cache_name]
    append(caches, cache_vpc, cache_name)

    vpcs = ec2client.describe_vpcs()
    for vpc in vpcs['Vpcs']:
    @@ -59,8 +73,11 @@ def count(my_list, my_key):
    for tag in vpc['Tags']:
    if tag['Key'] == "Name":
    vpc_name = tag['Value']
    print('\033[1;32;40m' + vpc_id + ' | ' + vpc_name + ' | ' + vpc['CidrBlock'] + ' (' + count(instances, vpc_id) + ' ec2 instances, ' + count(dbs, vpc_id) + ' rds instances)\033[0;37;40m')
    print(Fore.GREEN + Style.BRIGHT + vpc_id + ' | ' + vpc_name + ' | ' + vpc['CidrBlock'] + ' (' + count(instances, vpc_id) + ' ec2 instances, ' + count(dbs, vpc_id) + ' rds instances, ' + count(caches, vpc_id) + ' elastica\
    che instances)' + Style.RESET_ALL)
    if vpc_id in instances:
    print('\033[1;33;40m ec2 instances: \033[0;37;40m' + ','.join(instances[vpc_id]))
    print(Fore.YELLOW + ' ec2 instances: ' + Style.RESET_ALL + ','.join(instances[vpc_id]))
    if vpc_id in dbs:
    print('\033[1;35;40m rds instances: \033[0;37;40m' + ','.join(dbs[vpc_id]))
    print(Fore.CYAN + ' rds instances: ' + Style.RESET_ALL + ','.join(dbs[vpc_id]))
    if vpc_id in caches:
    print(Fore.BLUE + ' elasticache instances: ' + Style.RESET_ALL + ','.join(caches[vpc_id]))
  3. jezhumble revised this gist Dec 17, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion inventory_vpcs.py
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ def count(my_list, my_key):
    else:
    return str(len(my_list[my_key]))

    parser = argparse.ArgumentParser(description='Delete all internet gateways, subnets, and unoccupied vpcs in an AWS account. Use with care.')
    parser = argparse.ArgumentParser(description='Show all EC2 and RDS instances in your AWS account, grouped by VPC.')
    parser.add_argument('--aws-key', dest='aws_key', help='AWS Key')
    parser.add_argument('--aws-secret-key', dest='aws_secret_key', help='AWS Secret Key')
    parser.add_argument('--region', dest='region', help='Limit to a single region')
  4. jezhumble renamed this gist Dec 17, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. jezhumble revised this gist Dec 17, 2016. No changes.
  6. jezhumble revised this gist Dec 17, 2016. 1 changed file with 35 additions and 7 deletions.
    42 changes: 35 additions & 7 deletions find_empty_vpcs.py
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,13 @@

    import argparse, sys, boto3, pprint

    parser = argparse.ArgumentParser(description='Find VPCs that contain no EC2 instances')
    def count(my_list, my_key):
    if my_key not in my_list:
    return '0'
    else:
    return str(len(my_list[my_key]))

    parser = argparse.ArgumentParser(description='Delete all internet gateways, subnets, and unoccupied vpcs in an AWS account. Use with care.')
    parser.add_argument('--aws-key', dest='aws_key', help='AWS Key')
    parser.add_argument('--aws-secret-key', dest='aws_secret_key', help='AWS Secret Key')
    parser.add_argument('--region', dest='region', help='Limit to a single region')
    @@ -20,19 +26,41 @@
    print("Region: " + region)
    if (not args.region) or (args.region == region):
    ec2client = session.client('ec2', region)
    rdsclient = session.client('rds', region)

    instances = {}
    dbs = {}

    ec2_instances = ec2client.describe_instances(Filters=[ { 'Name': 'instance-state-name', 'Values': [ 'running' ] } ])
    for reservation in ec2_instances['Reservations']:
    for instance in reservation['Instances']:
    if instance['VpcId'] in instances:
    instances[instance['VpcId']].append(instance['InstanceId'])
    instance_name = instance['InstanceId']
    for tag in instance['Tags']:
    if tag['Key'] == 'Name':
    instance_name = instance['InstanceId'] + ' (' + tag['Value'] + ')'
    if instance['VpcId'] in instances:
    instances[instance['VpcId']].append(instance_name)
    else:
    instances[instance['VpcId']] = [ instance['InstanceId'] ]
    vpcs = ec2client.describe_vpcs()
    instances[instance['VpcId']] = [ instance_name ]

    db_instances = rdsclient.describe_db_instances()
    for db_instance in db_instances['DBInstances']:
    if 'DBSubnetGroup' in db_instance:
    db_vpc = db_instance['DBSubnetGroup']['VpcId']
    if db_vpc in dbs:
    dbs[db_vpc].append(db_instance['DBInstanceIdentifier'])
    else:
    dbs[db_vpc] = [ db_instance['DBInstanceIdentifier'] ]

    vpcs = ec2client.describe_vpcs()
    for vpc in vpcs['Vpcs']:
    if vpc['IsDefault'] == False:
    contents = ', '.join(instances[vpc['VpcId']]) if vpc['VpcId'] in instances else 'empty'
    print(vpc['VpcId'] + ' ' + vpc['CidrBlock'] + ' ' + contents)
    vpc_id = vpc['VpcId']
    for tag in vpc['Tags']:
    if tag['Key'] == "Name":
    vpc_name = tag['Value']
    print('\033[1;32;40m' + vpc_id + ' | ' + vpc_name + ' | ' + vpc['CidrBlock'] + ' (' + count(instances, vpc_id) + ' ec2 instances, ' + count(dbs, vpc_id) + ' rds instances)\033[0;37;40m')
    if vpc_id in instances:
    print('\033[1;33;40m ec2 instances: \033[0;37;40m' + ','.join(instances[vpc_id]))
    if vpc_id in dbs:
    print('\033[1;35;40m rds instances: \033[0;37;40m' + ','.join(dbs[vpc_id]))
  7. jezhumble created this gist Dec 16, 2016.
    38 changes: 38 additions & 0 deletions find_empty_vpcs.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-

    import argparse, sys, boto3, pprint

    parser = argparse.ArgumentParser(description='Find VPCs that contain no EC2 instances')
    parser.add_argument('--aws-key', dest='aws_key', help='AWS Key')
    parser.add_argument('--aws-secret-key', dest='aws_secret_key', help='AWS Secret Key')
    parser.add_argument('--region', dest='region', help='Limit to a single region')
    args = parser.parse_args()

    if args.aws_key and args.aws_secret_key:
    session = boto3.Session(aws_access_key_id=args.aws_key, aws_secret_access_key=args.aws_secret_key)
    else:
    session = boto3.Session()

    regions = session.get_available_regions('ec2')

    for region in regions:
    print("Region: " + region)
    if (not args.region) or (args.region == region):
    ec2client = session.client('ec2', region)

    instances = {}

    ec2_instances = ec2client.describe_instances(Filters=[ { 'Name': 'instance-state-name', 'Values': [ 'running' ] } ])
    for reservation in ec2_instances['Reservations']:
    for instance in reservation['Instances']:
    if instance['VpcId'] in instances:
    instances[instance['VpcId']].append(instance['InstanceId'])
    else:
    instances[instance['VpcId']] = [ instance['InstanceId'] ]
    vpcs = ec2client.describe_vpcs()

    for vpc in vpcs['Vpcs']:
    if vpc['IsDefault'] == False:
    contents = ', '.join(instances[vpc['VpcId']]) if vpc['VpcId'] in instances else 'empty'
    print(vpc['VpcId'] + ' ' + vpc['CidrBlock'] + ' ' + contents)