AWS Api-Gateway Lambda代理端点的Swagger定义

时间:2018-06-04 13:31:35

标签: amazon-web-services swagger documentation aws-api-gateway api-gateway

仅供参考 - 我已检查过与此相关的类似问题,但没有一个能解决我的问题。

我试图在AWS Api-Gateway下为许多API创建Swagger定义。我能够从我从API Stage下载的自动生成的YAML配置中成功地为其他(POST,GET)端点执行此操作。

但是当我尝试使用Lambda代理集成为Api-Gateway端点执行相同操作时遇到了问题:Error from Swagger editor.swagger.io

以下是我对失败端点的YAML定义:

    swagger: "2.0"
    info:
      version: "2018-04-18T17-09-07Z"
      title: "XXX API"
    host: "api.xxx.io"
    schemes:
    - "https"
    parameters:
      stage:
        name: stage
        in: path
        type: string
        enum: [ staging, production]    
        required: true
    paths:
      /env/{stage}/{proxy+}:
        x-amazon-apigateway-any-method:
          produces:
            - "application/json"
          parameters:
            - $ref: '#/parameters/stage'
            - name: "proxy"
              in: "path"
              required: true
              type: "string"
          responses: {}
          x-amazon-apigateway-integration:
            uri: "arn:aws:apigateway:eu-central-1:lambda:path/2015-03-31/functions/arn:aws:lambda:eu-central-1:xxxxxxxxx:function:environment/invocations"
            responses:
              default:
                statusCode: "200"
            passthroughBehavior: "when_no_match"
            httpMethod: "POST"
            cacheNamespace: "4vbcjm"
            cacheKeyParameters:
              - "method.request.path.proxy"
            contentHandling: "CONVERT_TO_TEXT"
            type: "aws_proxy"

这与AWS文档内联:enter link description here

拜托,我错过了什么?

1 个答案:

答案 0 :(得分:0)

乍一看,我相信您在parameters块中有一个错误。如果包含$ref,它将丢弃该块后面的任何内容,因此将删除您的代理名称。我使用api-gateway代理对lambda的所有调用进行了类似的设置,这是我的参数块:

parameters:
- name: "proxy"
  in: "path"
  required: true
  type: "string"

此外,如果您完全担心DDoS或提供安全数据,则可能需要授权者。这是通过向参数中添加security数组作为同级,并向securityDefinitions添加paths

security:
- authorizer: []

securityDefinitions:
  authorizer:
    type : "apiKey"
    name : "Authorization"
    in : "header"
    x-amazon-apigateway-authtype : "custom"
    x-amazon-apigateway-authorizer : {
      type : "request",
      authorizerUri : "arn:aws:apigateway:${region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${region}:${account_id}:function:${authorizer_function_name}/invocations",
      authorizerResultTtlInSeconds : 58,
      identitySource: "method.request.header.authorization",
    }

*请注意,我正在将摇摇欲坠作为terraform模板发布,因此将${}替代。