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

在Azure Functions部署FastAPI项目遇「无HTTP触发器」问题求助

将FastAPI应用部署到Azure Functions时提示"No HTTP triggers found"

报错信息

No HTTP triggers found 

项目结构

项目结构

function.json 内容

{    
    "scriptFile": "backend/main.py",    
    "bindings": [      
        {        
            "authLevel": "anonymous",        
            "type": "httpTrigger",        
            "direction": "in",        
            "name": "req",        
            "methods": [          
                "get",          
                "post"        
            ],        
            "route": "{*route}"      
        },      
        {        
            "type": "http",        
            "direction": "out",        
            "$return": "$return"      
        }    
    ]
}

main.py 核心代码

import logging
import azure.functions as func

from apis.base import api_router
from core.config import settings

from db.base import Base
from db.session import engine
from db.utils import check_db_connected
from db.utils import check_db_disconnected
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from webapps.base import api_router as web_app_router

def include_router(app):    
    app.include_router(api_router)    
    app.include_router(web_app_router)

def configure_static(app):    
    app.mount("/static", StaticFiles(directory="static"), name="static")

def create_tables():    
    Base.metadata.create_all(bind=engine)

def start_application():    
    app = FastAPI(title=settings.PROJECT_NAME, version=settings.PROJECT_VERSION)    
    include_router(app)    
    configure_static(app)    
    create_tables()    
    return app

app = start_application()

async def main(req: func.HttpRequest) -> func.HttpResponse:    
    logging.info('Python HTTP trigger function processed a request.')    
    await check_db_connected()    
    try:        
        body = await req.get_body()        
        return func.HttpResponse(str(body))    
    except Exception as e:        
        logging.exception('Error')        
        return func.HttpResponse("Error", status_code=500)    
    await check_db_disconnected()

问题

之前成功部署过测试API,调整项目结构后部署失败,是否遗漏了什么配置?


排查与修复方案

1. 修正function.json的绑定错误

  • 输出绑定字段错误:将bindings中的输出项修改为"name": "$return",原代码中错误写成了"$return": "$return",这会导致Azure无法识别输出绑定。
  • 确认文件路径:Azure Functions要求function.json必须放在独立的函数文件夹中(比如命名为HttpTrigger),如果你的function.json位置不符,需调整文件夹结构,或修正scriptFile的相对路径(比如如果函数文件夹在根目录,scriptFile应为../backend/main.py)。

2. 接入FastAPI的请求处理逻辑

当前main函数仅返回请求体,未关联FastAPI的路由处理,需将Azure触发器请求转换为FastAPI可处理的格式:

  • 安装依赖:在requirements.txt中添加starlette,执行pip install starlette
  • 修改main函数:
from starlette.requests import Request
from starlette.responses import Response

async def main(req: func.HttpRequest) -> func.HttpResponse:
    await check_db_connected()
    try:
        # 转换Azure请求为Starlette请求格式
        starlette_req = Request(
            scope={
                "type": "http",
                "method": req.method,
                "path": req.route_params.get("route", ""),
                "query_string": req.query_params.encode(),
                "headers": dict(req.headers),
            },
            receive=lambda: {"body": await req.get_body(), "more_body": False}
        )
        # 让FastAPI处理请求
        response = await app(starlette_req.scope, starlette_req.receive)
        # 转换为Azure HttpResponse返回
        return func.HttpResponse(
            body=response.body,
            status_code=response.status_code,
            headers=dict(response.headers)
        )
    except Exception as e:
        logging.exception('Error')
        return func.HttpResponse("Error", status_code=500)
    finally:
        await check_db_disconnected()

3. 规范项目结构与依赖

  • 确保根目录存在requirements.txt,包含所有依赖:
fastapi
azure-functions
sqlalchemy
starlette
# 补充你项目用到的其他依赖(如pydantic等)
  • 本地测试验证:执行func start启动本地函数,确认能正常触发HTTP请求,再进行部署。

4. 检查VSCode部署配置

  • 部署前确认选择了正确的Azure函数应用实例
  • 确保VSCode的Azure Functions扩展已更新到最新版本,避免兼容性问题

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 08:15:33