【问题标题】:AWS SAM template/cloudformation No integration defined for method (Service: AmazonApiGatewayAWS SAM 模板/cloudformation 没有为方法定义集成(服务:AmazonApiGateway
【发布时间】:2018-11-25 18:09:05
【问题描述】:

我正在尝试部署 lambda 函数和 API 网关。我使用 AWS CLI 创建了一个 .net 核心 Web API 项目。在 aws Web 控制台上仅部署函数并手动创建 API 网关和资源确实有效。

如果我在模板中包含 API 网关,在通过 Web 控制台或 CLI 进行 SAM 包部署后,我会收到以下错误:

“没有为方法定义集成(服务:AmazonApiGateway;状态代码:400;错误代码:BadRequestException;请求 ID:....)”

这里有什么问题或遗漏吗?

SAM 打包命令:

sam package  --template-file  sam-profile.yaml --output-template-file serverless-output.yaml  --s3-bucket testapp-fewtfvdy-lambda-deployments

SAM 模板:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
  ProfileFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: testapp.Profile.NetCoreVS::testapp.Profile.NetCoreVS.LambdaEntryPoint::FunctionHandlerAsync
      Runtime: dotnetcore2.0
      MemorySize : 128
      Timeout : 5
      CodeUri: bin/Release/netcoreapp2.0/publish
      Events:
        ProfileAny:
          Type: Api
          Properties:
            RestApiId: !Ref ProfileApiGateway
            Path: /profile/v1
            Method: GET

  ProfileApiGateway:
    DependsOn: ProfileFunction
    Type: 'AWS::Serverless::Api'
    Properties:
      StageName: Prod
      DefinitionUri: './swagger.yaml'

swagger.yaml:

---
swagger: '2.0'
info:
  version: v1
  title: ProfileAPI
paths:
  "/profile/v1":
    get:
      tags:
      - Values
      operationId: ProfileV1Get
      consumes: []
      produces:
      - text/plain
      - application/json
      - text/json
      parameters: []
      responses:
        '200':
          description: Success
          schema:
            type: array
            items:
              type: string
definitions: {}

.net核心方法:

[Route("profile/v1")]
    public class ValuesController : Controller
    {
        // GET api/values
        [HttpGet]
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2","value_new3" };
        }


    }

【问题讨论】:

    标签: aws-lambda aws-api-gateway amazon-cloudformation aws-serverless


    【解决方案1】:

    缺少你的招摇定义x-amazon-apigateway-integration

    这应该为您提供集成层:

    ---
    swagger: '2.0'
    info:
      version: v1
      title: ProfileAPI
    paths:
      "/profile/v1":
        get:
          tags:
          - Values
          operationId: ProfileV1Get
          consumes: []
          produces:
          - text/plain
          - application/json
          - text/json
          parameters: []
          responses:
            '200':
              description: Success
              schema:
                type: array
                items:
                  type: string
          x-amazon-apigateway-integration:
            httpMethod: post
            type: aws_proxy
            uri:
              Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${ProfileFunction.Arn}/invocations
    definitions: {}
    

    请注意,x-amazon-apigateway-integration 的 httpMethod 始终是 POST,因为无论您的 API 路由使用什么方法,API Gateway 总是向 Lambda 发出 POST 请求。

    【讨论】:

    • 我试过这个,它导致了这个错误:由于路径 /profile/v1 的集成格式错误,无法解析 swagger 文档。 (服务:AmazonApiGateway;状态代码:400;错误代码:BadRequestException
    • ProfileFunction.Arn 应该与您在 SAM 模板的 Resources 部分中定义的函数名称匹配。
    • 我通过在 AWS::Serverless::Api 定义中使用 DefinitionBody 而不是 DefinitionUri 解决了“无法解析 swagger”的问题。见discussionexample
    • 是否可以动态定义 x-amazon-apigateway-integration.uri?如何将其与 SAM 模板中的功能匹配?
    • 我试过这个,我得到这个权限错误。 ``` 由于配置错误,执行失败:Lambda 函数的权限无效 ``` 请对此提出任何建议。
    猜你喜欢
    • 2020-06-14
    • 1970-01-01
    • 1970-01-01
    • 2021-07-01
    • 2018-12-02
    • 2021-12-08
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    相关资源
    最近更新 更多