azure-functionsterraformterraform-provider-azureinfrastructure-as-code

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


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


Solution

  • 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).