AWS SAM 模板无法为 API 网关创建配置

AWS SAM Template Fails to Create Configurations for API Gateway

我在使用 AWS SAM 和配置 API 网关配置时遇到问题。我正在尝试做一些事情:

  1. 配置 API 网关以在 headers
  2. 中要求 api-key
  3. 按照我的配置文件中的定义创建我自己的舞台。
  4. 我的文件中定义的 API 网关模型没有被创建

目前,API 网关已配置并链接到我的 lambda 函数,但它无法满足上述两个要求。以下是我的文件:template.yaml 和 swagger.yaml.

Template.yaml

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
    sam-nfeed-s3

# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
    Function:
        Timeout: 60
    Api:
      EndpointConfiguration: REGIONAL
Resources:
  SAMnfeedS3API:
    Type: AWS::Serverless::Api
    Properties:
      StageName: alpha
      DefinitionUri: ./swagger.yaml
Resources:
  SAMnfeedS3Lambda:
    Type: AWS::Serverless::Function
    Properties:
        CodeUri: test-function-sam/
        Handler: nfeed_vdp_clusters.lambda_handler
        Runtime: python3.6
        Role: arn:aws:iam::XXXXXXX:role/Lambda
        Events:
            SAMnfeedS3API:
                Type: Api
                Properties:
                    Path: /vdp_clusters
                    Method: GET 
        Environment:
            Variables:
                TEST: test

Outputs:

    SAMnfeedS3API:
      Description: "API Gateway endpoint URL for Staging env"
      Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Staging/vdp_clusters"

    SAMnfeedS3Lambda:
      Description: "Lambda Function ARN"
      Value: !GetAtt SAMnfeedS3Lambda.Arn

Swagger.yaml

---
swagger: '2.0'
info:
  title: !Ref AWS::StackName
basePath: "/alpha"
schemes:
- "https"
x-amazon-apigateway-api-key-source : "HEADER"
paths:
  "/vdp_clusters":
    get:
      consumes:
      - application/json
      produces:
      - application/json
      parameters:
      - name: x-api-key
        in: header
        required: true
        type: string
      responses:
        200:
          description: "200 response"
          schema:
            $ref: "#/definitions/Empty"
      x-amazon-apigateway-integration:
        uri: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${AWS::Region}:XXXXXXXXX:function:${SAMnfeedS3Lambda.Arn}/invocations
        responses:
          default:
            statusCode: "200"
        httpMethod: "POST"
        type: aws_proxy
      security:
      - api_key: []
securityDefinitions:
  api_key:
    type: "apiKey"
    name: "x-api-key"
    in: "header"
definitions:
  Empty:
    type: "object"
    title: "Empty Schema"
    $schema: "http://json-schema.org/draft-04/schema#"

根据我的 swagger 和模板文件中的定义,应该为网关创建 "alpha" 阶段,但什么也没有出现。 "Empty" 模型和 api-key 要求也不会出现。如有任何帮助,我们将不胜感激。

问题是您在模板中复制了 Resources 键。

我建议始终在 SAM 模板上使用 yamllint 实用程序,因为它可以检测到 sam validate 无法始终检测到的 YAML 格式问题。这是我得到的:

▶ yamllint sam-app/template.yaml
sam-app/template.yaml
...
  18:1      error    duplication of key "Resources" in mapping  (key-duplicates)

如果您随后查看由 sam build 步骤创建的 packaged.yml 文件,您会注意到您定义的 API 将丢失。那是因为 Python 中的字典不可能包含重复的键。当 Python YAML 库读取文件时,您指定的第二个 Resources 块将覆盖第一个块。

SAM 然后根据您在 Events 中指定的 API 使用其自己生成的 Swagger 而不是您(以为您)生成的 Swagger 生成隐式 API SAMnfeedS3API提供。

另请注意,在解决重复键问题后,您还需要从 Events 中引用您的 API,并使用以下行:

    Events:
      SAMnfeedS3API:
        Type: Api
        Properties:
          Path: /vdp_clusters
          Method: GET
          RestApiId: !Ref SAMnfeedS3API  ## ADD THIS LINE

另见我之前的回答