Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
badra001 committed Mar 27, 2026
1 parent df98d55 commit dbba569
Showing 1 changed file with 53 additions and 0 deletions.
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

0 comments on commit dbba569

Please sign in to comment.