powertools-lambda-python: Bug: `log_uncaught_exceptions` only works locally and not on lambda

Expected Behaviour

When log_uncaught_exceptions=True, uncaught exceptions in a lambda are handled and transformed to a structured log.

Current Behaviour

Uncaught exceptions are printed unstructured.

Code snippet

from aws_lambda_powertools import Logger
from aws_lambda_powertools.utilities.typing import LambdaContext

logger = Logger(log_uncaught_exceptions=True)

def lambda_handler(event: dict, context: LambdaContext) -> str:
    raise(Exception("asdf"))

Possible Solution

Probably none. Document this behavior.

I don’t think sys.excepthook can be overridden in a lambda container, but I have no proof because the original forum thread that details this has been deleted by AWS with no replacement.

Steps to Reproduce

Set log_uncaught_exceptions=True in an actual running lambda. If log_uncaught_exceptions=True on a local machine, the behavior runs just as intended.

AWS Lambda Powertools for Python version

latest

AWS Lambda function runtime

3.9

Packaging format used

Lambda Layers

Debugging logs

No response

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Comments: 18 (12 by maintainers)

Most upvoted comments

Good news! Met with Lambda engineering to rule out options, and we have a potential solution for all runtimes.

I say potential because security review might take a while to ensure there’s no angle for abuse.

Setting expectations

One caveat tho:

It might be applied to newer runtimes only (pending investigation). This means, if we can’t backport this will only work for Python 3.11+.

Lambda will literally change the component responsible for handling every customer’s request to ensure each programming language hook for unhandled exception works as expected without side effects or abuse - we’ll take due diligence as expected for any component in the critical path.

We’ll keep you posted; progress nevertheless.

I appreciate everyone’s patience on this.

TL;DR - we’re still working with the AWS Lambda team on this, and related issues with the newly launched Advanced Logging Control like the warning above.

I usually capture the warnings using logging.captureWarnings(True) which I thought would suffice in this case. Also, I set the ALC to the most granular level and manage the log level solely in Powertools via the POWERTOOLS_LOG_LEVEL variable.

Not yet @matteocontrini, meeting the team early next week for a follow up and post here if anything changes. Appreciate the patience

Updating this issue… We’re working closely with the Lambda team to investigate the issue and determine if we have a workaround to resolve the issue.

I’ll come back here as soon as I have an update. In the meantime, I’ll update the documentation to suggest not using this code in production and putting this issue in a blocking state.