You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Terraform部署Azure Functions后门户不显示函数的问题求助

Azure Functions部署后函数不显示、无法触发的问题排查与解决

问题背景

通过Terraform部署Linux版Python 3.11 Azure Functions后,门户“应用文件”标签页可见代码,但概览页无函数列表,无法触发任何函数。相关Terraform代码和本地文件结构如下:

Terraform代码

resource "azurerm_storage_account" "function_storage" {
  name                          = var.storage_account_name
  resource_group_name           = var.resource_group_name
  location                      = var.location
  account_tier                  = "Standard"
  account_replication_type      = "LRS"
  min_tls_version               = "TLS1_2"
  public_network_access_enabled = true
}

resource "azurerm_storage_container" "function_container" {
  name                  = "${var.function_name}files"
  storage_account_name  = azurerm_storage_account.function_storage.name
  container_access_type = "private"
}

resource "azurerm_service_plan" "appplan" {
  name                = var.app_serviceplan_name
  location            = var.location
  resource_group_name = var.resource_group_name

  os_type  = "Linux"
  sku_name = "S1"
}

# Zip deployment can be very long (around 10 minutes)
data "archive_file" "file_function_app" {
  type       = "zip"
  source_dir = "../src/azure_functions/my_functions"
  # Use a hash of all code files as a zip filename to trigger the deployment on every changes
  output_path = "archives/${sha1(join("", [for f in fileset("../src/azure_functions/my_functions", "**") : filesha1("../src/azure_functions/my_functions/${f}")]))}-function.zip"
}

resource "azurerm_linux_function_app" "funcapp" {
  name                       = var.funcapp_name
  location                   = var.location
  resource_group_name        = var.resource_group_name
  service_plan_id            = azurerm_service_plan.appplan.id
  storage_account_name       = azurerm_storage_account.function_storage.name
  storage_account_access_key = azurerm_storage_account.function_storage.primary_access_key

  site_config {
    application_stack {
      python_version = 3.11
    }
  }

  app_settings = {
    WEBSITE_RUN_FROM_PACKAGE       = 1
    SCM_DO_BUILD_DURING_DEPLOYMENT = true
  }

  zip_deploy_file = data.archive_file.file_function_app.output_path
}

本地文件结构

├── infrastructure
│   ├── azure-function.tf
│   ├── main.tf
│   └── variable.tf
└── src
    ├── azure_functions
    │   └── my_function
    │       ├── commons
    │       │   ├── config
    │       │   │   ├── config.py
    │       │   │   ├── __init__.py
    │       │   ├── connectors
    │       │   │   ├── __init__.py
    │       │   │   └── sql.py
    │       │   ├── __init__.py
    │       ├── function_app.py
    │       ├── function_compute_all_profiles.py
    │       ├── function_p4_all_profiling.py
    │       ├── function_p4_profiling.py
    │       ├── host.json
    │       ├── __init__.py
    │       ├── local.settings.json
    │       └── requirements.txt

问题原因分析

  1. 打包路径不匹配:Terraform中data.archive_file的source_dir配置为../src/azure_functions/my_functions(复数),但本地实际目录是my_function(单数),导致打包内容不符合预期。
  2. Zip包结构错误:Azure Functions要求核心文件(host.json、function_app.py等)必须位于Zip根目录,若打包时将my_function目录本身包含在内,平台无法识别嵌套的函数代码。
  3. 函数触发器定义缺失/错误:若function_app.py或其他函数文件未使用正确的Python装饰器定义触发器(如@app.http_trigger),平台无法识别函数。
  4. 部署构建失败:SCM_DO_BUILD_DURING_DEPLOYMENT=true开启了部署时构建,但如果依赖缺失、代码语法错误,会导致构建失败,无法生成函数元数据。

解决步骤

1. 修正Terraform打包路径

修改data.archive_file的路径配置,匹配实际本地目录:

data "archive_file" "file_function_app" {
  type       = "zip"
  source_dir = "../src/azure_functions/my_function"  # 改为单数目录名
  output_path = "archives/${sha1(join("", [for f in fileset("../src/azure_functions/my_function", "**") : filesha1("../src/azure_functions/my_function/${f}")]))}-function.zip"
}

同步修正fileset和filesha1中的路径,确保哈希计算准确。

2. 确保Zip包结构正确

打包后的Zip根目录需直接包含以下内容:

  • host.json
  • function_app.py
  • requirements.txt
  • 所有函数文件(如function_compute_all_profiles.py)
  • commons目录

手动测试方式:进入../src/azure_functions/my_function目录,直接压缩目录内所有文件和子目录,而非压缩my_function目录本身。

3. 检查函数触发器定义

确保function_app.py中每个函数都有正确的触发器装饰器,示例:

import azure.functions as func

app = func.FunctionApp()

@app.function_name(name="ComputeAllProfiles")
@app.http_trigger(auth_level=func.AuthLevel.ANONYMOUS, methods=["get", "post"], route="compute-all-profiles")
def compute_all_profiles(req: func.HttpRequest) -> func.HttpResponse:
    # 业务逻辑
    return func.HttpResponse("执行成功", status_code=200)

注意:Python Azure Functions v2/v3版本需使用func.FunctionApp()初始化应用,确保装饰器语法符合版本要求。

4. 排查构建日志

在Azure门户的Function App中,进入部署中心 -> 日志,查看构建过程是否有错误:

  • 检查requirements.txt是否存在依赖版本冲突或缺失
  • 检查代码是否有语法错误导致构建失败

5. 重启Function App

修改配置或重新部署后,在门户中手动重启Function App,确保新配置和代码生效。

内容的提问来源于stack exchange,提问作者Nakeuh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 19:20:54