{
  "output": "Successfully created and wrote to new file: /Users/bob/git/dxw/aws-stuff/audits/vpc.py. Here is the updated code:\nimport boto3\nimport csv\nfrom botocore.exceptions import ClientError, ProfileNotFound\nfrom aws_utils import setup_org_accounts_session, get_boto_session, export_to_sheets\n\ndef get_vpc_name(tags):\n    if tags:\n        for tag in tags:\n            if tag[\"Key\"] == \"Name\":\n                return tag[\"Value\"]\n    return \"Unnamed VPC\"\n\ndef fetch_vpcs(ec2_client):\n    \"\"\"\n    Fetches VPC information from a single region.\n    Returns a list of dicts.\n    \"\"\"\n    vpcs_info = []\n    try:\n        paginator = ec2_client.get_paginator(\"describe_vpcs\")\n        for page in paginator.paginate():\n            for vpc in page[\"Vpcs\"]:\n                vpcs_info.append(\n                    {\n                        \"VPCName\": get_vpc_name(vpc.get(\"Tags\")),\n                        \"VPCID\": vpc[\"VpcId\"],\n                        \"CIDRBlock\": vpc.get(\"CidrBlock\", \"N/A\"),\n                    }\n                )\n    except ClientError as e:\n        print(f\"Error fetching VPCs: {e}\")\n    return vpcs_info\n\ndef get_all_org_vpcs(regions=None):\n    \"\"\"\n    Iterates through all accounts and regions to gather VPC info.\n    Returns a list of rows (dicts) for the report.\n    \"\"\"\n    if regions is None:\n        regions = [\"eu-west-1\", \"eu-west-2\"]\n    \n    all_rows = []\n    for account, profile_name in setup_org_accounts_session():\n        account_id = account[\"Id\"]\n        account_name = account[\"Name\"]\n        try:\n            # We use a session per account\n            account_session = boto3.Session(profile_name=profile_name)\n            for region in regions:\n                try:\n                    ec2_client = account_session.client(\"ec2\", region_name=region)\n                    vpcs_info = fetch_vpcs(ec2_client)\n                    \n                    for vpc in vpcs_info:\n                        row = {\n                            \"AccountName\": account_name,\n                            \"AccountID\": account_id,\n                            \"Region\": region,\n                            \"VPCName\": vpc[\"VPCName\"],\n                            \"VPCID\": vpc[\"VPCID\"],\n                            \"CIDRBlock\": vpc[\"CIDRBlock\"],\n                        }\n                        all_rows.append(row)\n                    \n                    if vpcs_info:\n                        print(f'\"{account_name}\" ({account_id}, {region}) VPCs found: {len(vpcs_info)}')\n                except ClientError as e:\n                    print(f\"Error in {region} for {account_name}: {e}\")\n        except (ClientError, ProfileNotFound) as e:\n            print(f\"Error accessing account {account_name}: {e}\")\n            \n    return all_rows\n\ndef run():\n    \"\"\"Main entry point for the VPC audit.\"\"\"\n    get_boto_session() # Ensure SSO login\n    \n    rows = get_all_org_vpcs()\n    fieldnames = [\"AccountName\", \"AccountID\", \"Region\", \"VPCName\", \"VPCID\", \"CIDRBlock\"]\n    \n    # Write to CSV\n    output_file = \"aws_vpcs_by_region.csv\"\n    with open(output_file, mode=\"w\", newline=\"\") as csvfile:\n        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)\n        writer.writeheader()\n        for row in rows:\n            writer.writerow(row)\n            \n    print(f\"\\nTotal number of VPCs found: {len(rows)}\")\n    print(f\"CSV file '{output_file}' generated successfully.\")\n    \n    # Export to Google Sheets\n    rows_for_sheets = [list(row.values()) for row in rows]\n    export_to_sheets(\"aws-vpc\", fieldnames, rows_for_sheets)\n\nif __name__ == \"__main__\":\n    run()\n"
}