Search code examples
azureazure-webapps

How do I programatically get an Azure webapp's ASUID?


I'm trying to write a domain management portal and to map a domain to an Azure webapp I need to add a TXT record with the app's ASUID to the domain. The only way I can find to get the ASUID is through the Azure Portal dashboard. Is there an API I can use to get it programmatically?


Solution

  • In case you get here wondering how to do this without manual intervention. This is my work flow:

    1. Deploy ARM template with web app and host name binding. It will fail and throw an error about the missing TXT record. I'm using PowerShell to catch the exception and check the error message: if ($ex -Match "TXT record"). If it's a hit I grab the asuid as below. If not I throw an exception.
    2. To get the asuid you need to load an AZ PowerShell module: Install-Module -Name Az.ResourceGraph. Note that only the host name binding failed so the app service is there. You can now run a query to get the value you need: $asuid = Search-AzGraph -Query "Resources | project name, properties.customDomainVerificationId, type | where type == 'microsoft.web/sites'" | where {$_.name -eq "my-webapp-name"}
    3. I now call a function that uses my DNS hosting providers API to add the TXT record with the applicable asuid. In my case it was available instantly so I could immediately re-deploy the ARM template successfully but your miles may vary.

    PS. If you are using the CLI and not PowerShell you can still do this using the CLI extension: az extension add --name resource-graph