A comprehensive solution for optimizing Azure costs through automated analysis of cost data, detection of idle resources, and recommendation generation for cost savings.
-
Resource Optimization
- Identifies idle VMs based on CPU usage metrics
- Recommends VM SKU downsizing for cost-efficient operations
- Detects orphaned (unattached) managed disks
- Identifies cost anomalies by comparing against baseline spending
-
Infrastructure as Code
- Terraform configuration for Azure resources
- Creates cost export schedules to Azure Storage
- Configures Log Analytics for resource metrics collection
- Sets up budget alerts for cost control
-
Security
- Azure Key Vault integration for secure credential storage
- Support for managed identities
- Secure storage of configuration with proper access controls
- Python 3.8+
- Terraform 1.5+
- Azure subscription with appropriate permissions
See requirements.txt
for the complete list of dependencies. Key components:
- Azure SDK libraries
- Analytics and data processing libraries
- Testing and development utilities
azure-cost-optimizer/
│
├── docs/ # Documentation
│ ├── architecture.md # Solution architecture
│ └── usage.md # Detailed usage guide
│
├── infra/ # Infrastructure as Code
│ ├── main.tf # Main Terraform configuration
│ └── variables.tf # Terraform variables
│
├── src/ # Source code
│ ├── __init__.py # Package initialization
│ ├── azure_client.py # Azure API client implementation
│ ├── config.py # Configuration management
│ └── optimizer.py # Core optimization logic
│
├── tests/ # Test suite
│ ├── __init__.py # Test package initialization
│ ├── test_azure_client.py # Tests for Azure client
│ └── test_optimizer.py # Tests for optimizer logic
│
├── azure-pipelines.yml # CI/CD pipeline configuration
├── LICENSE # Project license
├── README.md # This file
└── requirements.txt # Python dependencies
-
Clone the repository:
git clone https://github.com/arnabdey73/azure-cost-optimizer.git cd azure-cost-optimizer
-
Set up a Python virtual environment:
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
-
Authentication:
- Set up environment variables for Azure authentication:
# Service Principal authentication export AZURE_TENANT_ID="your-tenant-id" export AZURE_CLIENT_ID="your-client-id" export AZURE_CLIENT_SECRET="your-client-secret" export AZURE_SUBSCRIPTION_ID="your-subscription-id" # Optional: Log Analytics export LOG_ANALYTICS_WORKSPACE_ID="your-workspace-id"
- Or use Azure Key Vault for secure credential storage:
export AZURE_KEY_VAULT_URL="https://your-keyvault.vault.azure.net/"
- Set up environment variables for Azure authentication:
-
Initialize Terraform:
cd infra terraform init
-
Configure Terraform variables: Create a
terraform.tfvars
file with:subscription_id = "your-subscription-id" storage_account_name = "costoptimizer12345" # Must be globally unique allowed_ip_ranges = ["123.123.123.123"] # Your IP address
-
Deploy infrastructure:
terraform plan -out=tfplan terraform apply tfplan
Execute the optimizer to generate cost optimization recommendations:
python src/optimizer.py --subscription-id "your-subscription-id" --start-date "2025-05-01" --end-date "2025-05-28"
See docs/usage.md
for detailed command options and scenarios.
The optimizer generates a JSON file containing recommendations for:
- Idle VMs that could be shut down
- VMs that could be resized to smaller SKUs
- Orphaned disks that can be removed
- Cost anomalies that should be investigated
Example output:
{
"timestamp": "2025-05-28T14:30:00.000Z",
"idleVMs": [
{"resourceId": "/subscriptions/.../vm1", "averageCpu": 2.5}
],
"skuResizes": [
{"resourceId": "/subscriptions/.../vm2", "currentSku": "Standard_D8s_v3", "suggestedSku": "Standard_D4s_v3"}
],
"orphanedDisks": [
{"diskName": "disk1", "ageDays": 45}
],
"costAnomalies": [
{"date": "2025-05-15", "cost": 150.0, "baseline": 100.0}
]
}
For more detailed information, please refer to the following documents:
- Architecture Overview - Solution architecture and component descriptions
- Detailed Usage Guide - Comprehensive instructions and examples
- API Reference - Detailed reference for all classes and methods
- Security Best Practices - Security guidelines for deployment
- Contributing Guidelines - How to contribute to this project
This application supports multiple authentication methods for Azure:
- Service Principal with Client Secret (stored securely in Key Vault)
- Managed Identity (recommended for production)
- DefaultAzureCredential (integrates with VS Code, Azure CLI, etc.)
For production use, we recommend:
- Use Key Vault for all secrets
- Enable diagnostic settings on all resources
- Implement resource locks to prevent accidental deletion
- Apply least-privilege permissions for service principals
Contributions are welcome! Please follow the guidelines in CONTRIBUTING.md
(if available).
This project is licensed under the MIT License.