-
|
Without AWS PowerTools, a minimal Lambda function using a container image looks like below. This work flawlessly. app.py import logging
logger = logging.getLogger()
logger.setLevel("DEBUG")
def handler(event, context):
logger.debug("hello")
return`However switching to AWS PowerTools as below, I come up an error. It looks like AWS PowerTools is incompatible with awslambdaric. app.py from aws_lambda_powertools import Logger
logger = Logger()
def handler(event, context):
logger.debug("hello")
returnThe docker build and run are as below: docker build -t test -F Dockerfile .
docker run --rm -p 9000:8080 \
-e POWERTOOLS_DEBUG=true \
-e POWERTOOLS_LOG_LEVEL=DEBUG \
-v $(PWD)/app.py:/var/task/app.py \
testDockerfile # syntax=docker/dockerfile:1
ARG PYTHON_VERSION=3.13
FROM public.ecr.aws/lambda/python:${PYTHON_VERSION} AS lambda
ENV PYTHONUNBUFFERED=1 \
PYTHONWARNINGS=error \
POWERTOOLS_DEBUG=true
RUN <<EOF
mkdir -p /opt/{extensions,python}
pip install --no-cache-dir --target /opt/python aws-lambda-powertools
EOF
COPY --link --chmod=0644 app.py ${LAMBDA_TASK_ROOT}/app.py
CMD [ "app.handler" ] |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
It looks I found the root clause. aws_lambda_powertools/init.py -> call set_package_logger_handler() -> call powertools_debug_is_set() The Exception is caught in awslambdaric In addition, an environment To work with container as a Lambda runtime, a workaround is to capture all warnings while importing AWS PowerTools. import logging
import warnings
with warnings.catch_warnings(record=True) as captured_warnings:
warnings.filterwarnings("always", category=UserWarning)
from aws_lambda_powertools import Logger
for warning_message in captured_warnings:
logging.warning(warning_message)
logger = Logger()
# logger = logging.getLogger()
# logger.setLevel("DEBUG")
def handler(event, context):
logger.debug("hello")
returnThis will result a normal behavior with the logged UserWarnings without exit the Lambda. This might not be ideal in production but it is useful for local debugging. 10 Dec 2025 14:50:58,204 [INFO] (rapid) exec '/var/runtime/bootstrap' (cwd=/var/task, handler=)
START RequestId: e36d708a-93fe-4ae7-8759-0990f7064d64 Version: $LATEST
10 Dec 2025 14:50:59,783 [INFO] (rapid) INIT START(type: on-demand, phase: init)
10 Dec 2025 14:50:59,783 [INFO] (rapid) Starting runtime without AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN , Expected?: false
[WARNING] 2025-12-10T14:50:59.868Z {message : UserWarning('POWERTOOLS_DEBUG environment variable is enabled. Setting logging level to DEBUG.'), category : 'UserWarning', filename : '/opt/python/aws_lambda_powertools/package_logger.py', lineno : 20, line : None}
2025-12-10 14:50:59,868 aws_lambda_powertools.logging.logger [DEBUG] Adding filter in root logger to suppress child logger records to bubble up
[DEBUG] 2025-12-10T14:50:59.868Z Adding filter in root logger to suppress child logger records to bubble up
2025-12-10 14:50:59,868 aws_lambda_powertools.logging.logger [DEBUG] Marking logger service_undefined as preconfigured
[DEBUG] 2025-12-10T14:50:59.868Z Marking logger service_undefined as preconfigured
10 Dec 2025 14:50:59,869 [INFO] (rapid) INIT RTDONE(status: success)
10 Dec 2025 14:50:59,869 [INFO] (rapid) INIT REPORT(durationMs: 86.483000)
10 Dec 2025 14:50:59,869 [INFO] (rapid) INVOKE START(requestId: 0729ba63-16b9-4084-913c-8ff3d23587d3)
{"level":"DEBUG","location":"handler:17","message":"hello","timestamp":"2025-12-10 14:50:59,870+0000","service":"service_undefined"}
10 Dec 2025 14:50:59,871 [INFO] (rapid) INVOKE RTDONE(status: success, produced bytes: 0, duration: 1.676000ms)
END RequestId: 0729ba63-16b9-4084-913c-8ff3d23587d3
REPORT RequestId: 0729ba63-16b9-4084-913c-8ff3d23587d3 Init Duration: 0.12 ms Duration: 88.71 ms Billed Duration: 89 ms Memory Size: 3008 MB Max Memory Used: 3008 MB
^C10 Dec 2025 14:57:55,773 [INFO] (rapid) Received signal signal=interrupt
10 Dec 2025 14:57:55,773 [INFO] (rapid) Shutting down...
10 Dec 2025 14:57:55,774 [WARNING] (rapid) Reset initiated: SandboxTerminated
10 Dec 2025 14:57:55,774 [INFO] (rapid) Sending SIGKILL to runtime-1(17).
10 Dec 2025 14:57:55,778 [INFO] (rapid) Waiting for runtime domain processes termination |
Beta Was this translation helpful? Give feedback.
It looks I found the root clause.
aws_lambda_powertools/init.py -> call set_package_logger_handler() -> call powertools_debug_is_set()
As
POWERTOOLS_DEBUG=true,powertools_debug_is_set()triggers warnings.warn() which throws an ExceptionThe Exception is caught in awslambdaric
bootstrap.pyand resultssys.exit(1)https://github.com/aws/aws-lambda-python-runtime-interface-client/blob/3f43f4d089600669435038d7e1fa41145d9ff94f/awslambdaric/bootstrap.py#L506-L516
In addition, an environment
PYTHONWARNINGS=erroris set in AWSpublic.ecr.aws/lambda/python:3.13 imageTo work with container as a Lambda runtime, a workaround is to capture all warnings while importing AWS PowerTools.