DEV Community

Cover image for Azure Nibble - Hosts File Generator using KQL for App Services
Marcel.L
Marcel.L

Posted on • Updated on

Azure Nibble - Hosts File Generator using KQL for App Services

Azure Resource Graph

Azure Resource Graph allows us to quickly and efficiently query across Azure subscriptions. Analyse cloud inventory using complex queries launched programmatically or from the Azure portal. The query language used is known as Kusto Query Lanuage (KQL).

Hosts File Generator

Recently I was looking into a mechanism to generate a hosts file to add a very large number of Azure App services that have been private endpoint enabled for both the default as well as the scm hosts of each app, so that these apps could be tested without/outside of DNS, by using a hosts file instead. This has led me to write a short KQL query that will do just this. I will share this query with you today.

In the Azure portal search for Resource Graph Explorer:

rge

In the query editor add the following lines of code:

Resources
| where type =~ "microsoft.web/sites"
| mvexpand pe = properties.privateEndpointConnections
| extend peip = tostring(pe.properties.ipAddresses[0])
| mvexpand hosts = properties.hostNameSslStates
| project peip, hosts=tostring(hosts.name)
| order by peip asc
Enter fullscreen mode Exit fullscreen mode

You can also select the scope at which you want to run the query:

scope

Then select Run query:

run

That is it, you can now export the results to a CSV and copy the results over into a hosts txt file. In addition you can also save the query for future re-use to run it again if any private IP addresses of your App services private endpoints change to get an updated hosts file formatted result.

You can also select whether you want to save the query as a private query or a shared query. The later allows you to save the query as an object in a resource group that others can also access and run.

save

The Kusto Query Language is truly a powerful query tool that can be utilized in many different scenarios and is the main query language used in Azure Data Explorer and Azure Monitor. To learn more about KQL have a look at the following sample queries that can be used in Azure Monitor.

I hope you have enjoyed this post and have learned something new. You can also find the query code sample for this tutorial on my GitHub page. ❤️

Author

Like, share, follow me on: 🐙 GitHub | 🐧 X/Twitter | 👾 LinkedIn

Top comments (0)