Quickstart: Run your first Resource Graph query using Ruby

The first step to using Azure Resource Graph is to check that the required gems for Ruby are installed. This quickstart walks you through the process of adding the gems to your Ruby installation.

At the end of this process, you'll have added the gems to your Ruby installation and run your first Resource Graph query.

Prerequisites

  • If you don't have an Azure subscription, create a free account before you begin.
  • An Azure service principal, including the clientId and clientSecret.

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. 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 run the code in this article in Azure Cloud Shell:

  1. Start Cloud Shell.

  2. Select the Copy button on a code block to copy the code.

  3. 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.

  4. Select Enter to run the code.

Create the Resource Graph project

To enable Ruby to query Azure Resource Graph, the gem must be added to the Gemfile. This gem works wherever Ruby can be used, including with Azure Cloud Shell, bash on Windows 10, or locally installed.

  1. Check that the latest Ruby is installed (at least 2.7.1). If it isn't yet installed, download it at Ruby-Lang.org.

  2. In your Ruby environment of choice, initialize a bundle in a new project folder:

    # Initialize a bundle to create a new Gemfile
    bundle init
    
  3. Update your Gemfile with the gems needed for Azure Resource Graph. The updated file should look similar to this example:

    # frozen_string_literal: true
    
    source "https://rubygems.org"
    
    git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
    
    # gem "rails"
    gem 'azure_mgmt_resourcegraph', '~> 0.17.2'
    
  4. From the project folder, run bundle install. Confirm the gems were installed with bundle list.

  5. In the same project folder, create argQuery.rb with the following code and save the updated file:

    #!/usr/bin/env ruby
    
    require 'azure_mgmt_resourcegraph'
    ARG = Azure::ResourceGraph::Profiles::Latest::Mgmt
    
    # Get arguments and set options
    options = {
        tenant_id: ARGV[0],
        client_id: ARGV[1],
        client_secret: ARGV[2],
        subscription_id: ARGV[3]
    }
    
    # Create Resource Graph client from options
    argClient = ARG::Client.new(options)
    
    # Create Resource Graph QueryRequest for subscription with query
    query_request = ARGModels::QueryRequest.new.tap do |qr|
        qr.subscriptions = [ARGV[3]]
        qr.query = ARGV[4]
    end
    
    # Get the resources from Resource Graph
    response = argClient.resources(query_request)
    
    # Convert data to JSON and output
    puts response.data.to_json
    

Run your first Resource Graph query

With the Ruby script saved and ready to use, it's time to try out a simple Resource Graph query. The query returns the first five Azure resources with the Name and Resource Type of each resource.

In each call to argQuery, there are variables that are used that you need to replace with your own values:

  • {tenantId} - Replace with your tenant ID
  • {clientId} - Replace with the client ID of your service principal
  • {clientSecret} - Replace with the client secret of your service principal
  • {subscriptionId} - Replace with your subscription ID
  1. Change directories to the project folder where you created the Gemfile and argClient.rb files.

  2. Run your first Azure Resource Graph query using the gems and the resources method:

    ruby argQuery.rb "{tenantId}" "{clientId}" "{clientSecret}" "{subscriptionId}" "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.

  3. Change the final parameter to argQuery.rb and change the query to order by the Name property:

    ruby argQuery.rb "{tenantId}" "{clientId}" "{clientSecret}" "{subscriptionId}" "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 by comes after the limit. This command order first limits the query results and then orders them.

  4. Change the final parameter to argQuery.rb and change the query to first order by the Name property and then limit to the top five results:

    ruby argQuery.rb "{tenantId}" "{clientId}" "{clientSecret}" "{subscriptionId}" "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 gems from your Ruby environment, you can do so by using the following command:

# Remove the installed gems from the Ruby environment
gem uninstall azure_mgmt_resourcegraph

Note

The gem azure_mgmt_resourcegraph has dependencies such as ms_rest and ms_rest_azure that may have also been installed depending on your environment. You may uninstall these gems also if no longer needed.

Next steps

In this quickstart, you've added the Resource Graph gems to your Ruby environment and run your first query. To learn more about the Resource Graph language, continue to the query language details page.