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

Amazon web services 如何从API网关异步调用Lambda?,amazon-web-services,aws-lambda,Amazon Web Services,Aws Lambda,根据,我认为设置X-Amz-Invocation-Type:Event的头应该将Lambda调用设置为异步 但是,通过放置导入时间;time.sleep(5000)在Lambda函数开始时,向API网关发送请求时,我发现: $ aws apigateway get-integration --rest-api-id <api-id> \ --resource-id <resource-id> \ --http-method POST | jq -r '.r

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

但是,通过放置
导入时间;time.sleep(5000)
在Lambda函数开始时,向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}
null
是否应该有一些静态返回
200 OK

至于替代方案:我看到
invoke async
是。我真的不想承担去
API网关->SNS->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
Wed Mar 07 17:24:57 UTC 2018:开始执行请求:测试调用请求
星期三3月7日17:24:57 UTC 2018:HTTP方法:POST,资源路径:/
2018年3月7日星期三17:24:57 UTC:方法请求路径:{}
2018年3月7日星期三17:24:57 UTC:方法请求查询字符串:{}
2018年3月7日星期三17:24:57 UTC:方法请求头:{}
2018年3月7日星期三17:24:57 UTC:转换前的方法请求正文:{“abc”:“def”}
2018年3月7日星期三17:24:57 UTC:端点请求URI:https://lambda.us-east-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:us-east-1::函数:[……]Lambda-4HOA0ZSFAYCI/调用
2018年3月7日星期三17:24:57 UTC:端点请求标题:{X-Amz-Date=20180307T172457Z,X-amzn-apigateway-api-id=,Accept=application/json,User-Agent=AmazonAPIGateway,Host=lambda.us-east-1.amazonaws.com,X-Amz-Content-Sha256=2C3FBDA5F48B04E39D3A87F89E5BD00B48B6E5E3C4A093DE0A87B8CC8B3B,X-amzn-Trace-id=Root=1-5aa02069-DB86705D98D9D8,X-amzn-amzn-tag=LAMBADA-TED-ETA-ETA-ETA-TESS,集成请求***男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生************************************************************************************************************************************************************************************************************************************************男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生男生******************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************z-Source-Arn=Arn:aws:execute-api:us-east-1::/null/POST/,X-Amz-Invocation-Type=Event,X-Amz-Security-Token=[redact][TRUNCATED]
2018年3月7日星期三17:24:57 UTC:转换后的端点请求正文:{“abc”:“def”}
2018年3月7日星期三17:24:57 UTC:向发送请求https://lambda.us-east-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:us-east-1::函数:[……]Lambda-4HOA0ZSFAYCI/调用
2018年3月7日星期三17:24:57 UTC:收到响应。集成延迟:43毫秒
2018年3月7日星期三17:24:57 UTC:转换前的端点响应主体:
UTC 2018年3月7日星期三17:24:57:端点响应标题:{x-amzn-Remapped-Content-Length=0,连接=keep alive,x-amzn-RequestId=75501cbf-222c-11e8-a1fc-2B19AF19429,内容长度=0,日期=Wed,2018年3月7日17:24:57 GMT,x-amzn-Trace-Id=root=1-5aa02069-8670eb5d98dbc4ade9df03d8;采样=0}
2018年3月7日星期三17:24:57 UTC:转换后的方法响应主体:
2018年3月7日星期三17:24:57 UTC:方法响应标题:{X-Amzn-Trace-Id=sampled=0;root=1-5aa02069-8670eb5d98dbc4ade9df03d8,内容类型=application/json}
2018年3月7日星期三17:24:57 UTC:成功完成执行
2018年3月7日星期三17:24:57 UTC:方法完成,状态:200

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

我找到了scubbo的答案,但它直到很久以后才给我启发,所以我发布了同样的内容,但为将来失去的灵魂提供了截图


很抱歉!为我辩护,我(可能-那是很久以前!)使用AWS CLI进行部署,其用法更加不言自明:)@scubbo没什么害处!这对我和你都有帮助。最终,真正的问题是每次集成更改时都要部署terraform。
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