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

如何通过Terraform在Azure Function App中创建HTTP触发器

解决方案

当前HashiCorp官方azurerm provider未在azurerm_function_app资源中提供内嵌创建Function触发器的参数,你可以通过以下两种方式实现HTTP触发器的创建:

方案1:使用azurerm_function_app_function独立资源(推荐)

注意:该资源要求azurerm provider版本不低于3.0.0
该资源为azurerm provider后续新增的专用资源,可直接在已创建的Function App下新增单个Function,支持全量HTTP触发器配置,示例代码如下:

resource "azurerm_function_app_function" "http_trigger" {
  name            = "HttpTriggerDemo"
  function_app_id = azurerm_function_app.example.id
  language        = "Python" # 按需替换为实际使用的开发语言,支持CSharp、JavaScript、Java、PowerShell、Python等

  config_json = jsonencode({
    "bindings": [
      {
        "authLevel": "function", # 可选值:anonymous、function、admin,对应触发器的权限等级
        "type": "httpTrigger",
        "direction": "in",
        "name": "req",
        "methods": ["get", "post"] # 配置允许的HTTP请求方法
      },
      {
        "type": "http",
        "direction": "out",
        "name": "$return"
      }
    ]
  })

  # 关联Function的执行代码,支持直接写内容或通过file函数读取本地代码文件
  file {
    name    = "run.py" # 对应语言的入口文件名,C#为run.csx、JavaScript为index.js
    content = file("${path.module}/src/HttpTriggerDemo/run.py")
  }
}

方案2:通过代码包挂载实现

如果你使用的azurerm版本低于3.0.0,无法使用上述资源,可以通过在Function App的配置项中指定代码包地址实现HTTP触发器部署:

  • 提前在本地按照Azure Function的目录规范编写HTTP触发器代码(包含function.json配置文件和业务入口代码),打包为zip格式
  • 将zip包上传到可访问的存储位置,在azurerm_function_app的app_settings中添加WEBSITE_RUN_FROM_PACKAGE参数指向该包地址即可,示例如下:
resource "azurerm_function_app" "example" {
  name                      = "functionapptest"
  location                  = var.location
  resource_group_name       = var.resource_group_name
  app_service_plan_id       = var.functionappplan.id
  storage_connection_string = azurerm_storage_account.functionapptestsa.primary_blob_connection_string
 
  app_settings = {
      APPINSIGHTS_INSTRUMENTATIONKEY = azurerm_application_insights.app_insight.instrumentation_key
      WEBSITE_RUN_FROM_PACKAGE = "https://你的存储账号地址/function-package/demo.zip"
  }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 11:45:06