Azure Optimizer is a Python-based tool used to optimize Azure resource costs by applying various policies to resources across multiple subscriptions. It identifies resources that meet specific criteria and applies actions such as scaling, stopping, or deleting them to reduce costs. The tool also provides detailed reports and logs the financial impact of the applied policies.
- Apply Policies: Apply predefined policies to resources, such as stopping unused VMs, deleting unattached disks, scaling SQL databases, etc.
- Stop unused virtual machines based on tags and usage.
- Delete unattached disks based on tags. 1
- Delete all resources in a resource group based on tags.
- Delete idle Application Gateway.
- Delete unattached Public IP Addresses.
- Delete unattached Network Interfaces.
- SQL DTU Scaling: Dynamically scale Azure SQL databases based on defined policies and schedules.
- Storage Account SKU change: Update Storage Account SKU to Standard_LRS for specific SKUs and tags
- Downgrade disks of deallocated VM's: Automatically downgrade disks of deallocated VM's.
- Analyze cost data for trends and anomalies.
- Generate summary reports.
- Multi-Subscription Support: Process multiple subscriptions within a tenant.
- Application Insights Integration: Track events and metrics in Azure Application Insights. (Work in progress)
-
Stop Unused VMs
name: stop-unused-vms resource: azure.vm filters: - type: last_used days: 30 threshold: 5 actions: - type: stop
-
Delete Unattached Disks
name: delete-unused-disks resource: azure.disk filters: - type: unattached actions: - type: delete
-
Delete Resources in Group
name: delete-resources-in-group resource: azure.resourcegroup filters: - type: tag key: delete value: true actions: - type: delete
-
Delete Idle Application Gateway
name: delete-idle-application-gateway resource: azure.applicationgateway filters: - type: empty_backend_pools actions: - type: delete
-
Delete Unattached Public IPs
name: delete-unattached-public-ips resource: azure.publicip filters: - type: unattached actions: - type: delete
-
Delete Unattached Network Interfaces
name: delete-unattached-nics resource: azure.nic filters: - type: unattached actions: - type: delete
-
Scale SQL Database DTU
name: scale-sql-database-dtu resource: azure.sql actions: - type: scale_sql_database tiers: - name: Standard min_dtu: 10 max_dtu: 100 off_peak_dtu: 50 peak_dtu: 100
-
Storage Account SKU change
name: update-storage-account-sku description: Update Storage Account SKU to Standard_LRS for specific SKUs and tags resource: "azure.storage" filters: - type: "sku" values: ["Standard_GRS", "Standard_RAGRS", "Standard_ZRS", "Standard_GZRS"] - type: "tag" key: "costopt" value: "true"
- Azure Subscription: Ensure you have an active Azure subscription.
- Azure CLI: Install the Azure CLI for authentication and managing Azure resources.
- Python 3.8+: Ensure Python 3.8 or later is installed on your machine.
- Azure SDK for Python
- Configuration file (config.yaml)
-
Clone the repository:
git clone https://github.com/jamelachahbar/CostOptTool.git cd CostOptTool -
Install dependencies:
pip install -r requirements.txt
-
Configure your Azure credentials and subscription ID:
- Set the environment variables
AZURE_SUBSCRIPTION_ID,APPINSIGHTS_INSTRUMENTATIONKEY,AZURE_CLIENT_ID,AZURE_TENANT_ID, andAZURE_CLIENT_SECRETin your environment.
- Set the environment variables
-
Define your policies in
policies/policies.yaml.
To run the script locally, ensure that you have set the required environment variables for Azure authentication. You can set these variables in your shell or create a .env file.
Example of setting environment variables in the shell:
export AZURE_SUBSCRIPTION_ID="your_subscription_id"
export APPINSIGHTS_INSTRUMENTATIONKEY="your_application_insights_instrumentation_key"
export AZURE_CLIENT_ID="your_client_id"
export AZURE_TENANT_ID="your_tenant_id"
export AZURE_CLIENT_SECRET="your_client_secret"Execute the main script to apply your policies:
Dry run mode can be used to see if any action will be taken before actually applying actions:
python src/main.py --mode dry-runpython src/main.py --mode dry-run [--all-subscriptions]- --mode: Mode to run the tool (dry-run or apply)
- --all-subscriptions: Process all subscriptions in the tenant
Example
Dry run mode for a single subscription:
python src/main.py --mode dry-runApply mode for all subscriptions:
python src/main.py --mode apply --all-subscriptionsWork In Progress --> Apply mode for all subscriptions + getting cost from adls:
python src/main.py --mode apply --all-subscriptions --use-adlsThe tool provides detailed output, including:
- Cost Analysis Report: Trend and anomaly detection in cost data.
- Policy Application Summary: Summary of impacted and non-impacted resources for each policy.
- Operation Status: Detailed status of each operation performed on the resources.
- Subscription Details: Outputs are clearly labeled with subscription IDs for clarity.
Example Output
_______ _______ _______ _ _
(_______) (_______) _ (_______) _ (_) (_)
_______ _____ _ _ ____ _____ _ ___ ___ _| |_ _ _ ____ _| |_ _ ____ _ _____ _____ ____
| ___ (___ ) | | |/ ___) ___ | | | / _ \ /___|_ _) | | | | _ (_ _) | \| (___ ) ___ |/ ___)
| | | |/ __/| |_| | | | ____| | |____| |_| |___ | | |_ | |___| | |_| || |_| | | | | |/ __/| ____| |
|_| |_(_____)____/|_| |_____) \______)___/(___/ \__) \_____/| __/ \__)_|_|_|_|_(_____)_____)_|
|_|
Running Azure Cost Optimizer Tool...
Please wait...
Azure Cost Optimizer Tool is ready!
==============================================================================================================
Cost Analysis for Subscription ID: <subscription_id>
+----------------------------+------------+---------+
| Anomaly Detected | Date | Cost |
+----------------------------+------------+---------+
| Isolation Forest Algorithm | 2024-06-04 | €218.72 |
+----------------------------+------------+---------+
Impacted Resources for Subscription ID: <subscription_id>
+------------------------+---------------+---------+
| Policy | Resource | Actions |
+------------------------+---------------+---------+
| scale-sql-database-dtu | master | scale |
| scale-sql-database-dtu | costopttestdb | scale |
| scale-sql-database-dtu | onpremdgwsql | scale |
+------------------------+---------------+---------+
Non-Impacted Resources for Subscription ID: <subscription_id>
+--------------------------------+---------------------+
| Policy | Resource Type |
+--------------------------------+---------------------+
| stop-unused-vms | VM |
| delete-unused-disks | Disk |
| delete-unattached-public-ips | Public IP |
| delete-resources-in-group | Resource Group |
| update-storage-account-sku | Storage Account |
| delete-empty-backend-pool-app- | Application Gateway |
| gateways | |
+--------------------------------+---------------------+
Operation Status for Subscription ID: <subscription_id>
+---------------+--------+---------+--------------------------------+
| Resource | Action | Status | Message |
+---------------+--------+---------+--------------------------------+
| costopttestdb | scale | Dry Run | Dry run mode, no action taken. |
| | | | Would scale DTU to 50 |
| onpremdgwsql | scale | Dry Run | Dry run mode, no action taken. |
| | | | Would scale DTU to 50 |
+---------------+--------+---------+--------------------------------+
Azure Cost Optimizer Tool completed!
==============================================================================================================