Skip to content

RaunitArya/Azure-Cost-Analyzer-

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

119 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Azure Cloud Cost Analyzer

Project Status Version

FastAPI Swagger Postgres Azure SDK Python Pydantic uv

πŸ“Œ Problem Description

Developers often overspend on cloud; this tool: Fetches Azure VM, app service, DB cost Predicts monthly bill Shows per-service breakdown Alerts if budget exceeds a limit Suggests cost-optimization tips

🎯 Objectives

  • To analyze Azure cloud resource costs
  • To provide detailed cost breakdown and visualization
  • To implement budget monitoring and alerting
  • To integrate AI-based cost intelligence

πŸ“ Directory Structure

Azure Cost Analyzer/
β”œβ”€β”€ README.md
β”œβ”€β”€ backend/                          # FastAPI backend application
β”‚   β”œβ”€β”€ alembic.ini
β”‚   β”œβ”€β”€ docker-compose.yml
β”‚   β”œβ”€β”€ Dockerfile
β”‚   β”œβ”€β”€ entrypoint.sh
β”‚   β”œβ”€β”€ pyproject.toml
β”‚   β”œβ”€β”€ requirements.txt
β”‚   └── app/
β”‚       β”œβ”€β”€ config.py                 # Application configuration
β”‚       β”œβ”€β”€ main.py                   # FastAPI application entry point
β”‚       β”œβ”€β”€ scheduler.py              # Task scheduler for alerts & anomalies
β”‚       β”œβ”€β”€ alembic/                  # Database migration scripts
β”‚       β”‚   β”œβ”€β”€ env.py
β”‚       β”‚   β”œβ”€β”€ README
β”‚       β”‚   β”œβ”€β”€ script.py.mako
β”‚       β”‚   └── versions/             # Migration history
β”‚       β”œβ”€β”€ azure/                    # Azure SDK integration
β”‚       β”‚   β”œβ”€β”€ auth.py               # Azure authentication logic
β”‚       β”‚   └── cost_client.py        # Azure Cost Management API client
β”‚       β”œβ”€β”€ db/
β”‚       β”‚   β”œβ”€β”€ database.py           # PostgreSQL connection setup
β”‚       β”‚   β”œβ”€β”€ models.py             # SQLAlchemy ORM models
β”‚       β”‚   β”œβ”€β”€ operations.py         # Generic database operations
β”‚       β”‚   └── alert_operations.py   # Alert-specific DB operations
β”‚       β”œβ”€β”€ exceptions/
β”‚       β”‚   └── cost_exceptions.py    # Custom exception classes
β”‚       β”œβ”€β”€ handlers/
β”‚       β”‚   └── exception_handlers.py # Global exception handlers
β”‚       β”œβ”€β”€ models/                   # Pydantic data models
β”‚       β”‚   β”œβ”€β”€ alert_models.py       # Alert request/response models
β”‚       β”‚   └── cost_models.py        # Cost analysis data models
β”‚       β”œβ”€β”€ routes/                   # API endpoints
β”‚       β”‚   β”œβ”€β”€ alert_routes.py       # Alert API routes
β”‚       β”‚   └── cost_routes.py        # Cost analysis API routes
β”‚       β”œβ”€β”€ services/                 # Business logic & orchestration
β”‚       β”‚   β”œβ”€β”€ alert_service.py      # Alert management logic
β”‚       β”‚   β”œβ”€β”€ cache_service.py      # Caching layer for performance
β”‚       β”‚   β”œβ”€β”€ cost_preprocessor.py  # Data preprocessing pipeline
β”‚       β”‚   β”œβ”€β”€ cost_service.py       # Cost data fetching & analysis
β”‚       β”‚   β”œβ”€β”€ cost_tasks.py         # Background task definitions
β”‚       β”‚   └── email_service.py      # Email notification service
β”‚       └── utils/
β”‚           └── responses.py          # Standardized API response helpers
└── frontend/                         # React + TypeScript UI application
    β”œβ”€β”€ package.json
    β”œβ”€β”€ tsconfig.json
    β”œβ”€β”€ vite.config.ts
    β”œβ”€β”€ tailwind.config.ts
    β”œβ”€β”€ postcss.config.js
    β”œβ”€β”€ eslint.config.js
    β”œβ”€β”€ vitest.config.ts
    β”œβ”€β”€ components.json
    β”œβ”€β”€ index.html
    β”œβ”€β”€ public/
    β”‚   β”œβ”€β”€ robots.txt
    β”‚   └── staticwebapp.config.json
    └── src/
        β”œβ”€β”€ App.tsx
        β”œβ”€β”€ main.tsx
        β”œβ”€β”€ vite-env.d.ts
        β”œβ”€β”€ App.css
        β”œβ”€β”€ index.css
        β”œβ”€β”€ components/               # Reusable UI components
        β”‚   β”œβ”€β”€ AppSidebar.tsx
        β”‚   β”œβ”€β”€ NavLink.tsx
        β”‚   β”œβ”€β”€ dashboard/            # Dashboard-specific components
        β”‚   └── ui/                   # Shadcn/ui components
        β”œβ”€β”€ hooks/                    # Custom React hooks
        β”‚   β”œβ”€β”€ use-cost-data.ts
        β”‚   β”œβ”€β”€ use-mobile.tsx
        β”‚   └── use-toast.ts
        β”œβ”€β”€ lib/                      # Utility functions & types
        β”‚   β”œβ”€β”€ api.ts                # API client configuration
        β”‚   β”œβ”€β”€ colors.ts             # Color constants
        β”‚   β”œβ”€β”€ config.ts             # Frontend configuration
        β”‚   β”œβ”€β”€ types.ts              # TypeScript type definitions
        β”‚   └── utils.ts              # Helper utilities
        β”œβ”€β”€ pages/                    # Page components
        β”‚   β”œβ”€β”€ Index.tsx             # Dashboard home page
        β”‚   β”œβ”€β”€ CostAnalysis.tsx      # Cost analysis page
        β”‚   β”œβ”€β”€ Budget.tsx            # Budget management page
        β”‚   β”œβ”€β”€ Reports.tsx           # Reports page
        β”‚   β”œβ”€β”€ Settings.tsx          # Settings page
        β”‚   └── NotFound.tsx          # 404 page
        └── test/                     # Test files
            β”œβ”€β”€ example.test.ts
            └── setup.ts

πŸš€ Setup & Installation

Prerequisites

  • Python 3.11+ recommended
  • Git installed
  • (Optional) Azure CLI for auth workflows

Option 1: Fast setup with uv (recommended)

# Install uv if needed
curl -LsSf https://astral.sh/uv/install.sh | sh
# Create and sync environment
uv venv
uv pip install -r requirements.txt
uv run app/main.py

Option 2: Standard virtualenv + pip

python -m venv .venv
source .venv/bin/activate  # Windows: .venv\Scripts\activate
pip install --upgrade pip
pip install -r requirements.txt
python -m app/main.py

πŸ›‘οΈ Obtaining Azure Credentials

To use this project, you must have:

  • An Azure Subscription
  • Billing Reader or Cost Management Reader role on that subscription
  • Owner access (only once, to set things up)

Step 1: Log in to the Azure Portal

Step 2: Register an Application (App Registration)

  • Navigate to "Microsoft Entra ID (Azure AD)" β†’ "App registrations" β†’ "New registration".

  • Enter a name (eg: cost-analyzer)

  • Supported account types: Choose

    Accounts in this organizational directory only (Single tenant)
    
  • Click Register.

Step 3: Copy important IDs

After registration, you land on Overview page.

Copy and save these safely:

Field What it is
Application (client) ID App’s username
Directory (tenant) ID Your Azure organization
Object ID Internal Azure reference

You will use:

Put them in a .env file later.

Step 4: Create a Client Secret

  • Go to "Certificates & secrets" β†’ "New client secret".
  • Add a description and expiry, then click "Add".
  • Copy the generated Value (this is your AZURE_CLIENT_SECRET).

Step 5: Get Your Subscription ID

  • In the Azure Portal, search for "Subscriptions".
  • Select your subscription and copy the Subscription ID.

Step 6: Assign Cost Management Reader Role to the App

  • Go to Subscriptions β†’ "Access control (IAM)" β†’ "Add role assignment".
  • You'll see a long list of roles. Search for "Cost Management Reader" role β†’ Click Next.
  • Select your app (Service Principal):
    i) search by app name or client id
    ii) select it β†’ click Select β†’ click Next

Step 7: Review and Assign

  • Review details:
    i) Role: Cost Management Reader
    ii) Scope: Subscription
    iii) Member: Your app
  • Click Assign

βš™οΈ Environment variables (.env)

Refer the .env.example from both backend and frontend folders. Create a .env file in each and fill in the required values.

πŸ“§ Setting Up SMTP for Email Alerts

The application supports sending email alerts for budget warnings and anomaly detection. Follow the steps below to configure SMTP:

Gmail SMTP Setup

  1. Enable 2-Step Verification (if not already enabled):

  2. Generate an App Password:

  3. Add to .env:

    SMTP_SERVER=smtp.gmail.com
    SMTP_PORT=587
    SMTP_USERNAME=your-email@gmail.com
    SMTP_PASSWORD=your-16-char-app-password
    SMTP_FROM_EMAIL=your-email@gmail.com
    SMTP_FROM_NAME=Azure Cost Analyzer
  4. You can also refer to How to Configure Gmail SMTP Server Settings

About

Cloud Cost Analyzer for Microsoft Azure

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors