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

使用sam deploy部署API Gateway时遇No integration defined for method错误求助

解决API Gateway部署时"No integration defined for method"错误

以下是针对该问题的排查方向和解决方法:

  • 检查OpenAPI文件的集成配置
    用OpenAPI定义API Gateway时,必须通过x-amazon-apigateway-integration扩展指定请求转发的目标Lambda函数。确保api.yaml中对应路径的method下包含该配置块,示例如下:

    paths:
      /your-endpoint:
        get:
          responses:
            '200':
              description: Success response
          x-amazon-apigateway-integration:
            type: aws_proxy
            uri: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${LambdaFunctionArn}/invocations
            httpMethod: POST
            credentials: arn:aws:iam::${AWS::AccountId}:role/YourApiGatewayExecutionRole
    

    注意type需设为aws_proxy(Lambda代理集成),httpMethod固定为POST,uri指向你的Lambda函数ARN,credentials是API Gateway调用Lambda所需的IAM角色ARN。

  • 动态传递Lambda ARN到OpenAPI文件
    不要在api.yaml中硬编码Lambda ARN,而是在template.yaml中通过!Sub动态注入,避免环境不一致问题。示例:

    Resources:
      MyApi:
        Type: AWS::Serverless::Api
        Properties:
          StageName: prod
          DefinitionBody:
            !Sub
              - |
                openapi: 3.0.1
                info:
                  title: My API
                  version: 1.0.0
                paths:
                  /your-endpoint:
                    get:
                      responses:
                        '200':
                          description: OK
                      x-amazon-apigateway-integration:
                        type: aws_proxy
                        uri: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${LambdaArn}/invocations
                        httpMethod: POST
                        credentials: !GetAtt ApiGatewayRole.Arn
              - LambdaArn: !GetAtt MyLambdaFunction.Arn
    
  • 添加API Gateway调用Lambda的权限
    新增Lambda权限资源,允许API Gateway触发你的Lambda函数:

    LambdaInvokePermission:
      Type: AWS::Lambda::Permission
      Properties:
        FunctionName: !Ref MyLambdaFunction
        Action: lambda:InvokeFunction
        Principal: apigateway.amazonaws.com
        SourceArn: !Sub "arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${MyApi}/*/*/*"
    
  • 验证OpenAPI文件完整性
    确认api.yaml中定义的所有路径和method都配置了对应的集成块,不能仅定义路径和响应规则而遗漏集成配置。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 18:30:19