Types of possible errors in an AWS Lambda function and how to handle them
An AWS Lambda invocation can raise errors for a variety of reasons1. A few examples are listed below. Invocation errors will make Lambda return a 400-series or 500-series HTTP status code.
For a complete list of invocation errors, please refer to the AWS documentation2.
AWS Lambda Function errors happen when, for example:
AWS Lambda will automatically add the
X-Amz-Function-Error header and a JSON as the response with details about the function error. Because of that, it is a good practice to avoid the code from raising exceptions, since it will expose internal implementation details. This could potentially compromise the application security.
To avoid this issue, developers should catch all exceptions in the
function_handler, log them and return a sanitized and gentle response to the requester.
AWS provides guidance on handling function exceptions for all runtimes supported by Lambda. Below is list with reference pages for each runtime:
Save time spent on debugging applications.
Increase development velocity and quality.
Get actionable insights to your infrastructure.