Questions regarding the API Gateway Event Handler #879
-
Hey All, Been trying to dive in and use these cool tools but am struggling to understand how they fit. At the moment we have three Lambda's that support 3 different functions (GET, PUT, PUT('s)). I'm unsure whether we should be using the API Gateway Event Handler vs using the Event Parser/Event Source Data Classes. We want to keep this micro, and are not wanting to really use the monolithic approach. Based on this, I feel like the API Gateway Event Handler is more for that monolithic approach, but maybe I have misunderstood it's intent. Ultimately we are just trying to achieve a way of getting path params/query params/payload throwing 400 errors if the incoming request isn't valid. I wonder if we should just be looking at using the incoming_request validation process? Handling exceptions also seems to be the hardest part biggest hurdle with the pydantic implementation in that we want certain exceptions to be translated to varying http status codes and translate ValidationErrors from pydantic to something that that is more inline with our API standards where we expect the message to be something like "field X is required" or field Y is empty" etc. Looking for others who may have had issues here, or potentially more documentation that might help indicate whether we should use the event handler vs the non-core utilities. And if there are any real-world examples in there that would be bonus++. Appreciate any help or assistance provided. This is a tool that looks super promising and cannot wait to use it on more projects. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
There are some improvements I want to make to this. To make handling errors (like form validation type of errors) easier to manage and customerize. And for validation to be built in. |
Beta Was this translation helpful? Give feedback.
-
👋 Hey there. I had a chance to talk with @michaelbrewer during re:Invent and walked through how we were using Powertools. We ran into challenges with how we wanted to handle API Gateway event sources with input/output schema validation. What we ended up doing was using the Here's the interface: from aws_lambda_powertools.utilities.data_classes import APIGatewayProxyEventV2
from api_common import api_middleware, ApiResponse
@api_middleware(
event_data_class=APIGatewayProxyEventV2,
input_validator=Draft7Validator("ConfigurationCreate.json"), # validator class psuedo-code
output_validator=Draft7Validator("Created.json"), # validator class psuedo-code
)
def lambda_handler(event, context) -> ApiResponse:
# Do stuff
return ApiResponse(201, {"message": "Success"}) Setting We're using
The error response we standardize on looks like this: {
"id": "20554228-97b1-46ce-9236-a70dc58f8b6e",
"code": "ValidationError",
"description": "The request body failed validation",
"details": {
"validationErrors": [
{
"path": "$",
"description": "'name' is a required property"
}
]
}
} Here We have standardized |
Beta Was this translation helpful? Give feedback.
👋 Hey there.
I had a chance to talk with @michaelbrewer during re:Invent and walked through how we were using Powertools. We ran into challenges with how we wanted to handle API Gateway event sources with input/output schema validation.
What we ended up doing was using the
aws_lambda_powertools.middleware_factory.lambda_handler_decorator
to create a middleware that merged together the ideas of the API Gateway event handler and the JSON schema input/output validator.Here's the interface: