6

我正在尝试部署 lambda 函数和 API gateway 。我使用 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'

招摇。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" };
        }


    }
4

1 回答 1

14

您的招摇定义缺失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: {}

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

于 2018-06-18T00:19:07.173 回答