如何从API网关异步调用Lambda?

时间:2018-03-07 06:45:51

标签: amazon-web-services aws-lambda

根据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 -> SNS -> Lambda

编辑:以下是通过&#34;测试&#34;调用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

2 个答案:

答案 0 :(得分:1)

我找到了scubbo的答案,但直到很久以后才启发我,所以我发布了它,但附有截图,供将来迷失的灵魂

enter image description here

答案 1 :(得分:0)

现在我觉得很蠢 - 我需要部署我的API。这似乎是API网关相当于&#34;您是否尝试将其关闭并再次打开?&#34; :)