{
  "output": "import boto3\nfrom aws_utils import get_account_names, get_previous_month_range, get_boto_session\n\n\ndef fetch_costs_with_savings(ce_client, org_client, start_date, end_date):\n    \"\"\"Fetches costs for EC2 grouped by account and service.\"\"\"\n    account_map = get_account_names()\n\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[\"ResultsByTime\"]:\n            print(\"No cost data available for the specified period.\")\n            return\n\n        for result in response[\"ResultsByTime\"]:\n            period_start = result[\"TimePeriod\"][\"Start\"]\n            print(f\"Billing Period: {period_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 = metrics[\"UnblendedCost\"][\"Amount\"]\n                    amortized = metrics[\"AmortizedCost\"][\"Amount\"]\n                    net_amortized = metrics[\"NetAmortizedCost\"][\"Amount\"]\n                    net_unblended = metrics[\"NetUnblendedCost\"][\"Amount\"]\n                    unit = metrics[\"UnblendedCost\"][\"Unit\"]\n\n                    account_name = account_map.get(account_id, account_id)\n\n                    print(f\"Account: {account_name} | Service: {service}\")\n                    print(f\"  - Total Cost (Unblended): {unblended} {unit}\")\n                    print(f\"  - Net Cost (Amortized): {net_amortized} {unit}\")\n                    print(f\"  - Net Cost (Unblended): {net_unblended} {unit}\")\n                    print(f\"  - Final Cost (Amortized): {amortized} {unit}\")\n\n    except Exception as e:\n        print(f\"Error fetching costs: {e}\")\n\n\ndef main():\n    session = get_boto_session()\n    ce_client = session.client(\"ce\", region_name=\"us-east-1\")\n    org_client = session.client(\"organizations\", region_name=\"us-east-1\")\n\n    start_date, end_date = get_previous_month_range()\n\n    print(\n        f\"Fetching AWS EC2 costs with compute savings from {start_date} to {end_date}...\"\n    )\n    fetch_costs_with_savings(ce_client, org_client, start_date, end_date)\n\n\nif __name__ == \"__main__\":\n    main()\n"
}