Azure Management Advisor
The `azure-mgmt-advisor` library is the Microsoft Azure Advisor Client Library for Python. It provides a programmatic interface to interact with Azure Advisor, which offers personalized recommendations to help optimize Azure deployments for cost, performance, reliability, and security. Currently at version 9.0.0, it is part of the Azure SDK for Python, which follows a regular release cadence with updates typically occurring monthly.
Warnings
- breaking The credential system was completely revamped in `9.0.0b1`. Legacy authentication methods like `azure.common.credentials` or `msrestazure.azure_active_directory` are no longer supported. You must use classes from `azure-identity` (e.g., `DefaultAzureCredential`).
- breaking Client initialization parameters changed in `9.0.0b1`. The `credentials` parameter was renamed to `credential`, and the `config` attribute no longer exists on client objects. Configuration should now be passed as keyword arguments to the client constructor.
- breaking Long-running operations (LROs) now return an `azure.core.polling.LROPoller` object (instead of `msrest.polling.LROPoller`) and are typically prefixed with `begin_`.
- breaking The exception hierarchy has been simplified. Most exceptions now inherit from `azure.core.exceptions.HttpResponseError`. The `CloudError` class has been removed.
- gotcha HTTP connection pooling is enabled by default. To ensure proper resource management and avoid potential issues, client objects should ideally be used as context managers or have their `close()` method explicitly called when they are no longer needed.
- gotcha If you are not seeing expected Advisor recommendations for certain categories (e.g., 'Security'), ensure that there are actual recommendations in that category for your resources. You might need to explicitly filter the list operation if only specific categories are desired.
Install
-
pip install azure-mgmt-advisor azure-identity
Imports
- AdvisorManagementClient
from azure.mgmt.advisor import AdvisorManagementClient
- DefaultAzureCredential
from azure.identity import DefaultAzureCredential
Quickstart
import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.advisor import AdvisorManagementClient
# --- Authentication ---
# For more info, see https://learn.microsoft.com/en-us/azure/developer/python/sdk/authentication-overview
# Set AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET, and AZURE_SUBSCRIPTION_ID environment variables,
# or authenticate via Azure CLI (az login).
try:
credential = DefaultAzureCredential()
subscription_id = os.environ.get("AZURE_SUBSCRIPTION_ID", "")
if not subscription_id:
raise ValueError("AZURE_SUBSCRIPTION_ID environment variable not set.")
except Exception as e:
print(f"Authentication setup failed: {e}")
print("Please ensure you have authenticated to Azure (e.g., via `az login`) and set AZURE_SUBSCRIPTION_ID.")
exit(1)
# --- Client Initialization ---
print(f"Initializing AdvisorManagementClient for subscription: {subscription_id}")
client = AdvisorManagementClient(credential, subscription_id)
# --- Fetching Recommendations ---
print("Listing Advisor recommendations:")
try:
# The list() method returns an iterator over recommendations.
for recommendation in client.recommendations.list():
print(f" Category: {recommendation.category}")
print(f" Impact: {recommendation.impact}")
# Accessing nested properties like short_description.problem
if recommendation.short_description:
print(f" Problem: {recommendation.short_description.problem}")
print(f" Solution: {recommendation.short_description.solution}")
print("-" * 20)
except Exception as e:
print(f"Failed to list recommendations: {e}")