AWS Lambda provides built-in logging configuration as described here so that log records include timestamp and request ID. Request ID is a UUID for a particular invocation of a Lambda function, so it's important to include it in each log record because it allows the user to query for all log records with a given request ID (using CloudWatch Logs Insights), which allows for retrieving log records from a particular Lambda invocation. (It is not sufficient to simply filter by log stream, as a single log stream can contain logs from multiple invocations if the invocations were run within the same environment, as described here.)
Unfortunately, when a Python AWS Lambda function crashes due to an unhandled exception, the log record for the exception does not include the request ID. The obvious solution is to log unhandled exceptions via the logging
module, so that request ID is included in the log record. This appears to be the cleanest solution but it does not work in this case because overriding sys.excepthook
does not appear to have any effect within a Lambda environment (here is the only other source I can find that references this fact).
Therefore, I'm wondering if there's a recommended way to include request ID in the log record for an unhandled exception in a Python AWS Lambda function? This seems like a problem that should have a standard solution. Otherwise, I will just surround the lambda handler code with a try
/except
to log and re-raise all unhandled exceptions.