From dbba56977fd11844a3b4c776ea47d276bbe4d99c Mon Sep 17 00:00:00 2001 From: badra001 Date: Fri, 27 Mar 2026 08:29:38 -0400 Subject: [PATCH] add --- .../check_region_resources.py | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100755 local-app/python-tools/cross-organization/check_region_resources.py diff --git a/local-app/python-tools/cross-organization/check_region_resources.py b/local-app/python-tools/cross-organization/check_region_resources.py new file mode 100755 index 00000000..d8d8548e --- /dev/null +++ b/local-app/python-tools/cross-organization/check_region_resources.py @@ -0,0 +1,53 @@ +import boto3 + +# --- VERSIONING --- +__version__ = "1.0.0" + +def account_task(account_session, account_id, account_name, region): + results = {"alias": "N/A", "data": {}} + try: + ec2_mgmt = account_session.client('ec2', region_name=region) + # Get all regions and their opt-in status + region_info = ec2_mgmt.describe_regions(AllRegions=True)['Regions'] + + for reg in region_info: + r_name = reg['RegionName'] + r_status = reg['OptInStatus'] + + # Skip regions that are not opted-in/accessible + if r_status == 'not-opted-in': + results["data"][r_name] = {"status": r_status, "resources": {}} + continue + + # Regional resource collection + ec2 = account_session.client('ec2', region_name=r_name) + reg_data = {"status": r_status, "vpcs": []} + + try: + # Describe VPCs and flag defaults + vpcs = ec2.describe_vpcs() + for vpc in vpcs['Vpcs']: + v_id = vpc['VpcId'] + is_default = vpc.get('IsDefault', False) + + # Gather associated resources + subnets = ec2.describe_subnets(Filters=[{'Name': 'vpc-id', 'Values': [v_id]}]) + igws = ec2.describe_internet_gateways(Filters=[{'Name': 'attachment.vpc-id', 'Values': [v_id]}]) + rts = ec2.describe_route_tables(Filters=[{'Name': 'vpc-id', 'Values': [v_id]}]) + + reg_data["vpcs"].append({ + "vpc_id": v_id, + "is_default": is_default, + "subnets": [s['SubnetId'] for s in subnets['Subnets']], + "igws": [igw['InternetGatewayId'] for igw in igws['InternetGateways']], + "route_tables": [rt['RouteTableId'] for rt in rts['RouteTables'] if not rt.get('Associations')] + }) + except Exception as e: + reg_data["error"] = str(e) + + results["data"][r_name] = reg_data + + results["data"]["account_summary"] = {"_summary": f"REGIONS:{len(results['data'])-1}"} + except Exception as e: + results["error"] = str(e) + return results