Quickstart: Run your first Resource Graph query using Python
The first step to using Azure Resource Graph is to check that the required libraries for Python are installed. This quickstart walks you through the process of adding the libraries to your Python installation.
At the end of this process, you'll have added the libraries to your Python installation and run your first Resource Graph query.
Prerequisites
If you don't have an Azure subscription, create a free account before you begin.
Use Azure Cloud Shell
Azure hosts Azure Cloud Shell, an interactive shell environment that you can use through your browser. You can use either Bash or PowerShell with Cloud Shell to work with Azure services. You can use the Cloud Shell preinstalled commands to run the code in this article, without having to install anything on your local environment.
To start Azure Cloud Shell:
| Option | Example/Link |
|---|---|
| Select Try It in the upper-right corner of a code block. Selecting Try It doesn't automatically copy the code to Cloud Shell. | ![]() |
| Go to https://shell.azure.com, or select the Launch Cloud Shell button to open Cloud Shell in your browser. | ![]() |
| Select the Cloud Shell button on the menu bar at the upper right in the Azure portal. | ![]() |
To run the code in this article in Azure Cloud Shell:
Start Cloud Shell.
Select the Copy button on a code block to copy the code.
Paste the code into the Cloud Shell session by selecting Ctrl+Shift+V on Windows and Linux, or by selecting Cmd+Shift+V on macOS.
Select Enter to run the code.
Add the Resource Graph library
To enable Python to query Azure Resource Graph, the library must be added. This library works wherever Python can be used, including bash on Windows 10 or locally installed.
Check that the latest Python is installed (at least 3.8). If it isn't yet installed, download it at Python.org.
Check that the latest Azure CLI is installed (at least 2.5.1). If it isn't yet installed, see Install the Azure CLI.
Note
Azure CLI is required to enable Python to use the CLI-based authentication in the following examples. For information about other options, see Authenticate using the Azure management libraries for Python.
Authenticate through Azure CLI.
az loginIn your Python environment of choice, install the required libraries for Azure Resource Graph:
# Add the Resource Graph library for Python pip install azure-mgmt-resourcegraph # Add the Resources library for Python pip install azure-mgmt-resource # Add the CLI Core library for Python for authentication (development only!) pip install azure-cli-core # Add the Azure identity library for Python pip install azure.identityNote
If Python is installed for all users, these commands must be run from an elevated console.
Validate that the libraries have been installed.
azure-mgmt-resourcegraphshould be 2.0.0 or higher,azure-mgmt-resourceshould be 9.0.0 or higher, andazure-cli-coreshould be 2.5.0 or higher.# Check each installed library pip show azure-mgmt-resourcegraph azure-mgmt-resource azure-cli-core azure.identity
Run your first Resource Graph query
With the Python libraries added to your environment of choice, it's time to try out a simple
subscription-based Resource Graph query. The query returns the first five Azure resources with the
Name and Resource Type of each resource. To query by
management group, use the management_groups parameter with
QueryRequest.
Run your first Azure Resource Graph query using the installed libraries and the
resourcesmethod:# Import Azure Resource Graph library import azure.mgmt.resourcegraph as arg # Import specific methods and models from other libraries from azure.mgmt.resource import SubscriptionClient from azure.identity import AzureCliCredential # Wrap all the work in a function def getresources( strQuery ): # Get your credentials from Azure CLI (development only!) and get your subscription list credential = AzureCliCredential() subsClient = SubscriptionClient(credential) subsRaw = [] for sub in subsClient.subscriptions.list(): subsRaw.append(sub.as_dict()) subsList = [] for sub in subsRaw: subsList.append(sub.get('subscription_id')) # Create Azure Resource Graph client and set options argClient = arg.ResourceGraphClient(credential) argQueryOptions = arg.models.QueryRequestOptions(result_format="objectArray") # Create query argQuery = arg.models.QueryRequest(subscriptions=subsList, query=strQuery, options=argQueryOptions) # Run query argResults = argClient.resources(argQuery) # Show Python object print(argResults) getresources("Resources | project name, type | limit 5")Note
As this query example does not provide a sort modifier such as
order by, running this query multiple times is likely to yield a different set of resources per request.Update the call to
getresourcesand change the query toorder bythe Name property:getresources("Resources | project name, type | limit 5 | order by name asc")Note
Just as with the first query, running this query multiple times is likely to yield a different set of resources per request. The order of the query commands is important. In this example, the
order bycomes after thelimit. This command order first limits the query results and then orders them.Update the call to
getresourcesand change the query to firstorder bythe Name property and thenlimitto the top five results:getresources("Resources | project name, type | order by name asc | limit 5")
When the final query is run several times, assuming that nothing in your environment is changing, the results returned are consistent and ordered by the Name property, but still limited to the top five results.
Clean up resources
If you wish to remove the installed libraries from your Python environment, you can do so by using the following command:
# Remove the installed libraries from the Python environment
pip uninstall azure-mgmt-resourcegraph azure-mgmt-resource azure-cli-core azure.identity
Next steps
In this quickstart, you've added the Resource Graph libraries to your Python environment and run your first query. To learn more about the Resource Graph language, continue to the query language details page.
Povratne informacije
Pošalјite i prikažite povratne informacije za


