【问题标题】:How to invoke Lambda asyncronously from API Gateway?如何从 API Gateway 异步调用 Lambda?
【发布时间】:2020-11-13 00:30:15
【问题描述】:

根据here,我认为设置X-Amz-Invocation-Type: Event 的标头应该将我的Lambda 调用设置为异步。

但是,通过在我的 Lambda 函数的开头放置一个 import time;time.sleep(5000),并向我的 API 网关发送请求,我发现:

$ aws apigateway get-integration --rest-api-id <api-id> \
    --resource-id <resource-id> \
    --http-method POST | jq -r '.requestParameters'
{
  "integration.request.header.X-Amz-Invocation-Type": "'Event'"
}
$ aws apigateway get-integration --rest-api-id <api-id> \
    --resource-id <resource-id> \
    --http-method POST | jq -r '.uri'
arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:<account-id>:function:[...]Lambda-4HOA0ZSFAYCI/invocations
$ curl https://<api-id>.execute-api.us-east-1.amazonaws.com/LATEST/<path> -d '{}'
{"message": "Endpoint request timed out"}

[here I removed the sleep from my lambda function and made it return immediately]

$ curl https://<api-id>.execute-api.us-east-1.amazonaws.com/LATEST/<path> -d '{}'
{"body": "Request OK", "headers": {"Content-Type": "application/json"}, "statusCode": 200}

假设文档是准确的,我最好的猜测是我以某种方式错误配置了集成 - 也许"'Event'" 不正确,应该是"Event"?但是,我很确定需要单引号来划分静态值(与从请求中解析的值相反)。特别怀疑我的responseParameters不对:

$ aws apigateway get-integration --rest-api-id <api-id> \
    --resource-id dzv1zj \
    --http-method POST | jq -r '.integrationResponses'
{
  "200": {
    "responseTemplates": {
      "application/json": null
    },
    "statusCode": "200"
  }
}

应该null 有一些VTL 静态返回200 OK

至于替代方案:我看到invoke-asyncdeprecated。我真的不想去API Gateway -&gt; SNS -&gt; Lambda 的开销。

编辑:以下是通过控制台上的“测试”选项调用 API 的日志:

请求测试请求的执行日志

Wed Mar 07 17:24:57 UTC 2018 : Starting execution for request: test-invoke-request
Wed Mar 07 17:24:57 UTC 2018 : HTTP Method: POST, Resource Path: /<path>
Wed Mar 07 17:24:57 UTC 2018 : Method request path: {}
Wed Mar 07 17:24:57 UTC 2018 : Method request query string: {}
Wed Mar 07 17:24:57 UTC 2018 : Method request headers: {}
Wed Mar 07 17:24:57 UTC 2018 : Method request body before transformations: {"abc":"def"}
Wed Mar 07 17:24:57 UTC 2018 : Endpoint request URI: https://lambda.us-east-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:us-east-1:<account-id>:function:[...]Lambda-4HOA0ZSFAYCI/invocations
Wed Mar 07 17:24:57 UTC 2018 : Endpoint request headers: {X-Amz-Date=20180307T172457Z, x-amzn-apigateway-api-id=<api-id>, Accept=application/json, User-Agent=AmazonAPIGateway_<api-id>, Host=lambda.us-east-1.amazonaws.com, X-Amz-Content-Sha256=2c3fbda5f48b04e39d3a87f89e5bd00b48b6e5e3c4a093de65de0a87b8cc8b3b, X-Amzn-Trace-Id=Root=1-5aa02069-8670eb5d98dbc4ade9df03d8, x-amzn-lambda-integration-tag=test-request, Authorization=**********************************************************************************************************************************************************************************************************************************************************************************************************************************************bfe3de, X-Amz-Source-Arn=arn:aws:execute-api:us-east-1:<account-id>:<api-id>/null/POST/<path>, X-Amz-Invocation-Type=Event, X-Amz-Security-Token=[REDACTED] [TRUNCATED]
Wed Mar 07 17:24:57 UTC 2018 : Endpoint request body after transformations: {"abc":"def"}
Wed Mar 07 17:24:57 UTC 2018 : Sending request to https://lambda.us-east-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:us-east-1:<account-id>:function:[...]Lambda-4HOA0ZSFAYCI/invocations
Wed Mar 07 17:24:57 UTC 2018 : Received response. Integration latency: 43 ms
Wed Mar 07 17:24:57 UTC 2018 : Endpoint response body before transformations: 
Wed Mar 07 17:24:57 UTC 2018 : Endpoint response headers: {x-amzn-Remapped-Content-Length=0, Connection=keep-alive, x-amzn-RequestId=75501cbf-222c-11e8-a1fc-2b19f19a9429, Content-Length=0, Date=Wed, 07 Mar 2018 17:24:57 GMT, X-Amzn-Trace-Id=root=1-5aa02069-8670eb5d98dbc4ade9df03d8;sampled=0}
Wed Mar 07 17:24:57 UTC 2018 : Method response body after transformations: 
Wed Mar 07 17:24:57 UTC 2018 : Method response headers: {X-Amzn-Trace-Id=sampled=0;root=1-5aa02069-8670eb5d98dbc4ade9df03d8, Content-Type=application/json}
Wed Mar 07 17:24:57 UTC 2018 : Successfully completed execution
Wed Mar 07 17:24:57 UTC 2018 : Method completed with status: 200

【问题讨论】:

    标签: amazon-web-services aws-lambda


    【解决方案1】:

    现在我觉得自己很愚蠢 - 我需要部署我的 API。这似乎是 API 网关相当于“你试过关闭它然后再打开它吗?” :)

    【讨论】:

      【解决方案2】:

      我找到了斯库博的答案,但直到很久以后才启发我,所以我发布了相同的内容,但为任何未来迷失的灵魂附上了截图

      【讨论】:

      • DenverCoder9道歉!在我的辩护中,我(可能 - 不久前!)使用 AWS CLI 进行部署,其用法更不言自明:)
      • @scubbo 没有伤害!它帮助了我,你得到了你的 2 票。最后,真正的问题是每次集成更改时都要部署 terraform。
      猜你喜欢
      • 1970-01-01
      • 2018-01-23
      • 2019-04-17
      • 2017-04-27
      • 1970-01-01
      • 2018-01-19
      • 2021-06-23
      • 2020-03-04
      • 2021-01-10
      相关资源
      最近更新 更多