Skip to content

Instantly share code, notes, and snippets.

@yogendratamang48
Last active May 6, 2022 16:06
Show Gist options
  • Save yogendratamang48/9ab4cd9dc8154cc03aa0fc28d50d1123 to your computer and use it in GitHub Desktop.
Save yogendratamang48/9ab4cd9dc8154cc03aa0fc28d50d1123 to your computer and use it in GitHub Desktop.

Show simple information about your AWS

Requirements

pip install boto3
pip install tabulate
#! /usr/bin/env python3
"""
lists your aws loadbalancers
each function will return following meta about the account
- Resource Name:
- LB Type: classic, network, application
- Scheme: internal, public-facing
- Targets:
"""
import boto3
from hypothesis import target
from tabulate import tabulate
from multiprocessing.pool import Pool
elb = boto3.client('elb')
elbv2 = boto3.client('elbv2')
HEADER = ['name', 'lb_type', 'scheme', 'targets']
def get_targets(target_arn):
"""returns number of targets
"""
targets = elbv2.describe_target_health(TargetGroupArn=target_arn)['TargetHealthDescriptions']
return len(targets)
def get_target_groups(lb):
"""returns target groups"""
tgs = elbv2.describe_target_groups(LoadBalancerArn=lb['arn'])
obj = {** lb }
obj['tg_arns'] = []
for tg in tgs['TargetGroups']:
obj['tg_arns'].append(tg['TargetGroupArn'])
return obj
def get_elbv2s():
"""
returns subnets and cidrs
"""
lbs = elbv2.describe_load_balancers()['LoadBalancers']
clean_lbs = []
for lb in lbs:
obj = {}
obj['lb_type'] = lb['Type']
obj['scheme'] = lb['Scheme']
obj['name'] = lb['LoadBalancerName']
obj['arn'] = lb['LoadBalancerArn']
clean_lbs.append(obj)
return clean_lbs
def build_data(obj):
"""returns row for the table
"""
row_data = []
for row_key in HEADER:
row_data.append(obj.get(row_key))
return row_data
def get_elbs():
"""
returns subnets and cidrs
"""
lbs = elb.describe_load_balancers()['LoadBalancerDescriptions']
clean_lbs = []
for lb in lbs:
obj = {}
obj['lb_type'] = "classic"
obj['targets'] = len(lb['Instances'])
obj['scheme'] = lb['Scheme']
obj['name'] = lb['LoadBalancerName']
row_data = build_data(obj)
clean_lbs.append(row_data)
return clean_lbs
if __name__ == '__main__':
data = []
data.extend(get_elbs())
# data.extend(get_elbv2s())
clean_lbs = get_elbv2s()
# print(clean_lbs)
with Pool(1) as P:
clean_obj = []
target_groups = P.map(get_target_groups, clean_lbs)
for tg in target_groups:
tg_counts = P.map(get_targets, tg['tg_arns'])
tg['targets'] = len(tg_counts)
raw_data = build_data(tg)
clean_obj.append(raw_data)
data.extend(clean_obj)
print(tabulate(data, headers=HEADER))
#! /usr/bin/env python3
"""
lists your vpc, subnets and CIDRs
each function will return following meta about the account
- Resource ID:
- Resource Type: vpc, subnet
- Resource Name:
- CIDR:
Supports two arguments
region
profile
"""
import boto3
from tabulate import tabulate
import argparse
import os
import os
profile = "default"
# Check environments
if os.environ.get('AWS_PROFILE'):
profile = os.environ['AWS_PROFILE']
# Priority to passed arguments
parser = argparse.ArgumentParser()
parser.add_argument('--profile', help="aws profile")
args = parser.parse_args()
session = boto3.Session()
if args.profile:
profile = args.profile
session = boto3.Session(profile_name=profile)
print(f"AWS Profile: {profile}\n")
client = session.client('ec2')
# HEADER = ['Id', 'Resource Type', 'Resource Name', 'cidr']
HEADER = ['id', 'resource_type', 'name', 'cidr']
def get_vpcs():
"""returns vpc, and cidrs
"""
resp = client.describe_vpcs()
vpcs = []
resource_type = "vpc"
for vpc in resp['Vpcs']:
obj = {}
obj['resource_type'] = resource_type
obj['cidr'] = vpc['CidrBlock']
obj['id'] = vpc['VpcId']
try:
obj['name'] = [_ for _ in vpc['Tags'] if _['Key'] == 'Name'][0]['Value']
except Exception as e:
obj['name'] = "-"
row_data = build_data(obj)
vpcs.append(row_data)
return vpcs
def build_data(obj):
"""returns row for the table
"""
row_data = []
for row_key in HEADER:
row_data.append(obj.get(row_key))
return row_data
def get_subnets():
"""
returns subnets and cidrs
"""
resp = client.describe_subnets()
subnets = []
resource_type = 'subnet'
for subnet in resp['Subnets']:
obj = {}
obj['resource_type'] = resource_type
obj['cidr'] = subnet['CidrBlock']
obj['id'] = subnet['SubnetId']
try:
obj['name'] = [_ for _ in subnet['Tags'] if _['Key'] == 'Name'][0]['Value']
except Exception as e:
obj['name'] = "-"
row_data = build_data(obj)
subnets.append(row_data)
return subnets
if __name__ == '__main__':
data = []
data.extend(get_vpcs())
data.extend(get_subnets())
print(tabulate(data, headers=HEADER))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment