使用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
相关产品推荐
相关产品推荐

