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

AWS SAM: The REST API doesn't contain any methods

我正在尝试使用 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

错误信息如下:

The REST API doesn't contain any methods (Service: AmazonApiGateway;
Status Code: 400; Error Code: BadRequestException

查看 Google,我确实发现在手动指定部署时提到此错误 (here, or here)。就我而言,部署是隐式的,因此我认为我的问题有所不同。

我使用的代码基于 SAM 示例 (here)。我绞尽脑汁想知道我的堆栈出了什么问题。

有任何解决方案吗?

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

In my case, the deployment is implicit, hence I assume my issue is different.

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

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

更多信息:

  • 参见 关于隐式 API 和显式 API 的回答(我也写过)。
  • 请参阅 this 页面了解 Swagger 的结构。