If you haven't already, set up an environment where you can run this code. Here are some options:
Configure a Python virtual environment using venv or your tool of choice. You can create the virtual environment locally or in Azure Cloud Shell and run the code there. Be sure to activate the virtual environment to start using it.
Create a file named requirements.txt with the following contents:
azure-mgmt-resource
azure-identity
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 DefaultAzureCredential
from azure.mgmt.resource import ResourceManagementClient
import os
# Acquire a credential object.
credential = DefaultAzureCredential()
# 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 DefaultAzureCredential
from azure.mgmt.resource import ResourceManagementClient
import os
# Acquire a credential object.
credential = DefaultAzureCredential()
# 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
Later in this article, you sign in to Azure with the Azure CLI to run the sample code. If your account has permissions to create and list resource groups in your Azure subscription, the code will run successfully.
To use such code in a production script, you can set environment variables to use a service principal-based method for authentication. To learn more, see How to authenticate Python apps with Azure services. You need to ensure that the service principal has sufficient permissions to create and list resource groups in your subscription by assigning it an appropriate role in Azure; for example, the Contributor role on your subscription.
If you haven't already, sign in to Azure using the Azure CLI:
az login
Set the AZURE_SUBSCRIPTION_ID environment variable to your subscription ID. (You can run the az account show command and get your subscription ID from the id property in the output):
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.
For reference: equivalent Azure CLI commands
The following Azure CLI command lists resource groups in a subscription:
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