【问题标题】:AWS SAM Template Fails to Create Configurations for API GatewayAWS SAM 模板无法为 API Gateway 创建配置
【发布时间】:2019-09-01 12:38:39
【问题描述】:

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

  1. 将 API 网关配置为在标头中需要 api-key
  2. 按照我的配置文件中的定义创建我自己的阶段。
  3. 未创建我文件中定义的 API 网关模型

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

模板.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-key 要求也没有出现。任何帮助,将不胜感激。

【问题讨论】:

  • 该模板为我生成了语法错误。你确定你在这里复制的正确吗?
  • 嘿@AlexHarvey,感谢您提供帮助。这是因为我用“XXXXX”替换了我的 accountId。如果您将其更改为您的,应该可以工作。 lambda 函数的资源部分也发生了变化,让我来解决这个问题

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


【解决方案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)

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

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

另请注意,在您修复重复密钥问题后,您还需要从 Events 引用您的 API,使用如下行:

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

另见我之前的回答here

【讨论】:

猜你喜欢
  • 2018-04-15
  • 2020-06-14
  • 1970-01-01
  • 1970-01-01
  • 2019-08-11
  • 2020-05-23
  • 2022-01-13
  • 1970-01-01
  • 2019-04-03
相关资源
最近更新 更多