Closed
Description
Expected Behaviour
Using the event source decorator with APIGatewayProxyEventV2 will run Lambda function and not fail with TypeError
Current Behaviour
When activating debug mode through either APIGatewayHttpResolver(debug=True)
or POWERTOOLS_EVENT_HANDLER_DEBUG=true
causes TypeError: Object of type APIGatewayProxyEventV2 is not JSON serializable
and no further debug entries get logged.
Code snippet
from aws_lambda_powertools.utilities.data_classes import (
APIGatewayProxyEventV2,
event_source,
)
from aws_lambda_powertools.logging import correlation_paths
from aws_lambda_powertools import Logger
from aws_lambda_powertools.event_handler import APIGatewayHttpResolver
from aws_lambda_powertools.utilities.typing import LambdaContext
logger = Logger()
app = APIGatewayHttpResolver(debug=True)
@logger.inject_lambda_context(correlation_id_path=correlation_paths.API_GATEWAY_HTTP)
@event_source(data_class=APIGatewayProxyEventV2)
def lambda_handler(event: APIGatewayProxyEventV2, context: LambdaContext):
return app.resolve(event, context)
@app.get("/my/path")
def get_hello_universe():
return {"message": "hello universe"}
Possible Solution
Workaround: Enable debug mode and comment event source out
Steps to Reproduce
Create a Lambda function accepting APIGatewayProxyEventV2 requests. Decorate the handler method with logger inject and event source and add a simple route that will just return a small JSON response. Then enable debug mode by one of the supported ways and watch the lambda function fail
AWS Lambda Powertools for Python version
latest (Layer version 18)
AWS Lambda function runtime
3.9
Packaging format used
Lambda Layers
Debugging logs
[ERROR] TypeError: Object of type APIGatewayProxyEventV2 is not JSON serializable
Traceback (most recent call last):
File "/opt/python/aws_lambda_powertools/logging/logger.py", line 354, in decorate
return lambda_handler(event, context)
File "/opt/python/aws_lambda_powertools/middleware_factory/factory.py", line 134, in wrapper
response = middleware()
File "/opt/python/aws_lambda_powertools/utilities/data_classes/event_source.py", line 39, in event_source
return handler(data_class(event), context)
File "/var/task/app.py", line 28, in lambda_handler
return app.resolve(event, context)
File "/opt/python/aws_lambda_powertools/event_handler/api_gateway.py", line 495, in resolve
print(self._json_dump(event), end="")
File "/opt/python/aws_lambda_powertools/event_handler/api_gateway.py", line 688, in _json_dump
return self._serializer(obj)
File "/var/lang/lib/python3.9/json/__init__.py", line 234, in dumps
return cls(
File "/var/lang/lib/python3.9/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/var/lang/lib/python3.9/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/opt/python/aws_lambda_powertools/shared/json_encoder.py", line 16, in default
return super().default(obj)
File "/var/lang/lib/python3.9/json/encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '