DEV Community

holger
holger

Posted on

How to use the Azure SDK for Python to retrieve a list of Azure Resources

Introduction

The Azure SDK for Python [1] is one of many ways to connect to Azure REST APIs and extract information. Here is an example on how to retrieve all resources from an Azure subscription, put the data into a Pandas DataFrame [2] and plot a simple pie chart using matplotlib [3].

Import Libraries

We need to import the following libraries.

# Import libraries
from azure.identity import AzureCliCredential
from azure.mgmt.resource import ResourceManagementClient
import pandas as pd
import matplotlib
Enter fullscreen mode Exit fullscreen mode

Acquire Credentials and obtain the Management Object

When using AzureCliCredential(), Python will use the Azure CLI context of the currently logged-in user for executing the queries.

# Acquire credential
credential = AzureCliCredential()

# Define Scope
subscription_id = "subscription_id"

# Obtain the management object for resources.
resource_client = ResourceManagementClient(credential, subscription_id)
Enter fullscreen mode Exit fullscreen mode

Create and Populate a Dictionary

Using resource_client.resources.list() through the resource client will retrieve an interable object with all Azure resources in the given subscription. We can now populate a Python dictionary with the resources and add them to a Pandas DataFrame.

resources = resource_client.resources.list()
dict = []
for resource in resources:
    dict.append(resource.as_dict())
Enter fullscreen mode Exit fullscreen mode

Create a DataFrame from the Dictionary

df = pd.DataFrame.from_dict(dict)
Enter fullscreen mode Exit fullscreen mode

Select only the Top 10 rows based on the Resource Type Count

type_df = df['type'].value_counts()
type_df_top = type_df.head(10)
Enter fullscreen mode Exit fullscreen mode

Finally plot the Pie Chart

plot = type_df_top.plot.pie(figsize=(10, 10), autopct= lambda x: '{:.0f}'.format(x*type_df_top.sum()/100))
Enter fullscreen mode Exit fullscreen mode

... which might look like something along these lines.
Pie Chart

Summary

This short article has scratched the surface on what is possible when using the Azure SDK for Python since it allows as to combine the power of Python with the capabilities of the Azure REST API.
We have extracted a list of Azure resources based on a given context, added them to a Pandas DataFrame and plotted a simple pie chart based on the top 10 resource count.

References

Top comments (0)