How to get the "Function Url" which is with in a Function-App deployed using Terraform?
Asked Answered
M

3

5

As part of IaC, A Function App, lets name it FuncAppX is deployed using Terraform, which has a function with-in.

I need to access the Url of the same function with-in a function app using Terraform. I am sharing the screenshot for same here for reference, in which its easy to get the "GetFunction Url" but using terraform, I am not getting a way to return the same, which needed to be passed as an input to another function app.

Function App Function within FunctionApp

Microdont answered 20/9, 2021 at 13:15 Comment(1)
Dont link to external image services. Add them to this post so the question will still be understandable in the future.Prenatal
O
13

You need to build the URL of a specific function endpoint yourself. Since the concrete function names are defined via the function.json, you need to copy it to your Terraform scripts.

locals {
  function_foo = "foo"
}

The URL contains a secret as query parameter, which you can grab via the data source azurerm_function_app_host_keys.

data "azurerm_function_app_host_keys" "example" {
  name                = azurerm_function_app.example.name
  resource_group_name = azurerm_function_app.example.resource_group_name

  depends_on = [azurerm_function_app.example]
}

Now you can build the URL yourself.

output "url" {
  value = "https://${azurerm_function_app.example.default_hostname}/api/${local.function_foo}?code=${data.azurerm_function_app_host_keys.example.default_function_key}"
}

Be aware, that the azurerm_function_app_host_keys values may not immediately available, since the deployment of the concrete Function App is decoupled from the azurerm_function_app service creation. Depending on your scenario, you may need to add some manual synchronization (e.g. with null_resource).

Ornamental answered 21/9, 2021 at 6:22 Comment(1)
Function App Host Key != Function KeyIvette
P
3

Concider exposing you http triggered functions via the API management resource in azure. However you can call functions directly also.

To get the url of the function app you can use the default_hostname attribute on azurerm_function_app and forexample put in a keyvault using azurerm_key_vault_secret.

The attribute is listed in the terraform documentation here.

Of course this will not link to the specific function and will not include the key needed.

To extract a key needed to access the function you can use the function_app_host_keys. Where you get the default function key with

Then you can retrieve the default_function_key.

Then you need to combine these things to call the function (note you can only call functions with a http trigger).

The url of the function would then be {default_hostname}/api/{functionname}?code={default_function_key}.

Prenatal answered 20/9, 2021 at 18:7 Comment(0)
F
1

The answer above from @sschmeck is great and does work, but it's worth noting that it returns the Function App host key, which is a key that can be used to access all functions hosted under the Function App.

If you want to use the key that is for the specific function (in the portal you see this under "Function keys" within the function, or within the "default (Function key)" URL) you can retrieve it using the AzApi Terraform provider as follows:

data "azapi_resource_action" "function_app_key" {
  type                   = "Microsoft.Web/sites/functions@2023-01-01"
  action                 = "listkeys"
  resource_id            = "/subscriptions/${var.subscription_id}/resourceGroups/${azurerm_function_app.example.resource_group_name}/providers/Microsoft.Web/sites/${azurerm_function_app.example.name}/functions/${local.function_foo}"
  response_export_values = ["default"]
}

You then can then build the URL as with the answer above:

output "url" {
  value = "https://${azurerm_function_app.example.default_hostname}/api/${local.function_foo}?code=${jsondecode(data.azapi_resource_action.function_app_key.output).default}"
}
Forsythe answered 10/9, 2024 at 14:18 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.