Lucas Carneiro
Lucas Carneiro

Reputation: 570

How to limit payload size for a specific API Gateway route

I currently have a lambda that is triggered by requests made to an API Gateway route, and I made some research about how to set a payload limit (e.g. 2kb) for this route. My goal is to guarantee that my lambda will never receive a large input to deal with, so it will have a slight execution time and low costs.

I found that the default payload limit for AWS API Gateway is 10 MB, while the default limit to AWS Lambda is 6 MB. Both of them cannot be increased.

However, I did not found any docs or discussion about how to decrease it. Is it possible? Are there any other AWS services that I should use as a middleware between API Gateway and my lambda to limit the received input? Or should I solve it by another approach?

Upvotes: 3

Views: 3152

Answers (1)

K Mo
K Mo

Reputation: 2155

AWS API Gateway does not have the functionality you are describing.

What you are describing I would consider a WAF function, and indeed AWS WAF does have this functionality.

https://docs.aws.amazon.com/waf/latest/developerguide/web-acl-size-conditions.html

I'm hesitant to add the following as it is a complete mis-use of the toolset, but...

A dirty and likely an inexact method to restrict payload size using just API Gateway would be check the length of the body of the incoming request with an IF statement in a body mapping template in the Integration Response, and if it is over a certain length, ignore it.

Example assuming Content-Type is application/json:

#set($allParams = $input.params())
{
#if ($input.body.length() < 5000)
    "request" : $input.json('$')
#else
    "request" : null
#end
}

The 5000 is just an arbitrary number, I haven't done the maths.

Doing this would still lead to your Lambda function being invoked even if the payload was to big (well long), and the Lambda would need to be able to deal with an empty request, but it will never receive a request with more than 5000 characters.

Upvotes: 1

Related Questions