Deep Dive: Lambda’s Response Payload Size Limit (1/2)

{
"errorType": "Function.ResponseSizeTooLarge",
"errorMessage": "Response payload size exceeded maximum allowed
payload size (6291556 bytes)."
}
Photo by Siora Photography on Unsplash

What is the limit?

The limit in question is listed on the Lambda quotas page as “Invocation payload (request and response)” and says it’s 6 MB for synchronous invocations.

Breaking the limit

It’s easy to trigger a ResponseSizeTooLarge error. This simple handler function does so by creating a string containing the letter A repeated 7 million times. Each A is one byte so this is well above the limit.

What is the response payload?

The docs for the Invoke action say “the response from the function, or an error object”. More importantly, they show that when Lambda serializes its HTTP response, the payload goes in the body.

Measuring response payloads

Let’s look at another example function handler.

This is incorrect!
Invoke a function using the AWS console and look for /invocations in the dev tools.
This logs 6,291,556 as expected.

API Gateway proxy integrations

API Gateway has a response payload limit of 10 MB. That’s well above Lambda’s limit. Therefore, it’s fair to assume that any payload size problems are caused by Lambda and your code running there.

What about headers?

Have you heard that headers do (or don’t) count toward the payload size limit? The truth is, discussing headers is completely missing the point. I’ll explain.

What about base64 encoding?

It should be obvious by now, but this makes absolutely no difference.

While we’re discussing API Gateway

There is an interesting gotcha when working with Lambda, large payloads, and API Gateway: Lambda still returns a 200 status code even when you get a ResponseSizeTooLarge error.

Summary

  • When the Lambda docs says 6 MB, they mean 6,291,556 bytes.
  • The payload size limit is applied by Lambda.
  • Lambda doesn’t care who or what called Invoke.
  • Lambda calls JSON.stringify on your handler’s return value.
  • Lambda uses UTF-8 encoding in its HTTP responses.
  • Some characters (especially emojis) use more than 1 byte.
  • Use TextEncoder instead of "".length to accurately check the size of your response in bytes.
  • When using API Gateway, calculate the size of the whole return object including custom headers, statusCode, etc. You’ll then know how many bytes can go in the body.
  • Base64 uses 1 byte per 6 bits of binary data. Do what Lambda does and convert your data to a Base64 string before calculating its size.

Part 2

This is part 1 of a 2 part series on Lambda’s payload size limits. If you enjoyed this post, you’ll enjoy the second part too:

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Zac Charles

Zac Charles

Serverless Engineer @ Stedi | AWS Community Builder