title: how to handle lambda error from the API gateway
body: <p>Using AWS I have set up a Lambda function using Python to act as a proxy. From the API Gateway I have linked the lambda (with 'Use Lambda Proxy integration' checkbox selected) to my API Gateway. Everything is working fine except that the max size of 6mb for the lambda function is getting in the way of my large files being returned successfully. The lambda prints an error that says "Response payload size exceeded maximum allowed payload size. Function.ResponseSizeTooLarge" and the API Gateway gets this message from lambda and prints 'Received response. Status 200', 'Lambda execution failed with status 200 due to customer function error: Response payload size exceeded maximum allowed payload size' and then API gateway spits out a 502 with the message 'Internal Server Error'. </p>
  
  <p>What I would expect to happen is that API Gateway would see this error message and automatically return a 413 Request Entity Too Large so that i can react to that specific error status from my webapp. </p>
  
  <p>In the past I have been able to use the Integration Response section of the api gateway to look for this error message and override the outgoing status code like so:</p>
  
  <pre><code>#set($inputRoot = $input.path('$'))
  $input.json("$")
  #if($inputRoot.toString().contains("ResponseSizeTooLarge"))
  #set($context.responseOverride.status = 413)
  #end
  </code></pre>
  
  <p>Unfortunately because i am using this lambda function from the api gateway as a lambda proxy I am unable to use the Integration Response to search for this specific message and override the outgoing status code.</p>
  
  <p><strong>Is there another way I can get the API Gateway to see that this 200 it is getting from the lambda actually has an error message and should be a 413 anyway?</strong> The only other way i can think to do this is check the payload size from the lambda before returning it to the API Gateway manually and then changing the status code before returning if its too large. I do not want to do this because the only way I have seen to get byte size of objects in python is by importing sys and using:</p>
  
  <pre><code>sys.getsizeof(lambdaResponse)
  </code></pre>
  
  <p>and that seems to only be returning a size of ~258 even though the actual size of its contents are much larger than that. I also do not want to be checking the size of every payload because it is probably adding unnecessary delay to responses that are not too large to be handled.</p>