Azure Management Advisor

9.0.0 · active · verified Thu Apr 09

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

Install

Imports

Quickstart

This quickstart demonstrates how to authenticate with Azure using `DefaultAzureCredential` and list Azure Advisor recommendations for a given subscription. Ensure your Azure credentials and `AZURE_SUBSCRIPTION_ID` are set in your environment variables or via `az login`.

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}")

view raw JSON →