Camilo Ortegón
Camilo Ortegón

Reputation: 3692

Catching timeout errors in AWS Api Gateway

Since Api Gateway time limit is 10 seconds to execute any request I'm trying to deal with timeout errors, but a haven't found a way to catch and respond a custom message.

Context of the problem: I have a function that takes less than 2 seconds to execute, but when the function performs a cold start sometimes it takes more than 10 seconds creating a connection with DynamoDB in Java. I've already optimize my function using threads but I still cannot keep between the 10-seconds limit for the initial call.

I need to find a way to deliver a response model like this:

{
  "error": "timeout"
}

To find a solution I created a function in Lambda that intentionally responds something after 10 seconds of execution. Doing the integration with Api Gateway I'm getting this response:

Request: /example/lazy
Status:
Latency: ms

Response Body

{
  "logref": "********-****-****-****-1d49e75b73de",
  "message": "Timeout waiting for endpoint response"
}

In documentation I found that you can catch this errors using HTTP status regex in Integration Response. But I haven't find a way to do so, and it seems that nobody on the Internet is having my same problem, as I haven't find this specific message in any forum.

I have tried with these regex:

.*"message".*
Timeout.*
.*"status":400.*
.*"status":404.*
.*"status":504.*
.*"status":500.*

Anybody knows witch regex I should use to capture this "message": "Timeout... ?

Upvotes: 4

Views: 5788

Answers (2)

amit
amit

Reputation: 166

You can improve the cold start time by allocating more memory to your Lambda function. With the default 512MB, I am seeing cold start times of 8-9 seconds for functions written in Java. This improves to 2-3 seconds with 1536MB of memory.

Amazon says that it is the CPU allocation that is really important, but there is not way to directly increase it. CPU allocation increases proportionately to memory.

And if you want close to zero cold start times, keeping the function warm is the way to go, as described here.

Upvotes: 3

Balaji
Balaji

Reputation: 1046

You are using Test Invoke feature from console which has a timeout limit of 10 seconds. But, the deployed API's timeout is 30 seconds as mentioned here. So, that should be good enough to handle Lambda cold start case. Please deploy and then test using the api link. If that times out because your endpoint takes more than 30 seconds, the response would be:

{"message": "Endpoint request timed out"}

To clarify, you can configure your method response based on the HTTP status code of integration response. But in case of timeout, there is no integration response. So, you cannot use that feature to configure the method response during timeout.

Upvotes: 3

Related Questions