4

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.

jth
  • 369
  • 3
  • 8
  • You have to just use try/except and log it yourself. – Marcin Oct 27 '22 at 00:25
  • Fully with you on "This seems like a problem that should have a standard solution". I'm looking for a slightly enhanced experience, to go quickly from error peaks in a CloudWatch metric to the corresponding lambda's unhandled exception logs... It isn't looking very promising. I can't even find a quick way to jump to the unhandled exceptions by poor man's time range filtering + text filtering... – Uldis Barbans Aug 04 '23 at 11:59
  • @UldisBarbans [Here](https://github.com/ASFHyP3/hyp3/blob/v3.9.7/lib/lambda_logging/lambda_logging/__init__.py) is what we came up with. Use like [this](https://github.com/ASFHyP3/hyp3/blob/v3.9.7/apps/start-execution-worker/src/start_execution_worker.py#L36-L41). – jth Aug 04 '23 at 23:03

0 Answers0