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!
Azure REST API
Before we jump into Logic App look at our documentation on the Azure REST API:
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:
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:
Subscription: Whatever subscription you plan to query
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
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’sactually 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”***
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.