How to get the host url of a linux app service in azure bicep deployment templates
Asked Answered
U

2

9

I've created an app service using a bicep resource shown below

  name: '${appName}'
  location: location
  kind: 'linux,container,fnapp'
  properties: {
    serverFarmId: servicePlan.id
    siteConfig: {
      linuxFxVersion: 'DOCKER|${dockerLocation}'
      healthCheckPath: '/api/healthcheck'
      alwaysOn: true
      appSettings: [
        ...
      ]
    }
  }
}

Which works as expected, however I'd like to get the url for that app service to use as a backend url for my apim service.

I'm currently using var fnAppUrl = 'https://${fnApp.name}.azurewebsites.net/api'. Is there any way I can get the default url from the direct output of the function app resource i.e. var fnAppUrl = fnApp.url or something similar?

TIA

Unloose answered 6/7, 2022 at 10:1 Comment(0)
B
6

There is no specific property for this base path as it is set by configuring the value of extensions.http.routePrefix in host.json. (Docs: https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-http-webhook?tabs=in-process%2Cfunctionsv2&pivots=programming-language-csharp#hostjson-settings)

If the /api prefix is not required, then it would suffice to use the following output in your Bicep file:

resource myFunctionApp 'Microsoft.Web/sites@2021-03-01' = {
   // ...
}

output functionBaseUrl string = 'https://${myFunctionApp.properties.defaultHostName}'

Then in your host.json ensure the routePrefix is set to an empty string:

{
    "extensions": {
        "http": {
            "routePrefix": ""
        }
    }
}

If you do want to use a route prefix, you need to combine it with the default host name:

resource myFunctionApp 'Microsoft.Web/sites@2021-03-01' = {
   // ...
}

output functionBaseUrl string = 'https://${myFunctionApp.properties.defaultHostName}/api'
Behre answered 6/7, 2022 at 16:17 Comment(0)
M
0

As far as I know, there is no direct url you can get from the function. But you can use something like:

output functionBaseUrl string = 'https://${function.properties.defaultHostName}/api'

This will result in the default hostname including the "azurewebsites.net" part.

Check the full example in the template.bicep here: https://github.com/DSpirit/azure-functions-bicep-outputs-host

Mielke answered 6/7, 2022 at 11:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.