-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
local-app/python-tools/cross-organization/check_region_resources.py
This file contains 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |