|
| 1 | +--- |
| 2 | +title: "Tutorial: Automate Running Your Resource Graph Queries within Logic Apps" |
| 3 | +description: In this tutorial, you will learn how to call Resource Graph in a Logic App |
| 4 | +ms.date: 11/18/2021 |
| 5 | +ms.topic: tutorial |
| 6 | +--- |
| 7 | +# Tutorial: Run Azure Resource Graph Queries in an Azure Logic App |
| 8 | + |
| 9 | +Azure Resource Graph Explorer lets you query your resources at scale, across your subscriptions, management groups, and your entire tenant. |
| 10 | + |
| 11 | +If you need to query your resources periodically to check for specific resource or management properties and act on the results, you can utilize Azure Logic Apps. |
| 12 | + |
| 13 | +In this tutorial, you will learn how to: |
| 14 | + |
| 15 | +> [!div class="checklist"] |
| 16 | +> - Write an Azure Resource Graph query that you plan to run periodically |
| 17 | +> - Create an Azure Logic App with a System Assigned Managed Identity |
| 18 | +> - Setup a Managed Identity to access specific resources, resource groups, and subscriptions |
| 19 | +> - Automate your Azure Resource Graph query execution by calling your Logic App periodically |
| 20 | +
|
| 21 | +## Prerequisites |
| 22 | + |
| 23 | +To complete this tutorial, you need an Azure subscription. If you don't have one, create a |
| 24 | +[free account](https://azure.microsoft.com/free/) before you begin. |
| 25 | + |
| 26 | +## Write an Azure Resource Graph query |
| 27 | + |
| 28 | +If you have an Azure Resource Graph query that you want to run periodically, you may use that. In this tutorial, we're using the following query to retrieve the power state summary of your Virtual Machines. |
| 29 | + |
| 30 | + ```kusto |
| 31 | + Resources |
| 32 | + | where type =~ 'microsoft.compute/virtualmachines' |
| 33 | + | extend vmPowerState = tostring(properties.extended.instanceView.powerState.code) |
| 34 | + | summarize count() by vmPowerState |
| 35 | + ``` |
| 36 | + |
| 37 | + For more details, see |
| 38 | + [Samples – Summarize virtual machine by power state](../samples/advanced.md#vm-powerstate). |
| 39 | + |
| 40 | +Keep the query above handy as we will need it later when we configure our Logic App. |
| 41 | + |
| 42 | +## Create a Logic App |
| 43 | + |
| 44 | +1. From the portal menu, select **Logic Apps**, or use the Azure search box at the top of all |
| 45 | + pages to search for and select **Logic Apps**. |
| 46 | + |
| 47 | +2. Click the **Create** button on upper left of your screen and continue with creating your Logic App. |
| 48 | + |
| 49 | +## Setup a Managed Identity |
| 50 | + |
| 51 | +### Create a New System-Assigned Managed Identity |
| 52 | + |
| 53 | +Within the Azure Portal, navigate to the Logic App you created. Select **Identity** on the left hand side of the page. Then, select the system-assigned identity button, set the status to **On**, and click **Save**. |
| 54 | + |
| 55 | +### Add Role Assignments to your Managed Identity |
| 56 | + |
| 57 | +To give the newly created Managed Identity ability to query across your subscriptions, resource groups, and resources so your queries - you need to assign access via Role Assignments. For details on how to assign Role Assignments for Managed Identities, reference: [Assign Azure roles to a managed identity](../../../role-based-access-control/role-assignments-portal-managed-identity.md) |
| 58 | + |
| 59 | +## Configure and Run Your Logic App |
| 60 | + |
| 61 | +In the code view of your Logic App within Azure Portal, paste: |
| 62 | + |
| 63 | +```json |
| 64 | +{ |
| 65 | + "definition": { |
| 66 | + "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#", |
| 67 | + "actions": { |
| 68 | + "HTTP_2": { |
| 69 | + "inputs": { |
| 70 | + "authentication": { |
| 71 | + "type": "ManagedServiceIdentity" |
| 72 | + }, |
| 73 | + "body": { |
| 74 | + "query": "Resources | where type =~ 'microsoft.compute/virtualmachines' | extend vmPowerState = tostring(properties.extended.instanceView.powerState.code) | summarize count() by vmPowerState" |
| 75 | + }, |
| 76 | + "headers": { |
| 77 | + "Content-Type": "application/json" |
| 78 | + }, |
| 79 | + "method": "POST", |
| 80 | + "queries": { |
| 81 | + "api-version": "2021-03-01" |
| 82 | + }, |
| 83 | + "uri": "https://management.azure.com/providers/Microsoft.ResourceGraph/resources" |
| 84 | + }, |
| 85 | + "runAfter": {}, |
| 86 | + "type": "Http" |
| 87 | + } |
| 88 | + }, |
| 89 | + "contentVersion": "1.0.0.0", |
| 90 | + "outputs": {}, |
| 91 | + "parameters": {}, |
| 92 | + "triggers": { |
| 93 | + "Recurrence": { |
| 94 | + "recurrence": { |
| 95 | + "frequency": "Minute", |
| 96 | + "interval": 1440 |
| 97 | + }, |
| 98 | + "type": "Recurrence" |
| 99 | + } |
| 100 | + } |
| 101 | + }, |
| 102 | + "parameters": {} |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +Then, go into the designer view of your Logic App within Azure Portal and modify your set up as you see fit. |
| 107 | + |
| 108 | +Finally, save your Logic App and run it. |
| 109 | + |
| 110 | +## Next steps |
| 111 | + |
| 112 | +In this tutorial, we've created an Azure Logic App that automates your ARG query requests at a set interval. To learn more about the Resource graph |
| 113 | +language, continue to the query language details page linked below and try out more Azure Resource Graph queries. |
| 114 | + |
| 115 | +If you have questions, please contact [email protected] |
| 116 | + |
| 117 | +> [!div class="nextstepaction"] |
| 118 | +> [Get more information about the query language](../concepts/query-language.md) |
0 commit comments