Retrieve Azure Static Web App Default Custom Domain in ARM Template

192 Views Asked by At

I am deploying an Azure API management app through an ARM template. As a part of my requirement, I need to retrieve the static web app default domain to configure APIs to receive requests through an ARM template.

For now, I am doing manually by following below link, but I need to automate same kind of steps through ARM template deployment.

https://learn.microsoft.com/en-us/azure/static-web-apps/apis-api-management#configure-apis-to-receive-requests

we can fetch azure function app default key using below script, which is something similar I am looking for same to retrieve static web app custom domain name.

listKeys(concat(resourceId(variables('functionSite-ResourceGroup'),'Microsoft.Web/sites', 'FunctionTestAppName'), '/host/default'), '2016-08-01').functionKeys.functionKeys.default

1

There are 1 best solutions below

0
Jahnavi On

Firstly, retrieval of default custom domain value from a static web site is not directly doable as the communication between custom domains and web sites will be managed externally. You can use either PowerShell or CLI to retrieve the default value easily using related commands.

CLI:

az staticwebapp show --name "jahnavistatic" --resource-group "resource_group" --query defaultHostname -o tsv

enter image description here

PowerShell:

$static=Get-AzStaticWebApp -ResourceGroupName "resource_group" -Name "jahnavistatic
$static.defaulthostname

enter image description here

If your requirement is still with the ARM template, then you can follow the workaround given below.

You can pass the above PowerShell or CLI commands directly to the deployment scripts in ARM template and reference the template later in the outputs block.

Refer MSDoc to check how to link the templates in outputs block.

Another alternative is to set the default custom domain value as a user defined value in the parameters block and retrieve it in the outputs block by referencing that related parameter.

I tried a sample template for the above workaround by referring to the github template and was worked as follows.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "location": {
            "defaultValue": "[resourceGroup().location]",
            "type": "String",
            "metadata": {
                "description": "Location for all resources."
            }
        },
        "webAppName": {
            "type": "String",
            "metadata": {
                "description": "latestjank"
            }
        },
        "sku": {
           "type": "String",
           "metadata": {
            "description": ""
           }
        },
          "skuname": {
           "type": "String",
           "metadata": {
            "description": ""
           }
        },
        "customHostname": {
            "type": "String",
            "metadata": {
                "description": "mynewhost"
            }
        }
    },
    "variables": {
        "appServicePlanName": "[concat(parameters('webAppName'),'-asp-', uniquestring(resourceGroup().id))]"
    },
    "resources": [
        {
            "type": "Microsoft.Web/staticsites",
            "apiVersion": "2019-08-01",
            "name": "[parameters('webAppName')]",
            "location": "[parameters('location')]",
            ],
            "properties": {
                "name": "[parameters('webAppName')]"
            },
            "sku": {
                    "Tier": "[parameters('sku')]",
                    "Name": "[parameters('skuname')]"
                }
        },
        {
            "type": "Microsoft.Web/sites/hostnameBindings",
            "apiVersion": "2019-08-01",
            "name": "[concat(parameters('webAppName'), '/', parameters('customHostname'))]",
            "location": "[parameters('location')]",
            "dependsOn": [
                "[resourceId('Microsoft.Web/staticsites', parameters('webAppName'))]"
            ],
            "properties": {}
        }
    ],
    "outputs": {
        "hostName": {
            "type": "String",
            "value": "[reference(parameters('customHostname'))]"
        }
    }
}

enter image description here