如何在 API 网关的 swagger 中设置“使用 Lambda 代理集成”?

2024-06-30

如何设置Use Lambda Proxy integration对 API 网关大摇大摆?

我目前的态度如下,但我真的很想设置代理集成。我会让我简化很多事情,更不用说我可以删除requestTemplates and responses阻止 swagger 定义。

我正在尝试设置新的 Lambda 代理模式这个博客 https://aws.amazon.com/blogs/aws/api-gateway-update-new-features-simplify-api-development/ post.

目前的霸气:

  '/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"

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

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

编辑:错别字 编辑2:固定类型 edit3:添加http方法

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 API 网关的 swagger 中设置“使用 Lambda 代理集成”? 的相关文章

随机推荐