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.


If you don't have an Azure subscription, create a free account before you begin.

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 or command block. Selecting Try It doesn't automatically copy the code or command to Cloud Shell. Screenshot that shows an example of Try It for Azure Cloud Shell.
Go to https://shell.azure.com, or select the Launch Cloud Shell button to open Cloud Shell in your browser. Screenshot that shows how to launch Cloud Shell in a new window.
Select the Cloud Shell button on the menu bar at the upper right in the Azure portal. Screenshot that shows the Cloud Shell button in the Azure portal

To use Azure Cloud Shell:

  1. Start Cloud Shell.

  2. Select the Copy button on a code block (or command block) to copy the code or command.

  3. Paste the code or command into the Cloud Shell session by selecting Ctrl+Shift+V on Windows and Linux, or by selecting Cmd+Shift+V on macOS.

  4. Select Enter to run the code or command.

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.

  1. Check that the latest Python is installed (at least 3.8). If it isn't yet installed, download it at Python.org.

  2. Check that the latest Azure CLI is installed (at least 2.5.1). If it isn't yet installed, see Install the Azure CLI.


    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.

  3. Authenticate through Azure CLI.

    az login
  4. In 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.identity


    If Python is installed for all users, these commands must be run from an elevated console.

  5. Validate that the libraries have been installed. azure-mgmt-resourcegraph should be 2.0.0 or higher, azure-mgmt-resource should be 9.0.0 or higher, and azure-cli-core should 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.

  1. Run your first Azure Resource Graph query using the installed libraries and the resources method:

    # 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():
        subsList = []
        for sub in subsRaw:
        # 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
    getresources("Resources | project name, type | limit 5")


    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.

  2. Update the call to getresources and change the query to order by the Name property:

    getresources("Resources | project name, type | limit 5 | order by name asc")


    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 by comes after the limit. This command order first limits the query results and then orders them.

  3. Update the call to getresources and change the query to first order by the Name property and then limit to 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.