Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 262
Mukteswar Patnaik Avatar asked Dec 30 '22 12:12

Mukteswar Patnaik


2 Answers

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

like image 74
sschmeck Avatar answered Apr 09 '23 09:04

sschmeck


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}.

like image 38
Achtung Avatar answered Apr 09 '23 07:04

Achtung