Quickstart: Run your first Resource Graph query using JavaScript
This quickstart walks you through the process of adding the libraries to your JavaScript installation. The first step to using Azure Resource Graph is to initialize a JavaScript application with the required libraries.
At the end of this process, you'll have added the libraries to your JavaScript installation and run your first Resource Graph query.
Prerequisites
Azure subscription: If you don't have an Azure subscription, create a free account before you begin.
Node.js: Node.js version 12 or higher is required.
Application initialization
To enable JavaScript to query Azure Resource Graph, the environment must be configured. This setup works wherever JavaScript can be used, including bash on Windows 10.
Initialize a new Node.js project by running the following command.
npm init -yAdd a reference to the yargs module.
npm install yargsAdd a reference to the Azure Resource Graph module.
npm install @azure/arm-resourcegraphAdd a reference to the Azure authentication library.
npm install @azure/identityNote
Verify in package.json
@azure/arm-resourcegraphis version 4.2.1 or higher and@azure/identityis version 2.0.4 or higher.
Query the Resource Graph
Create a new file named index.js and enter the following code.
const argv = require("yargs").argv; const { DefaultAzureCredential } = require("@azure/identity"); const { ResourceGraphClient } = require("@azure/arm-resourcegraph"); if (argv.query) { const query = async () => { const credentials = new DefaultAzureCredential(); const client = new ResourceGraphClient(credentials); const result = await client.resources( { query: argv.query }, { resultFormat: "table" } ); console.log("Records: " + result.totalRecords); console.log(result.data); }; query(); }Note
This code creates a tenant-based query. To limit the query to a management group or subscription, define and add a queryrequest to the
client.resourcescall and specify eithermanagementGroupsorsubscriptions.Enter the following command in the terminal:
node index.js --query "Resources | project name, type | limit 5"Note
As this query example doesn't provide a sort modifier such as
order by, running this query multiple times is likely to yield a different set of resources per request.Change the first parameter to
index.jsand change the query toorder bythe Name property.node index.js --query "Resources | project name, type | limit 5 | order by name asc"As the script attempts to authenticate, a message similar to the following message is displayed in the terminal:
To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code FGB56WJUGK to authenticate.
Once you authenticate in the browser, then the script continues to run.
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.Change the first parameter to
index.jsand change the query to firstorder bythe Name property and thenlimitto the top five results.node index.js --query "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 application, run the following command.
npm uninstall @azure/arm-resourcegraph @azure/identity yargs
Next steps
In this quickstart, you've added the Resource Graph libraries to your JavaScript 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