'Azure Function Url doesn't include authentication code when using azurerm_runction_app_function in Terraform

When you setup a Function in Azure, it lets you get the default function Url with the function key. It looks sort of like:

https://my-function-app.azurewebsites.net/api/my-function-name?code=[authentication-code]

This lets me call that function from an API endpoint without using any other authentication. I am trying to get access to that key via the azurerm provider somehow. UPDATE: I am using version 1.1.7 of Terraform and v3.0.2 of azurerm provider.

I can get the main Url (not including the authentication code) using azurerm_function_app_function.my-function.url. I can get the host key using data.azurerm_function_app_host_keys.default_function_key.

data "azurerm_function_app_host_keys" "function-host-keys" {
  name                = azurerm_windows_function_app.function-app.name
  resource_group_name = azurerm_resource_group.resource-group.name
}

resource "azurerm_function_app_function" "my-function" {
  name            = my-function
  function_app_id = azurerm_windows_function_app.function-app.id
  language        = "CSharp"

  file {
    name    = "MyFunc.cs"
    content = file("../../MyFunc.cs")
  }

  config_json = jsonencode({
    "bindings" = [
      {
        "authLevel" = "function"
        "direction" = "in"
        "methods" = [
          "get",
          "post",
        ]
        "name" = "req"
        "type" = "httpTrigger"
      },
      {
        "direction" = "out"
        "name"      = "$return"
        "type"      = "http"
      }
    ]
  })
}

locals {
  # This gets the host key but I need the function key
  host-key = data.azurerm_function_app_host_keys.function-host-keys.default_function_key

  # This gets the url, but without the "?code=######" 
  url = azurerm_function_app_function.my-function.url

  # How do I get the function key for the ?code=#### part of the Url?
  code = data.azurerm_some_function_resource.function_key
}

But I can’t seem to find anything that gives me the default function key for my specific function, as is available in the Azure portal. Please help.



Solution 1:[1]

Could you use a data source with a depends_on for the newly created FunctionApp?

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

  depends_on = [
    azurerm_function_app.item
  ]
}

https://github.com/hashicorp/terraform-provider-azurerm/issues/8515#issuecomment-694515657

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 BrettMiller