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

使用Terraform部署Python代码到Azure Function时函数未正常显示

问题描述

使用Terraform部署Python代码到Azure Function App时,Function App创建成功,但Azure Portal的Functions板块无可用函数,代码仅出现在AppFiles区域。

相关Terraform代码

provider "azurerm" {
  features {}
}

data "archive_file" "function_zip" {
  type        = "zip"
  source_dir  = "../${path.module}/blob_storage_trigger"
  output_path = "../${path.module}/blob_storage_trigger.zip"
  depends_on = [null_resource.pip]
}

resource "null_resource" "pip" {
  triggers = {
    requirements_md5 = "${filemd5("../${path.module}/blob_storage_trigger/requirements.txt")}"
  }
  provisioner "local-exec" {    
    command = "pip install --target='.python_packages/lib/site-packages' -r requirements.txt"
    working_dir = "../${path.module}/blob_storage_trigger"
  }
}

#Uploading to the blob container
resource "azurerm_storage_blob" "storage_blob_function" {
  name                   = "my-function.zip"
  storage_account_name   = "***"
  storage_container_name = "***"
  type                   = "Block"
  source                 = "../${path.module}/blob_storage_trigger.zip"
}

# Create Azure App Service Plan using Consumption pricing
resource azurerm_service_plan "app_service_plan" {
  name                   = "app_service_plan"
  location               = "West Europe"
  resource_group_name    = "***"
  os_type                = "Linux"
  sku_name               = "Y1"
}

resource "azurerm_function_app" "blurring_fn_app" {
  name                = "calinfntest-app-terraform"
  location            = "West Europe"
  resource_group_name = "***"

  storage_account_name       = "***"
  storage_account_access_key = "***"
  app_service_plan_id        = azurerm_service_plan.app_service_plan.id

  app_settings = {
    "WEBSITE_RUN_FROM_PACKAGE" = azurerm_storage_blob.storage_blob_function.url
  }
  site_config {}
}

观察到的情况

  • Function App创建成功,pip依赖已正确打包到压缩包,且压缩包已上传至指定存储容器(Terraform日志确认)
  • terraform apply执行无报错,但Portal中仅AppFiles可见代码,Functions板块无函数

解决方案

以下是排查和修复的具体方向:

1. 检查函数代码的目录结构与配置

Azure Functions对Python代码有严格的目录要求:

  • 每个函数必须有独立子目录,且子目录内包含function.json配置文件
  • 正确目录结构示例:
    blob_storage_trigger/
    ├── BlobTriggerFunction/
    │   ├── function.json
    │   └── __init__.py
    ├── .python_packages/
    └── requirements.txt
    
  • 确认function.json中scriptFile路径指向正确的Python文件(如__init__.py),触发器配置(Blob路径、连接字符串等)无误

2. 修正Blob包的访问权限

若存储容器为私有,直接使用Blob URL会导致Function App无法读取包:

  • 生成SAS令牌并拼接进URL,修改Terraform配置:
    # 添加SAS令牌生成
    data "azurerm_storage_account_sas" "function_blob_sas" {
      connection_string = azurerm_storage_account.your_storage_account.primary_connection_string
      resource_types {
        service   = false
        container = false
        object    = true
      }
      services {
        blob  = true
        queue = false
        table = false
        file  = false
      }
      start  = timeadd(timestamp(), "-1h")
      expiry = timeadd(timestamp(), "1y")
      permissions {
        read   = true
        write  = false
        delete = false
        list   = false
        add    = false
        create = false
      }
    }
    
    # 更新Function App的app_settings
    resource "azurerm_function_app" "blurring_fn_app" {
      # ... 其他配置 ...
      app_settings = {
        "WEBSITE_RUN_FROM_PACKAGE" = "${azurerm_storage_blob.storage_blob_function.url}${data.azurerm_storage_account_sas.function_blob_sas.sas}"
      }
    }
    

3. 配置Linux环境的Python版本

Linux Function App需要明确指定Python版本,避免版本不兼容:

  • 在azurerm_function_app的site_config中添加版本配置:
    site_config {
      linux_fx_version = "PYTHON|3.11" # 替换为代码使用的Python版本
      application_stack {
        python_version = "3.11"
      }
    }
    

4. 检查压缩包内容

本地解压blob_storage_trigger.zip,确认:

  • .python_packages/lib/site-packages下包含所有依赖包
  • 函数子目录和function.json直接在压缩包根目录,没有多余的上层嵌套
  • 没有遗漏必要的文件(如__init__.py)

5. 查看函数启动日志

进入Azure Portal的Function App,打开监测 > 日志,查看启动时的错误信息,常见问题包括:

  • 依赖包路径错误
  • function.json格式语法错误
  • Python版本不匹配

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 02:38:10