Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Does AWS PowerTools support Lambda using container image? #7780

Answered by gnought
gnought asked this question in Q&A
Discussion options

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")
 return
07 Dec 2025 07:45:50,538 [INFO] (rapid) exec '/var/runtime/bootstrap' (cwd=/var/task, handler=)
START RequestId: 632c768b-e4ef-4ba5-935e-d96e4721eebd Version: $LATEST
07 Dec 2025 07:45:51,978 [INFO] (rapid) INIT START(type: on-demand, phase: init)
07 Dec 2025 07:45:51,979 [INFO] (rapid) Starting runtime without AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN , Expected?: false
[WARNING] 2025年12月07日T07:45:52.021Z LAMBDA_WARNING: Unhandled exception. The most likely cause is an issue in the function code. However, in rare cases, a Lambda runtime update can cause unexpected function behavior. For functions using managed runtimes, runtime updates can be triggered by a function change, or can be applied automatically. To determine if the runtime has been updated, check the runtime version in the INIT_START log entry. If this error correlates with a change in the runtime version, you may be able to mitigate this error by temporarily rolling back to the previous runtime version. For more information, see https://docs.aws.amazon.com/lambda/latest/dg/runtimes-update.html
  warnings.warn("POWERTOOLS_DEBUG environment variable is enabled. Setting logging level to DEBUG.", stacklevel=2)
Exception ignored in: <socket.socket fd=3, family=2, type=1, proto=6, laddr=('127.0.0.1', 55310), raddr=('127.0.0.1', 9001)>
Traceback (most recent call last):
 File "/var/lang/lib/python3.13/site-packages/awslambdaric/lambda_runtime_client.py", line 108, in post_init_error
 self.call_rapid(
ResourceWarning: unclosed <socket.socket fd=3, family=2, type=1, proto=6, laddr=('127.0.0.1', 55310), raddr=('127.0.0.1', 9001)>
07 Dec 2025 07:45:52,039 [WARNING] (rapid) First fatal error stored in appctx: Runtime.ExitError
07 Dec 2025 07:45:52,039 [WARNING] (rapid) Process runtime-1 exited: exit status 1
07 Dec 2025 07:45:52,039 [INFO] (rapid) INIT RTDONE(status: error)
07 Dec 2025 07:45:52,039 [INFO] (rapid) INIT REPORT(durationMs: 60.936000)
07 Dec 2025 07:45:52,039 [ERROR] (rapid) Init failed error=Runtime exited with error: exit status 1 InvokeID=
07 Dec 2025 07:45:52,039 [WARNING] (rapid) Shutdown initiated: spindown
07 Dec 2025 07:45:52,039 [INFO] (rapid) Waiting for runtime domain processes termination
07 Dec 2025 07:45:52,040 [INFO] (rapid) INIT START(type: on-demand, phase: invoke)
07 Dec 2025 07:45:52,040 [INFO] (rapid) INIT REPORT(durationMs: 0.063000)
07 Dec 2025 07:45:52,040 [INFO] (rapid) INVOKE START(requestId: 3301816c-a1b5-43b8-8cf3-7a9c7bad0344)
07 Dec 2025 07:45:52,040 [ERROR] (rapid) Invoke failed error=Runtime exited with error: exit status 1 InvokeID=3301816c-a1b5-43b8-8cf3-7a9c7bad0344
07 Dec 2025 07:45:52,040 [ERROR] (rapid) Invoke DONE failed: Sandbox.Failure
07 Dec 2025 07:45:52,040 [WARNING] (rapid) Reset initiated: ReleaseFail
07 Dec 2025 07:45:52,040 [INFO] (rapid) Waiting for runtime domain processes termination

The 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 \
		test

Dockerfile

# 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" ]
You must be logged in to vote

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 Exception

The Exception is caught in awslambdaric bootstrap.py and results sys.exit(1)
https://github.com/aws/aws-lambda-python-runtime-interface-client/blob/3f43f4d089600669435038d7e1fa41145d9ff94f/awslambdaric/bootstrap.py#L506-L516

In addition, an environment PYTHONWARNINGS=error is set in AWS public.ecr.aws/lambda/python:3.13 image

To work with container as a Lambda runtime, a workaround is to capture all warnings while importing AWS PowerTools.

import logging
i...

Replies: 1 comment

Comment options

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 Exception

The Exception is caught in awslambdaric bootstrap.py and results sys.exit(1)
https://github.com/aws/aws-lambda-python-runtime-interface-client/blob/3f43f4d089600669435038d7e1fa41145d9ff94f/awslambdaric/bootstrap.py#L506-L516

In addition, an environment PYTHONWARNINGS=error is set in AWS public.ecr.aws/lambda/python:3.13 image

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")
 return

This 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月10日T14: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月10日T14: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月10日T14: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
You must be logged in to vote
0 replies
Answer selected by gnought
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant

AltStyle によって変換されたページ (->オリジナル) /