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

部署于AWS APIGW/Lambda的FastAPI Docs访问出现403错误

解决FastAPI+Mangum+AWS APIGW下/docs和/redoc 403/NotFound问题

以下是针对问题的具体解决方案,都是实际部署踩过的坑:

  • 修正APIGW的路由配置
    别只配置GET /docs,要把Swagger和Redoc的所有静态资源请求都转发到Lambda。在APIGW里添加两个路由:

    • GET /docs/{proxy+}
    • GET /redoc/{proxy+}
      同时确保集成请求的映射模板里,把原始请求的完整路径传递给Lambda(比如V2映射模板中保持$request.path原样,不要手动修改路径)。
  • 精准配置FastAPI和Mangum的路径参数
    不要乱试组合,直接按以下代码配置:

    from fastapi import FastAPI
    from mangum import Mangum
    
    # root_path与APIGW的阶段名保持一致,即你的/latest
    app = FastAPI(
        root_path="/latest",
        root_path_in_servers=True,
        docs_url="/docs",
        redoc_url="/redoc"
    )
    
    # 你的接口路由示例
    @app.get("/hello")
    async def hello():
        return {"message": "Hello World"}
    
    # Mangum必须指定api_gateway_base_path,与root_path保持一致
    handler = Mangum(app, api_gateway_base_path="/latest")
    

    这个api_gateway_base_path参数是Mangum处理APIGW前缀剥离后路径匹配的关键,漏了大概率会出现NotFound。

  • 确认访问URL的正确性
    部署后正确的访问路径是https://api.example.com/latest/docs,而非https://api.example.com/docs。因为你的DNS映射到APIGW的/latest阶段,必须带上这个前缀,否则APIGW找不到对应路由,直接返回403或NotFound。

  • 排查权限相关问题(针对403错误)
    如果仍出现403,检查以下几点:

    • APIGW的资源策略是否允许访问/docs路径
    • Lambda的执行角色是否拥有lambda:InvokeFunction权限,且信任APIGW作为触发方
    • 是否有AWS WAF规则拦截了/docs开头的请求

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 14:05:26