【问题标题】:Unable to execute Lambda in Async mode via API Gateway POST request无法通过 API Gateway POST 请求以异步模式执行 Lambda
【发布时间】:2016-11-14 02:41:40
【问题描述】:

为什么目前没有办法通过网关 API 以异步模式执行 AWS Lambda,而不涉及仅用于调用 invoke() 方法的中间 Lambda?

即使我像这样添加集成:

r = client.put_integration(
    restApiId=rest_api_id,
    resourceId=resource_id,
    httpMethod='POST',
    type='AWS',
    integrationHttpMethod='POST',
    uri=uri,
    requestParameters={
        'integration.request.header.X-Amz-Invocation-Type': "'Event'",
        'integration.request.header.Invocation-Type': "'Event'"
    }
)

它仍然同步执行... 是否有一些平台限制?

【问题讨论】:

  • 来自文档:docs.aws.amazon.com/apigateway/latest/developerguide/…“如果您的 API 对 Lambda 函数进行异步调用,您必须使用 AWS Service Proxy 集成类型”。您在使用 Service Proxy 集成吗?
  • 你的目标是响应请求然后继续工作吗?如果是这样,那么是的,您需要从处理程序内部调用一个单独的 lambda 函数。
  • @idbehold 我知道这样的解决方案,而且效果很好,但我不想使用额外的函数调用。 @MarkB 这里没有像 AWS Service Proxy 这样的类型被定义为有效类型:boto3.readthedocs.io/en/latest/reference/services/… BTW,看来我现在设法以异步模式启动 Lambda 函数执行。它只是工作,没有任何变化。不确定,是什么导致它以前不能以同样的方式工作。

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


【解决方案1】:

我有一个示例 Swagger 文档,您可以将调用类型切换为 Lambda 函数。我想您已经知道如何映射标头以触发不同的调用类型,但我认为您可能忘记部署 API。

大摇大摆

{
  "swagger": "2.0",
  "info": {
    "version": "2016-02-11T22:00:31Z",
    "title": "LambdaAsync"
  },
  "host": "<placeholder>",
  "basePath": "<placeholder>",
  "schemes": [
    "https"
  ],
  "paths": {
    "/": {
      "get": {
        "produces": [
          "application/json"
        ],
        "parameters": [
          {
            "name": "X-Amz-Invocation-Type",
            "in": "header",
            "required": false,
            "type": "string"
          }
        ],
        "responses": {
          "200": {
            "description": "200 response",
            "schema": {
              "$ref": "#/definitions/Empty"
            }
          }
        },
        "x-amazon-apigateway-integration": {
          "passthroughBehavior": "when_no_match",
          "httpMethod": "POST",
          "uri": "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:<account>:function:<function_name>/invocations?Qualifier=$LATEST",
          "responses": {
            "default": {
              "statusCode": "200"
            }
          },
          "requestParameters": {
            "integration.request.header.X-Amz-Invocation-Type": "method.request.header.X-Amz-Invocation-Type"
          },
          "type": "aws"
        }
      }
    }
  },
  "definitions": {
    "Empty": {
      "type": "object",
      "title": "Empty Schema"
    }
  }
}

【讨论】:

  • 正如我已经定义的'integration.request.header.X-Amz-Invocation-Type': "'Event'",,不需要发送额外的标头数据,或使用标头映射。正如我之前指出的,它现在似乎可以正常工作,没有任何额外的变化。
猜你喜欢
  • 1970-01-01
  • 2017-11-30
  • 1970-01-01
  • 2016-03-16
  • 2020-05-23
  • 2023-02-14
  • 2019-04-17
  • 1970-01-01
  • 2019-05-08
相关资源
最近更新 更多