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

时间:2019-04-11 00:13:56

标签: amazon-web-services aws-api-gateway aws-sam-cli aws-sam

我对AWS SAM和Provisioning API Gateway配置有疑问。我正在尝试做一些事情:

  1. 将API网关配置为在标头中要求api-key
  2. 按照我的配置文件中的定义创建自己的舞台。
  3. 未创建我文件中定义的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#"

根据我的招摇和模板文件中的定义,应该为网关创建“ alpha”阶段,但什么也没有出现。 “空”模型和api键要求也不会出现。任何帮助,将不胜感激。

1 个答案:

答案 0 :(得分:1)

问题是您在模板中重复了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)

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

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

请注意,在解决重复密钥问题之后,您还需要使用以下行引用Events中的API:

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

另请参阅我先前的答案here