API网关 - Lambda代理集成和异步调用

时间:2017-08-15 14:57:00

标签: amazon-web-services aws-lambda aws-api-gateway

如果我设置标头X-Amz-Invocation-Type:'Event',则调用是异步完成的,但是当Amazon文档声明(https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-set-up-simple-proxy.html#api-gateway-simple-proxy-for-lambda-output-format)时,当使用Proxy Lambda Integration时,lambda函数必须返回这种格式良好的响应:

callback(null, {"statusCode": 200, "body": "results"})

由于lambda函数是异步调用的,因此API网关永远不会得到答案,然后返回502 Bad Gateway错误而不是200 OK状态。

在swagger配置的摘录下面:

"/myFunc": {
  "post": {
    "parameters": [
      {
        "name": "myparam",
        "in": "query",
        "required": true,
        "type": "string"
      }
    ],
    "responses": {
      "200": {
        "description": "200 response"
      }
    },
    "x-amazon-apigateway-request-validator": "Validate query string parameters and headers",
    "x-amazon-apigateway-integration": {
      "responses": {
        "default": {
          "statusCode": "200"
        }
      },
      "uri": "arn:aws:apigateway:ap-northeast-1:lambda:path/2015-03-31/functions/arn:aws:lambda:ap-northeast-1:idAccount:function:myFunc/invocations",
      "passthroughBehavior": "when_no_match",
      "httpMethod": "POST",
      "type": "aws_proxy",
      "requestParameters": {
        "integration.request.header.X-Amz-Invocation-Type": "'Event'"
      }
    }
  }
}

有没有办法让它奏效?

1 个答案:

答案 0 :(得分:0)

您可以设置自定义lambda集成(不带代理标志)。您需要配置映射模板以将请求/响应转换为所需格式。

http://docs.aws.amazon.com/apigateway/latest/developerguide/getting-started-lambda-non-proxy-integration.html#getting-started-new-lambda