AWS SAM模板/ cloudformation没有为方法定义集成(服务:AmazonApiGateway

时间:2018-06-15 22:19:34

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

我正在尝试部署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" };
        }


    }

1 个答案:

答案 0 :(得分:3)

您的招摇定义缺失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网关始终向Lambda发出POST请求。