Azure REST API through Logic App

Posted by

This post has been republished via RSS; it originally appeared at: Core Infrastructure and Security Blog articles.


As you mature in your cloud journey you may start to be intrigued by the Azure REST API. I feel like sometimes our answer to almost anything is “we could do that through the REST API.” This is a true statement but when we are talking to Infrastructure Engineers a REST API solution might be intimidating. What I want to do in this blog is to use Logic App to make an Azure REST API call with the goal of taking away some of the stigma that you need to be a programmer to utilize API calls!   



Before we jump into Logic App look at our documentation on the Azure REST API: 


Azure REST API reference documentation | Microsoft Docs 


As you can see this library of APIs is extensive and powerful. That’s why you can do almost anything through the Azure REST API. For this blog I’m going to utilize the Virtual Network API for listing your Virtual Networks: 


Virtual Networks - List All (Azure Virtual Networks) | Microsoft Docs 


Most everyone has at least one Virtual Network in there subscription so this is a good one to start with. 


Virtual Network – List All 


We need to understand the API before we can call it. The first thing I always look at is the sample call: 




Couple thing to note from this: 

  • This is a “GET” operation 
  • The values we need to provide will be in the {} block. So for this particular API we need to just provide the Subcription ID we want to query 

The other section that I always look at too is the “Sample” section which will have a sample response. So for this one we should expect the response to look like this: 


{    "value": [      {        "id": "/subscriptions/subid/resourceGroups/rg1/providers/Microsoft.Network/virtualNetworks/vnet1",        "name": "vnet1",        "type": "Microsoft.Network/virtualNetworks",        "location": "westus",        "properties": {          "addressSpace": {            "addressPrefixes": [              ""            ]          },          "dhcpOptions": {            "dnsServers": []          },          "subnets": [            {              "id": "/subscriptions/subid/resourceGroups/rg1/providers/Microsoft.Network/virtualNetworks/vnet1/subnets/test-1",              "name": "test-1",              "properties": {                "addressPrefix": "",                "provisioningState": "Succeeded"              }            }          ],          "virtualNetworkPeerings": [],          "provisioningState": "Succeeded"        }      },      {VNET2},      {VNET3},      {…}    ]  } 


Logic App 

So how do we take advantage of Logic App to call this API? Well let’s start with a blank logic app. Here is some info if you’ve never created one: 


Quickstart - Create your first Logic Apps workflow - Azure portal - Azure Logic Apps | Microsoft Docs 


When you create a Logic App it always brings you to the designer page. You always need a trigger in your logic app and for testing I like to use the “When a HTTP Request is Received” (I can always change it later to something like a schedule). 



This will take you into the designer with the trigger already added. We are going to utilize the managed identity for the Logic App to do the REST API call. This is not enabled by default, so we need to enable it and give it some rights before we go further. Go ahead and save the Logic App in the designer. Then go to “Identity” under the “Settings” for your Logic App. 


Turn the status to “On” and click on “Save” 


Once it finishes saving click on the “Azure role assignments”: 


Click on “Add role assignment” and fill in the following: 

  • Scope: Subscription 
  • Subscription: Whatever subscription you plan to query 
  • Role: Reader 



Click on Save. At this point the Managed Identity has the permissions needed to query the REST API so we can move back to the Logic App Designer. 


In the designer, go ahead and click on the “New Step” below the trigger. 

Search for “http” and select the “HTTP” action 



 Then we can choose the basic HTTP Action 



Let’s go ahead and add the authentication before we fill anything out. Click on “Add new parameter” and check the box next to “Authentication”. You’ll need to click somewhere outside of the drop down to add it in so click for instance the “URI” field.  



Now we can fill out the required fields to call the REST API 

***Replace {subscriptionId} with the subscription id you want to query*** 

  • Authentication type: Managed identity 



Go ahead and save your Logic App and then click on Run. 



You should see the HTTP call succeed and in the “Body” section you should see your VNets. 



Congrats! You have authenticated to an Azure Rest API, made a request, and successfully gotten results. 


Using the Results 

I don’t want to leave you with just the results in the body of the HTTP action. Let’s actually do something with the results in Logic Apps. I would typically do some parsing of the JSON but to keep this simple being the focus here is the REST API and not Logic App we will keep the parsing of the JSON to a minimum. 


Go back into the Logic App Designer and click on “New Step” below the HTTP action.  

Search for “parse json” and then under the “Actions” section choose “Parse JSON 



 Click in the “Content” section and then from the “Dynamic content” choose “Body” 



 Click on the option to “Use sample payload to generate schema” 



 In the box that pops up you can paste in the below: 


***TIP: You can get the sample payload from the previous run we did. You can just grab the body from the “Output”*** 


{   "value": [      {        "id": "/subscriptions/subid/resourceGroups/rg1/providers/Microsoft.Network/virtualNetworks/vnet1",        "name": "vnet1",        "type": "Microsoft.Network/virtualNetworks",        "location": "westus",        "properties": {          "addressSpace": {            "addressPrefixes": [              ""            ]          },          "dhcpOptions": {            "dnsServers": []          },          "subnets": [            {              "id": "/subscriptions/subid/resourceGroups/rg1/providers/Microsoft.Network/virtualNetworks/vnet1/subnets/test-1",              "name": "test-1",              "properties": {                "addressPrefix": "",                "provisioningState": "Succeeded"              }            }          ],          "virtualNetworkPeerings": [],          "provisioningState": "Succeeded"        }      }    ]  } 


Click “Done” and it’ll generate the schema for you. 


Click on “New Step” below the “Parse JSON” 


Search for “create html table” and then under the “Actions” section choose “Create HTML table 



 Click in the “From” field and in “Dynamic content” search for “value”. Choose the “value” field from “Parse JSON” 



We now have an HTML table lets send an email with it attached. 


Click on “+ New Step” below this activity, search for “send an email (v2)”, and choose the Office 365 Outlook action named “Send an email (V2)”  



Rename the Action to “Email HTML Report” and fill out the following:  

  • Body: Whatever you wish for the Body of the email  
  • Subject: Whatever you wish for the Subject of the email  
  • To: Fill out the emails you wish to receive the report  
  • Click on “Add new parameter” and choose “Attachment” 
  • Attachment Content: from the “Dynamic content” choose “Output” under “Create HTML table”  
  • Attachment Name: Something like “VNets.html”  



 That’s it for the Logic App. You should now click on “Save” and once the Logic App is saved click on “Run”. Look for an email with the HTML attachment. 



In this blob we showed how to use Logic App to query the Azure Rest API, take the output and make a HTML report, then email that report as an attachment. The focus was on calling the REST API and not the report (there is more formatting that I’d do with that report but didn’t want to focus on that). So hopefully you see from this example that you don’t need to be a programmer to call Azure REST APIs. Logic App can take care of the authentication and we can focus on just formatting the request correctly! As an Azure Administrator this can open the doors for a lot of automation that you always wanted but never thought was possible. 

This articles are republished, there may be more discussion at the original link. But if you found this helpful, you're more than welcome to let us know!

This site uses Akismet to reduce spam. Learn how your comment data is processed.