AWS Lambda: Error 504 when returning a large dataset

I have a Lambda function that takes multiple arrays (~ 6) and returns all possible combinations.

When there are several hundred possible combinations, Lambda is successful. However, when there are several thousand possibilities, I get a rejection (answer below).

Please note that I am compressing / decompressing JSON in both directions to make the payload as small as possible.

I run this with maximum memory (1536) and a timeout of 20 seconds.

Any thoughts on what causes this?

{ "state": "rejected", "reason": { "name": "StatusCodeError", "statusCode": 504, "message": "504 - [object Object]", "error": { "message": "Endpoint request timed out" }, "options": { "uri": "https://blahblah/prod/getCombinations", "method": "POST", "timeout": 120000, "json": {... }, "simple": true, "resolveWithFullResponse": false }, "response": { "statusCode": 504, "body": { "message": "Endpoint request timed out" }, "headers": { "content-type": "application/json", "content-length": "41", "connection": "close", "date": "Thu, 20 Aug 2015 20:39:53 GMT", "x-amzn-requestid": "965d3b8d-477b-11e5-99d6-4102846d4b1e", "x-cache": "Error from cloudfront", "via": "1.1 b1103856e287e98f322630821d3c6e5b.cloudfront.net (CloudFront)", "x-amz-cf-id": "Dhk7ylTq6RDE74smC8uF8ajms8rpU0fp2dnexn4_I3qIXgvrrsg48w==" }, "request": { "uri": { "protocol": "https:", "slashes": true, "auth": null, "host": "blahblah", "port": 443, "hostname": "blahblah", "hash": null, "search": null, "query": null, "pathname": "/prod/getCombinations", "path": "/prod/getCombinations", "href": "https://blahblah" }, "method": "POST", "headers": { "accept": "application/json", "content-type": "application/json", "content-length": 10201 } } } } } 
+4
source share
1 answer

You can read the Amazon CloudFront 504 answer "Final request to the endpoint", literally, i.e. 504 Gateway Timeout usually indicates that the server acted as a gateway or proxy and did not receive a timely response from the upstream server.

CloudFront conforms to this standard, as shown in Configuring error responses , for example:

Server errors indicate a problem with the source server. For example, the HTTP server is busy or unavailable. If a server error occurs, either your source server returns an HTTP status code in the range 500 to CloudFront, or CloudFront does not receive a response from your source server for a certain period of time and receives a status code 504 (gateway timeout). [emphasis mine]

As Michael noted, CloudFront self -request timeout for user sources is 30 seconds:

DELETE, OPTIONS, PATCH, POST and POST requests. If the source does not respond within 30 seconds, CloudFront disconnects the connection and does not attempt to contact the source. The client can resend the request if necessary.

Given that the AWS Lambda function has a shorter timeout of 20 seconds, I would suggest that your dataset would be too large to be transmitted and processed through a POST request (presumably through the Amazon API Gateway ?!) For 20 seconds.

  • If your dataset is larger than a 1.5 megabyte lambda function with a run time of 20 seconds, you can either chop your data into smaller pieces, or transfer your calculations from Lambda to something more scalable - by chance AWS Compute Blog just shows a good example, how to make Amazon ECS easier for longer tasks like this, see Chris Barclay Better Together: Amazon ECS and AWS Lambda .
+2
source

All Articles