AWS SAM:REST API 不包含任何方法

Pie*_*rre 4 amazon-web-services aws-cloudformation aws-lambda aws-api-gateway aws-sam

我正在尝试使用 AWS SAM 部署一个简单的 API。当 API 很简单时(即没有明确指定 API 网关)。部署成功。

但是,以下部署失败:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Sample API

Parameters: 
  Stage:
    Type: String
    AllowedValues: 
      - dev
      - sat
      - demo
      - staging
      - prod
    Description: Enter dev, sat, demo, staging or prod

Resources:

  MyApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: !Ref Stage
      EndpointConfiguration: PRIVATE
      DefinitionBody:
        swagger: '2.0'
        x-amazon-apigateway-policy:
          Version: '2012-10-17'
          Statement:
            - Effect: Allow
              Principal: "*"
              Action: execute-api:Invoke
              Resource:
                - !Sub arn:aws:execute-api:*:*:*/${Stage}/*

  ThumbnailFunction:
    Type: 'AWS::Serverless::Function'
    Properties:
      Runtime: nodejs8.10
      Handler: get-config.handler
      CodeUri: ./functions
      Events:
        ThumbnailApi:
          Type: Api
          Properties:
            RestApiId: !Ref MyApi
            Path: /thumbnail
            Method: GET
Run Code Online (Sandbox Code Playgroud)

错误消息如下:

The REST API doesn't contain any methods (Service: AmazonApiGateway;
Status Code: 400; Error Code: BadRequestException
Run Code Online (Sandbox Code Playgroud)

在 Google 上查看,我确实发现在手动指定部署(此处此处)时提到了此错误。在我的情况下,部署是隐式的,因此我认为我的问题是不同的。

我使用的代码基于 SAM 示例(此处)。我正在挠头以了解我的堆栈出了什么问题。

任何指向解决方案的指针?

Ale*_*vey 8

正如错误消息所说,您尚未在 Swagger 中定义任何方法。我认为你的困惑在这里:

在我的情况下,部署是隐式的,因此我认为我的问题是不同的。

SAM 确实从 AWS::Serverless::Function 资源上定义的 Api 事件的联合创建了类型为 AWS::Serverless::Api 的隐式 API - 但前提是它们不引用(通过 RestApiId 属性)AWS::Serverless :: 您在模板中明确定义的 Api 资源。在你的情况下,它确实如此。

此外,您提到您的模板基于此处的“api_swagger_cors”示例 SAM 模板,但实际上您的模板与该示例之间存在关键区别,即:在示例中,正在从 S3 中提取 Swagger YAML 文件桶; 而在你的,你的 Swagger 是内联定义的,但它没有定义任何方法。

欲了解更多信息:

  • 这个隐v明确答案的API(我也写了这一点)。
  • 有关Swagger 的结构,请参阅页面。

  • 谢谢 ; 令人失望的是,Swagger 似乎是在 SAM 中定义网关策略的唯一方法,同时它基本上需要定义两次函数事件。 (4认同)