【问题标题】:How to set "Use Lambda Proxy integration" in swagger for API-Gateway?如何在 swagger 中为 API-Gateway 设置“使用 Lambda 代理集成”?
【发布时间】:2017-06-22 22:40:51
【问题描述】:

如何在 API-Gateway 的 swagger 中设置Use Lambda Proxy integration

我现在大摇大摆,但我真的很想设置代理集成。我会让我简化很多事情,更不用说我可以从 swagger 定义中删除 requestTemplatesresponses 块。

我正在尝试从this blog 帖子设置新的 Lambda 代理模式。

当前的招摇:

  '/document':
    options:
      summary: CORS support
      description: |
        Enable CORS by returning correct headers
      consumes:
        - application/json
      produces:
        - application/json
      tags:
        - CORS
      x-amazon-apigateway-integration:
        type: mock
        requestTemplates:
          application/json: |
            {
              "statusCode" : 200
            }
        responses:
          "default":
            statusCode: "200"
            responseParameters:
              method.response.header.Access-Control-Allow-Headers : "'Content-Type,X-Amz-Date,Authorization,X-Api-Key'"
              method.response.header.Access-Control-Allow-Methods : "'*'"
              method.response.header.Access-Control-Allow-Origin : "'*'"
            responseTemplates:
              application/json: |
                {}
      responses:
        200:
          description: Default response for CORS method
          headers:
            Access-Control-Allow-Headers:
              type: "string"
            Access-Control-Allow-Methods:
              type: "string"
            Access-Control-Allow-Origin:
              type: "string"
    x-amazon-apigateway-any-method:
      produces:
      - "application/json"
      responses:
        200:
          description: "200 response"
          schema:
            $ref: "#/definitions/Empty"
      x-swagger-router-controller: main
      x-lambda-function: ../../swiki/build/document
      x-amazon-apigateway-integration:
        type: aws
        httpMethod: POST
        uri: arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/@@LambdaFunctionDocument/invocations
        credentials: @@APIGatewayExecutionRole
        passthroughBehavior: "when_no_templates"
        requestTemplates:
          application/json: |
            #set($allParams = $input.params())
            {
              "bodyJson" : $input.json('$'),
              "format": "html",
              "params" : {
            #foreach($type in $allParams.keySet())
              #set($params = $allParams.get($type))
                "$type" : {
              #foreach($paramName in $params.keySet())
                  "$paramName" : "$util.escapeJavaScript($params.get($paramName))"
                #if($foreach.hasNext),#end
              #end
                }
              #if($foreach.hasNext),#end
            #end
              },
              "stageVariables" : {
            #foreach($key in $stageVariables.keySet())
                "$key" : "$util.escapeJavaScript($stageVariables.get($key))"
              #if($foreach.hasNext),#end
            #end
              },
              "context" : {
                "accountId" : "$context.identity.accountId",
                "apiId" : "$context.apiId",
                "apiKey" : "$context.identity.apiKey",
                "authorizerPrincipalId" : "$context.authorizer.principalId",
                "caller" : "$context.identity.caller",
                "cognitoAuthenticationProvider" : "$context.identity.cognitoAuthenticationProvider",
                "cognitoAuthenticationType" : "$context.identity.cognitoAuthenticationType",
                "cognitoIdentityId" : "$context.identity.cognitoIdentityId",
                "cognitoIdentityPoolId" : "$context.identity.cognitoIdentityPoolId",
                "httpMethod" : "$context.httpMethod",
                "stage" : "$context.stage",
                "sourceIp" : "$context.identity.sourceIp",
                "user" : "$context.identity.user",
                "userAgent" : "$context.identity.userAgent",
                "userArn" : "$context.identity.userArn",
                "requestId" : "$context.requestId",
                "resourceId" : "$context.resourceId",
                "resourcePath" : "$context.resourcePath"
              }
            }
        responses:
          default:
            statusCode: "200"

【问题讨论】:

    标签: swagger aws-lambda aws-api-gateway


    【解决方案1】:

    对于 Lambda 代理集成,必填字段为

      x-amazon-apigateway-integration:
        type: aws_proxy
        httpMethod: <method>
        uri:  <function_uri>
        credentials: <optional_creds>
    

    编辑:错字 编辑2:固定类型 编辑3:添加httpMethod

    【讨论】:

    • 当我尝试这个时,我收到了Warnings found during import: Unknown integration type 'lambda_proxy' for 'ANY /document'. Ignoring.
    • 抱歉我的错误,已修复
    • 看来你也需要httpMethod: POST。至少我做到了让它工作。感谢您的信息!
    【解决方案2】:

    请参阅blog post 以获取 HTTP 和 Lambda 代理的 3 个新功能(贪婪路径、ANY 方法、代理集成)的完整示例:

    ---
    swagger: "2.0"
    info:
      version: "2016-09-23T22:23:23Z"
      title: "Simple Proxy Example - Ryan Green"
    host: "zte3bswjjb.execute-api.us-east-1.amazonaws.com"
    basePath: "/demo"
    schemes:
    - "https"
    paths:
      /http/{proxy+}:
        x-amazon-apigateway-any-method:
          parameters:
          - name: "proxy"
            in: "path"
          x-amazon-apigateway-integration:
            type: "http_proxy"
            uri: "http://httpbin.org/{proxy}"
            httpMethod: "ANY"
            passthroughBehavior: "when_no_match"
            requestParameters:
              integration.request.path.proxy: "method.request.path.proxy"
      /lambda/{proxy+}:
        x-amazon-apigateway-any-method:
          parameters:
          - name: "proxy"
            in: "path"
          responses: {}
          x-amazon-apigateway-integration:
            type: "aws_proxy"
            uri: "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:[MY_ACCOUNT_ID]]:function:[MY_FUNCTION_NAME]]/invocations"
            passthroughBehavior: "when_no_match"
            httpMethod: "POST"
    

    【讨论】:

    • 好,所以您不需要需要凭据来使用代理?我正在使用awsSigv4,所以我不认为我想在那里指定凭据
    【解决方案3】:
    x-amazon-apigateway-integration:
        type: aws_proxy
        uri: "arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-west-2:<USER_ACCOUNT_ID>:function:<LAMBDA_FUNCTION_NAME>/invocations"
        httpMethod: POST
        credentials: "arn:aws:iam::<USER_ACCOUNT_ID>:role/<ROLE_NAME>"
        passthroughBehavior: when_no_match
    

    注意 -- 在 uri 中添加 /invocations

    注意 -- 将类型添加为 aws_proxy

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-14
      • 2021-09-11
      • 2021-07-17
      • 2021-06-30
      • 2019-07-09
      • 2018-01-23
      • 2018-05-04
      相关资源
      最近更新 更多