{
  "output": "Successfully created and wrote to new file: /Users/bob/git/dxw/aws-stuff/audits/cost.py. Here is the updated code:\nimport boto3\nfrom botocore.exceptions import ClientError\nfrom aws_utils import get_account_names, get_previous_month_range, get_boto_session\n\ndef fetch_costs_with_savings(ce_client, start_date, end_date, account_map):\n    \"\"\"\n    Fetches EC2 costs grouped by account and service.\n    Returns a list of dicts with cost details.\n    \"\"\"\n    results = []\n    try:\n        response = ce_client.get_cost_and_usage(\n            TimePeriod={\"Start\": start_date, \"End\": end_date},\n            Granularity=\"MONTHLY\",\n            Metrics=[\n                \"UnblendedCost\",\n                \"AmortizedCost\",\n                \"NetAmortizedCost\",\n                \"NetUnblendedCost\",\n            ],\n            GroupBy=[\n                {\"Type\": \"DIMENSION\", \"Key\": \"LINKED_ACCOUNT\"},\n                {\"Type\": \"DIMENSION\", \"Key\": \"SERVICE\"},\n            ],\n        )\n\n        if not response.get(\"ResultsByTime\"):\n            return results\n\n        for result in response[\"ResultsByTime\"]:\n            period_start = result[\"TimePeriod\"][\"Start\"]\n\n            for group in result[\"Groups\"]:\n                account_id = group[\"Keys\"][0]\n                service = group[\"Keys\"][1]\n\n                if \"Amazon Elastic Compute Cloud\" in service:\n                    metrics = group[\"Metrics\"]\n                    unblended = float(metrics[\"UnblendedCost\"][\"Amount\"])\n                    amortized = float(metrics[\"AmortizedCost\"][\"Amount\"])\n                    net_amortized = float(metrics[\"NetAmortizedCost\"][\"Amount\"])\n                    net_unblended = float(metrics[\"NetUnblendedCost\"][\"Amount\"])\n                    unit = metrics[\"UnblendedCost\"][\"Unit\"]\n\n                    account_name = account_map.get(account_id, account_id)\n\n                    results.append({\n                        \"PeriodStart\": period_start,\n                        \"AccountName\": account_name,\n                        \"AccountID\": account_id,\n                        \"Service\": service,\n                        \"UnblendedCost\": unblended,\n                        \"AmortizedCost\": amortized,\n                        \"NetAmortizedCost\": net_amortized,\n                        \"NetUnblendedCost\": net_unblended,\n                        \"Unit\": unit\n                    })\n\n    except ClientError as e:\n        print(f\"Error fetching costs: {e}\")\n        \n    return results\n\ndef run():\n    session = get_boto_session()\n    ce_client = session.client(\"ce\", region_name=\"us-east-1\")\n    account_map = get_account_names()\n    start_date, end_date = get_previous_month_range()\n\n    print(f\"Fetching AWS EC2 costs with compute savings from {start_date} to {end_date}...\")\n    \n    cost_data = fetch_costs_with_savings(ce_client, start_date, end_date, account_map)\n    \n    if not cost_data:\n        print(\"No cost data available for the specified period.\")\n        return\n\n    for item in cost_data:\n        print(f\"Billing Period: {item['PeriodStart']}\")\n        print(f\"Account: {item['AccountName']} | Service: {item['Service']}\")\n        print(f\"  - Total Cost (Unblended): {item['UnblendedCost']} {item['Unit']}\")\n        print(f\"  - Net Cost (Amortized): {item['NetAmortizedCost']} {item['Unit']}\")\n        print(f\"  - Net Cost (Unblended): {item['NetUnblendedCost']} {item['Unit']}\")\n        print(f\"  - Final Cost (Amortized): {item['AmortizedCost']} {item['Unit']}\")\n\nif __name__ == \"__main__\":\n    run()\n"
}