I am attempting to deploy an Azure Function in Terraform that must retrieve database connection strings from Azure Key Vault and then pass them to a python application deployed on a container instance. However despite several restructuring attempts, it cannot seem to find the host.json file.
The image queries a public API and then calls this function to write the API GET results to a postgres database.
The current file layout is:
- azure_function_secrets
- main.tf
- variable.tf
- src
- host.json
- requirements.txt
- azure_func
- azure_func.py
- function.json
The resource code in main.tf is defined as:
# Specify where terraform can locate the compressed function app logic and files
data "archive_file" "functionapp_zip" {
type = "zip"
source_dir = "./modules/azure_function_secrets/src" # Point to the src directory
output_path = "./modules/azure_function_secrets/functionapp.zip"
}
resource "azurerm_app_service_plan" "func_secrets_sp" {
name = "az-func-secrets-sp"
resource_group_name = data.azurerm_resource_group.resource_group.name
location = data.azurerm_resource_group.resource_group.location
kind = "FunctionApp"
sku {
tier = "Dynamic"
size = "Y1"
}
}
resource "azurerm_function_app" "secrets_function" {
name = "secrets-function"
resource_group_name = data.azurerm_resource_group.arc_resource_group.name
location = data.azurerm_resource_group.resource_group.location
app_service_plan_id = azurerm_app_service_plan.func_secrets_sp.id
storage_account_name = var.storage_account_name
storage_account_access_key = var.storage_account_access_key
app_settings = {
"FUNCTIONS_WORKER_RUNTIME" = "python"
"WEBSITE_RUN_FROM_PACKAGE" = azurerm_storage_blob.functionapp_secrets_code.url
TF_VAR_SUBSCRIPTION_ID = var.subscription_id
TF_VAR_RESOURCE_GROUP_NAME = data.azurerm_resource_group.resource_group.name
TF_VAR_LOCATION = data.azurerm_resource_group.resource_group.location
}
site_config {
always_on = false
ftps_state = "Disabled"
use_32_bit_worker_process = false
}
identity {
type = "SystemAssigned"
}
}
The host.json file is defined as:
{
"version": "2.0",
"logging": {
"logLevel": {
"default": "Debug"
},
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
}
}
}
The function itself is:
def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python secret retrieval function triggered.')
# Setup Key Vault client
try:
credential = DefaultAzureCredential()
key_vault_url = "https://<vault_name>.vault.azure.net/"
secret_client = SecretClient(vault_url=key_vault_url, credential=credential)
# Fetch secrets
user = secret_client.get_secret('username').value
password = secret_client.get_secret('password').value
return func.HttpResponse(
body = f'{{"user": "{user}", "password": "{password}"}}',
mimetype="application/json"
)
However, I consistently find this error in the AZ Function user interface on deployment:
mscorlib: Could not find file 'C:\home\site\wwwroot\host.json
Investigation into the container logs shows that the image is deployed without issue but the function is not:
Function host is not running.
I have tried moving the host.json file to the root and function directories without success. I have confirmed that the RBAC controls around the function are correct and should grant access. I have deployed monitoring insights but the log stream did not give any new errors to identify the issue.
Azure Function Cannot Find
host.json
: -
First of all, double-check the path of the src
directory specified in the data "archive_file" "functionapp_zip"
block.
And if the path is accurate but it still does not work, it indicates that it cannot always find the root
directory automatically. In that instance, set up a root
directory path
within the appsettings
block in the function_app
resource using AzureWebJobsScriptRoot
as detailed in the doc.
After checking all, I modified the required part of your code as shown below while keeping the rest of the code same.
resource "azurerm_app_service_plan" "func_secrets_sp" {
name = "xxxxx"
resource_group_name = data.azurerm_resource_group.resource_group.name
location = data.azurerm_resource_group.resource_group.location
kind = "FunctionApp"
sku {
tier = "Dynamic"
size = "Y1"
}
}
resource "azurerm_function_app" "secrets_function" {
name = "xxxxxx"
resource_group_name = data.azurerm_resource_group.arc_resource_group.name
location = data.azurerm_resource_group.resource_group.location
app_service_plan_id = azurerm_app_service_plan.func_secrets_sp.id
storage_account_name = var.storage_account_name
storage_account_access_key = var.storage_account_access_key
app_settings = {
"FUNCTIONS_WORKER_RUNTIME" = "python"
"WEBSITE_RUN_FROM_PACKAGE" = azurerm_storage_blob.functionapp_secrets_code.url
"AzureWebJobsScriptRoot" = "Set the root path as per your need"
TF_VAR_SUBSCRIPTION_ID = var.subscription_id
TF_VAR_RESOURCE_GROUP_NAME = data.azurerm_resource_group.resource_group.name
TF_VAR_LOCATION = data.azurerm_resource_group.resource_group.location
}
site_config {
always_on = false
ftps_state = "Disabled"
use_32_bit_worker_process = false
}
identity {
type = "SystemAssigned"
}
}
Deployment succeeded: