2017-02-12 27 views
1

我当前的堆栈是AWS API网关 - > AWS Lambda - > swagger-node + swagger-express-mw + aws-serverless-express。AWS API网关不能GET /当功能长时间

所以我的Swagger API作为一个node.js Lambda函数托管,并通过API网关的aws_proxy调用。这工作很好。唯一的问题是,当功能过长(冷启动?)时,我得到一个Cannot GET /作为我首先调用的每个URL的输出。从第二次请求开始,它运行速度非常快。有关于此的任何想法?

我不认为它来自API网关集成超时,因为它是30秒。函数直接通过lambda函数调用最慢的时间大约是2.5s,当它更频繁地调用时,通常不会超过150ms。我还将该功能的Lambda Timeout的时间增加到了10秒,所以从那里也不应该出现错误。

通过API网关首先调用

响应体

Cannot GET /hello 

响应头从测试请求日志

{ 
    "x-powered-by": "Express", 
    "x-content-type-options": "nosniff", 
    "content-type": "text/html; charset=utf-8", 
    "content-length": "18", 
    "date": "Sun, 19 Feb 2017 15:00:11 GMT", 
    "connection": "close", 
    "X-Amzn-Trace-Id": "<TRACE-ID>" 
} 

日志通过API网关第二次调用从测试请求

Execution log for request test-request 
Sun Feb 19 15:00:07 UTC 2017 : Starting execution for request: test-invoke-request 
Sun Feb 19 15:00:07 UTC 2017 : HTTP Method: GET, Resource Path: /hello 
Sun Feb 19 15:00:07 UTC 2017 : Method request path: {} 
Sun Feb 19 15:00:07 UTC 2017 : Method request query string: {} 
Sun Feb 19 15:00:07 UTC 2017 : Method request headers: {} 
Sun Feb 19 15:00:07 UTC 2017 : Method request body before transformations: 
Sun Feb 19 15:00:07 UTC 2017 : Endpoint request URI: https://lambda.eu-central-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:eu-central-1:<ACCOUNT-ID>:function:api/invocations 
Sun Feb 19 15:00:07 UTC 2017 : Endpoint request headers: {x-amzn-lambda-integration-tag=test-request, Authorization=**************************************************************************************************************************************************************************************************************************************************************************************************************************4b0637, X-Amz-Date=20170219T150007Z, x-amzn-apigateway-api-id=965h04axki, Accept=application/json, User-Agent=AmazonAPIGateway_965h04axki, X-Amz-Security-Token=<SECURITY-TOKEN> 
Sun Feb 19 15:00:07 UTC 2017 : Endpoint request body after transformations: {"resource":"/hello","path":"/hello","httpMethod":"GET","headers":null,"queryStringParameters":null,"pathParameters":null,"stageVariables":null,"requestContext":{"accountId":"<ACCOUNT-ID>","resourceId":"ll6gw8","stage":"test-invoke-stage","requestId":"test-invoke-request","identity":{"cognitoIdentityPoolId":null,"accountId":"<ACCOUNT-ID>","cognitoIdentityId":null,"caller":"<ACCOUNT-ID>","apiKey":"test-invoke-api-key","sourceIp":"test-invoke-source-ip","accessKey":"<ACCESS-ID>","cognitoAuthenticationType":null,"cognitoAuthenticationProvider":null,"userArn":"arn:aws:iam::<ACCOUNT-ID>:root","userAgent":"Apache-HttpClient/4.5.x (Java/1.8.0_102)","user":"<ACCOUNT-ID>"},"resourcePath":"/hello","httpMethod":"GET","apiId":"965h04axki"},"body":null,"isBase64Encoded":false} 
Sun Feb 19 15:00:11 UTC 2017 : Endpoint response body before transformations: {"statusCode":404,"body":"Cannot GET /hello\n","headers":{"x-powered-by":"Express","x-content-type-options":"nosniff","content-type":"text/html; charset=utf-8","content-length":"18","date":"Sun, 19 Feb 2017 15:00:11 GMT","connection":"close"},"isBase64Encoded":false} 
Sun Feb 19 15:00:11 UTC 2017 : Endpoint response headers: {x-amzn-Remapped-Content-Length=0, x-amzn-RequestId=19f8554e-f6b4-11e6-8184-d3ccf0ccf643, Connection=keep-alive, Content-Length=267, Date=Sun, 19 Feb 2017 15:00:11 GMT, Content-Type=application/json} 
Sun Feb 19 15:00:11 UTC 2017 : Method response body after transformations: Cannot GET /hello 

Sun Feb 19 15:00:11 UTC 2017 : Method response headers: {x-powered-by=Express, x-content-type-options=nosniff, content-type=text/html; charset=utf-8, content-length=18, date=Sun, 19 Feb 2017 15:00:11 GMT, connection=close, X-Amzn-Trace-Id=Root=1-58a9b2f7-91fc7371e41d6ae9c2fbf64d} 
Sun Feb 19 15:00:11 UTC 2017 : Successfully completed execution 
Sun Feb 19 15:00:11 UTC 2017 : Method completed with status: 404 

日志

响应体

"Hello, stranger!" 

响应头

{ 
    "x-powered-by": "Express", 
    "access-control-allow-origin": "*", 
    "content-type": "application/json; charset=utf-8", 
    "content-length": "18", 
    "etag": "W/\"12-E1p7iNXxJ4trMdmFBhlU9Q\"", 
    "date": "Mon, 13 Feb 2017 20:12:36 GMT", 
    "connection": "close", 
    "X-Amzn-Trace-Id": "<Trace-ID>" 
} 

日志

Execution log for request test-request 
Mon Feb 13 20:12:36 UTC 2017 : Starting execution for request: test-invoke-request 
Mon Feb 13 20:12:36 UTC 2017 : HTTP Method: GET, Resource Path: /hello 
Mon Feb 13 20:12:36 UTC 2017 : Method request path: {} 
Mon Feb 13 20:12:36 UTC 2017 : Method request query string: {} 
Mon Feb 13 20:12:36 UTC 2017 : Method request headers: {} 
Mon Feb 13 20:12:36 UTC 2017 : Method request body before transformations: 
Mon Feb 13 20:12:36 UTC 2017 : Endpoint request URI: https://lambda.eu-central-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:eu-central-1:<LAMBDA-FUNCTION-ID>:function:api/invocations 
Mon Feb 13 20:12:36 UTC 2017 : Endpoint request headers: {x-amzn-lambda-integration-tag=test-request, Authorization=*******************************************************************************************************************************************************************************************************************************************************************************************************************************************3e1b18, X-Amz-Date=20170213T2Z, x-amzn-apigateway-api-id=965h04axki, X-Amz-Source-Arn=arn:aws:execute-api:eu-central-1:<ACCOUNT-ID>:965h04axki/null/GET/hello, Accept=application/json, User-Agent=AmazonAPIGateway_965h04axki, X-Amz-Security-Token=<TOKEN> 
Mon Feb 13 20:12:36 UTC 2017 : Endpoint request body after transformations: {"resource":"/hello","path":"/hello","httpMethod":"GET","headers":null,"queryStringParameters":null,"pathParameters":null,"stageVariables":null,"requestContext":{"accountId":"<ACCOUNT-ID>","resourceId":"ll6gw8","stage":"test-invoke-stage","requestId":"test-invoke-request","identity":{"cognitoIdentityPoolId":null,"accountId":"<ACCOUNT-ID>","cognitoIdentityId":null,"caller":"427402682812","apiKey":"test-invoke-api-key","sourceIp":"test-invoke-source-ip","accessKey":"<ACCESS-KEY>","cognitoAuthenticationType":null,"cognitoAuthenticationProvider":null,"userArn":"arn:aws:iam::<ACCOUNT-ID>:root","userAgent":"Apache-HttpClient/4.5.x (Java/1.8.0_102)","user":"<ACCOUNT-ID>"},"resourcePath":"/hello","httpMethod":"GET","apiId":"965h04axki"},"body":null,"isBase64Encoded":false} 
Mon Feb 13 20:12:36 UTC 2017 : Endpoint response body before transformations: {"statusCode":200,"body":"\"Hello, stranger!\"","headers":{"x-powered-by":"Express","access-control-allow-origin":"*","content-type":"application/json; charset=utf-8","content-length":"18","etag":"W/\"12-E1p7iNXxJ4trMdmFBhlU9Q\"","date":"Mon, 13 Feb 2017 20:12:36 GMT","connection":"close"},"isBase64Encoded":false} 
Mon Feb 13 20:12:36 UTC 2017 : Endpoint response headers: {x-amzn-Remapped-Content-Length=0, x-amzn-RequestId=c3354327-f228-11e6-8c1d-ed11cc413770, Connection=keep-alive, Content-Length=315, Date=Mon, 13 Feb 2017 20:12:36 GMT, Content-Type=application/json} 
Mon Feb 13 20:12:36 UTC 2017 : Method response body after transformations: "Hello, stranger!" 
Mon Feb 13 20:12:36 UTC 2017 : Method response headers: {x-powered-by=Express, access-control-allow-origin=*, content-type=application/json; charset=utf-8, content-length=18, etag=W/"12-E1p7iNXxJ4trMdmFBhlU9Q", date=Mon, 13 Feb 2017 20:12:36 GMT, connection=close, X-Amzn-Trace-Id=Root=1-58a21334-8ea6c4b5944eebb873bc7d2e} 
Mon Feb 13 20:12:36 UTC 2017 : Successfully completed execution 
Mon Feb 13 20:12:36 UTC 2017 : Method completed with status: 200 

回答

0

我想回应 “不能GET /” 从你的lambda表达式本身到来。您是否可以检查API网关CW日志(或控制台中的测试调用功能)以查看第一次调用中的集成请求和响应中的不同之处?

+0

好一点。我通过API网关添加了来自第一次和第二次调用的日志 –

+0

是的,它肯定来自Lambda函数,它似乎是无服务器的快速模块。您可以在Lambda设置中增加Lambda函数超时吗?如果快速服务器的初始化时间超过2.5秒,这可能会有所帮助。 –

+0

@ JackKohn-AWS thx的提示。我增加了它,现在它工作。但是,我第一次没有拿到500美元,而是404美元。我把原木换成了顶级。 –

0

我没有看到任何有关它的真实文档(只是this Medium post),但我也经历过这样一个事实,即Lambda可以在第一次调用之前被冻结,或者在长时间未调用它的情况下。

一种解决方法是安排定期调用来唤醒你的λ,用Amazon CloudWatch Events

+0

当然,我知道这个解决方案,但在我看来,这只是一个不好的解决方案。如果没有更好的产品,我会在生产中做到这一点,但我认为这个主题值得一个问题,以获得一个迷人的解决方案;)因为如果AWS想推动Lambda向前不应该有这样的错误 –