Bug: event_source decorator with APIGatewayProxyEventV2 causes TypeError in debug mode
See original GitHub issueExpected 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__} '
Issue Analytics
- State:
- Created a year ago
- Comments:8 (7 by maintainers)
Top Results From Across the Web
REST API - Lambda Powertools Python - Open Source at AWS
You can use named decorators to specify the HTTP method that should be handled in your ... You can enable debug mode via...
Read more >Wraps gives TypeError when used in a decorator
Got it. Sorry the issue was I used wraps incorrectly as a decorator. Here is the correct code def debug(func): msg = func....
Read more >Troubleshooting and tips — Numba 0.50.1 documentation
Another common reason for Numba not being able to compile your code is that it cannot ... Setting the debug keyword argument in...
Read more >aws-lambda-powertools: Versions - Openbase
This address a common use case of centralizing CloudWatch Logs from multiple regions or accounts using Kinesis Data Streams. You can now easily...
Read more >TypeError: 'NoneType' object is not callable : PY-52137
Hello, I've found a bug that only comes up when run in the pycharm debugger, not when run normally in pycharm, and not...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@heitorlessa @sthuber90 - i am sorry for missing this edge case, but it is pretty valid if your handler wants to use the data class. So i put up a PR which is pretty small.
Otherwise a current workaround would be:
One thing i notices is that PyCharm does infer that event should be a dict.
Thank you. I actually think I found a use case:
Of course there are other ways to solve this, instead of using the event source handler 😏