Module aws_lambda_powertools.utilities.parser.parser
Functions
def event_parser(handler: Callable[..., EventParserReturnType], event: dict[str, Any], context: LambdaContext, model: type[T] | None = None, envelope: type[Envelope] | None = None, **kwargs: Any)
-
Lambda handler decorator to parse & validate events using Pydantic models
It requires a model that implements Pydantic BaseModel to parse & validate the event.
When an envelope is given, it'll use the following logic:
- Parse the event against the envelope model first e.g. EnvelopeModel(**event)
- Envelope will extract a given key to be parsed against the model e.g. event.detail
This is useful when you need to confirm event wrapper structure, and b) selectively extract a portion of your payload for parsing & validation.
NOTE: If envelope is omitted, the complete event is parsed to match the model parameter definition.
Example
Lambda handler decorator to parse & validate event
class Order(BaseModel): id: int description: str ... @event_parser(model=Order) def handler(event: Order, context: LambdaContext): ...
Lambda handler decorator to parse & validate event - using built-in envelope
class Order(BaseModel): id: int description: str ... @event_parser(model=Order, envelope=envelopes.EVENTBRIDGE) def handler(event: Order, context: LambdaContext): ...
Parameters
handler
:Callable
- Method to annotate on
event
:dict
- Lambda event to be parsed & validated
context
:LambdaContext
- Lambda context object
model
:type[T] | None
- Your data model that will replace the event.
envelope
:Envelope
- Optional envelope to extract the model from
Raises
ValidationError
- When input event does not conform with model provided
InvalidModelTypeError
- When model given does not implement BaseModel or is not provided
InvalidEnvelopeError
- When envelope given does not implement BaseEnvelope
def parse(event: dict[str, Any], model: type[T], envelope: type[Envelope] | None = None)
-
Standalone function to parse & validate events using Pydantic models
Typically used when you need fine-grained control over error handling compared to event_parser decorator.
Example
Lambda handler decorator to parse & validate event
from aws_lambda_powertools.utilities.parser import ValidationError class Order(BaseModel): id: int description: str ... def handler(event: Order, context: LambdaContext): try: parse(model=Order) except ValidationError: ...
Lambda handler decorator to parse & validate event - using built-in envelope
class Order(BaseModel): id: int description: str ... def handler(event: Order, context: LambdaContext): try: parse(model=Order, envelope=envelopes.EVENTBRIDGE) except ValidationError: ...
Parameters
event
:dict
- Lambda event to be parsed & validated
model
:Model
- Your data model that will replace the event
envelope
:Envelope
- Optional envelope to extract the model from
Raises
ValidationError
- When input event does not conform with model provided
InvalidModelTypeError
- When model given does not implement BaseModel
InvalidEnvelopeError
- When envelope given does not implement BaseEnvelope