Example: Use the Azure libraries to list resource groups and resources

This example demonstrates how to use the Azure SDK management libraries in a Python script to perform two tasks:

  • List all the resource groups in an Azure subscription.
  • List resources within a specific resource group.

All the commands in this article work the same in Linux/macOS bash and Windows command shells unless noted.

The Equivalent Azure CLI command is given later in this article.

1: Set up your local development environment

If you haven't already, follow all the instructions on Configure your local Python dev environment for Azure.

Be sure to create and activate a virtual environment for this project.

2: Install the Azure library packages

Create a file named requirements.txt with the following contents:

azure-mgmt-resource>=18.0.0
azure-identity>=1.5.0

Be sure to use these versions of the libraries. Using older versions will result in errors such as "'AzureCliCredential' object object has no attribute 'signed_session'."

In a terminal or command prompt with the virtual environment activated, install the requirements:

pip install -r requirements.txt

3: Write code to work with resource groups

3a. List resource groups in a subscription

Create a Python file named list_groups.py with the following code. The comments explain the details:

# Import the needed credential and management objects from the libraries.
from azure.identity import AzureCliCredential
from azure.mgmt.resource import ResourceManagementClient
import os

# Acquire a credential object using CLI-based authentication.
credential = AzureCliCredential()

# Retrieve subscription ID from environment variable.
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]

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

# Retrieve the list of resource groups
group_list = resource_client.resource_groups.list()

# Show the groups in formatted output
column_width = 40

print("Resource Group".ljust(column_width) + "Location")
print("-" * (column_width * 2))

for group in list(group_list):
    print(f"{group.name:<{column_width}}{group.location}")

3b. List resources within a specific resource group

Create a Python file named list_resources.py with the following code. The comments explain the details.

By default, the code lists resources in "myResourceGroup". To use a different resource group, set the RESOURCE_GROUP_NAME environment variable to the desired group name.

# Import the needed credential and management objects from the libraries.
from azure.identity import AzureCliCredential
from azure.mgmt.resource import ResourceManagementClient
import os

# Acquire a credential object using CLI-based authentication.
credential = AzureCliCredential()

# Retrieve subscription ID from environment variable.
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]

# Retrieve the resource group to use, defaulting to "myResourceGroup".
resource_group = os.getenv("RESOURCE_GROUP_NAME", "myResourceGroup")

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

# Retrieve the list of resources in "myResourceGroup" (change to any name desired).
# The expand argument includes additional properties in the output.
resource_list = resource_client.resources.list_by_resource_group(
    resource_group, expand = "createdTime,changedTime")

# Show the groups in formatted output
column_width = 36

print("Resource".ljust(column_width) + "Type".ljust(column_width)
    + "Create date".ljust(column_width) + "Change date".ljust(column_width))
print("-" * (column_width * 4))

for resource in list(resource_list):
    print(f"{resource.name:<{column_width}}{resource.type:<{column_width}}"
       f"{str(resource.created_time):<{column_width}}{str(resource.changed_time):<{column_width}}")

Authentication in the code

This code uses CLI-based authentication (using AzureCliCredential) because it demonstrates actions that you might otherwise do with the Azure CLI directly. In both cases, you're using the same identity for authentication. Depending on your environment, you may need to run az login first to authenticate.

To use such code in a production script (for example, to automate VM management), use DefaultAzureCredential (recommended) or a service principal based method as described in How to authenticate Python apps with Azure services.

4: Run the scripts

List all resources groups in the subscription:

python list_groups.py

List all resources in a resource group:

python list_resources.py

For reference: equivalent Azure CLI commands

The following Azure CLI command lists resource groups in a subscription using JSON output:

az group list

The following command lists resources within the "myResourceGroup" in the centralus region (the location argument is necessary to identify a specific data center):

az resource list --resource-group myResourceGroup --location centralus

See also