You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/utilities/parser.md
+21-21
Original file line number
Diff line number
Diff line change
@@ -13,7 +13,7 @@ The Parser utility simplifies data parsing and validation using [Pydantic](https
13
13
- Parse and validate Lambda event payloads
14
14
- Built-in support for common AWS event sources
15
15
- Runtime type checking with user-friendly error messages
16
-
- Compatible with Pydantic v2
16
+
- Compatible with Pydantic v2.x
17
17
18
18
## Getting started
19
19
@@ -22,14 +22,14 @@ The Parser utility simplifies data parsing and validation using [Pydantic](https
22
22
Powertools only supports Pydantic v2, so make sure to install the required dependencies for Pydantic v2 before using the Parser.
23
23
24
24
```python
25
-
pip install aws-lambda-powertools[tracer]
25
+
pip install aws-lambda-powertools[parser]
26
26
```
27
27
28
28
!!! info "This is not necessary if you're installing Powertools for AWS Lambda (Python) via [Lambda Layer/SAR](../index.md#lambda-layer){target="_blank"}"
29
29
30
-
You can also add as a dependency in your preferred tool: `e.g., requirements.txt, pyproject.toml`.
30
+
You can also add as a dependency in your preferred tool: `e.g., requirements.txt, pyproject.toml`, etc.
31
31
32
-
### Data Model with Parse
32
+
### Data Model with Parser
33
33
34
34
You can define models by inheriting from `BaseModel` or any other supported type through `TypeAdapter` to parse incoming events. Pydantic then validates the data, ensuring that all fields conform to the specified types and maintaining data integrity.
35
35
@@ -38,7 +38,7 @@ You can define models by inheriting from `BaseModel` or any other supported type
38
38
39
39
#### Event parser
40
40
41
-
The `@event_parser` decorator automatically parses the incoming event into the specified Pydantic model `MyEvent`. If the input doesn't match the model's structure or type requirements, we raises a `ValidationError` directly from Pydantic.
41
+
The `@event_parser` decorator automatically parses the incoming event into the specified Pydantic model `MyEvent`. If the input doesn't match the model's structure or type requirements, it raises a `ValidationError` directly from Pydantic.
42
42
43
43
=== "getting_started_with_parser.py"
44
44
@@ -68,7 +68,7 @@ You can use the `parse()` function when you need to have flexibility with differ
#### Keys difference between parse and event_parser
71
+
#### Keys differences between parse and event_parser
72
72
73
73
The `parse()` function offers more flexibility and control:
74
74
@@ -84,7 +84,7 @@ The `@event_parser` decorator is ideal for:
84
84
85
85
### Built-in models
86
86
87
-
You can use pre-built models provided by the Parser for parsing events from AWS services, so you don’t need to create these models yourself, we’ve already done that for you.
87
+
You can use pre-built models to work events from AWS services, so you don’t need to create them yourself. We’ve already done that for you!
88
88
89
89
=== "sqs_model_event.py"
90
90
@@ -158,7 +158,7 @@ Use the model to validate and extract relevant information from the incoming eve
158
158
159
159
### Envelopes
160
160
161
-
You can use **Envelopes**, which are **JMESPath expressions**, to extract specific portions of complex, nested JSON structures. This is useful when your actual payload is wrapped around a known structure, for example Lambda Event Sources like **EventBridge**.
161
+
You can use **Envelopes** to extract specific portions of complex, nested JSON structures. This is useful when your actual payload is wrapped around a known structure, for example Lambda Event Sources like **EventBridge**.
162
162
163
163
Envelopes can be used via `envelope` parameter available in both `parse` function and `event_parser` decorator.
164
164
@@ -181,23 +181,23 @@ You can use pre-built envelopes provided by the Parser to extract and parse spec
|**DynamoDBStreamEnvelope**| 1. Parses data using `DynamoDBStreamModel`. `` 2. Parses records in `NewImage` and `OldImage` keys using your model. `` 3. Returns a list with a dictionary containing `NewImage` and `OldImage` keys |`List[Dict[str, Optional[Model]]]`|
184
-
|**EventBridgeEnvelope**| 1. Parses data using `EventBridgeModel`. ``2. Parses`detail` key using your model and returns it. |`Model`|
185
-
|**SqsEnvelope**| 1. Parses data using `SqsModel`. ``2. Parses records in`body` key using your model and return them in a list. |`List[Model]`|
186
-
|**CloudWatchLogsEnvelope**| 1. Parses data using `CloudwatchLogsModel` which will base64 decode and decompress it. ``2. Parses records in`message` key using your model and return them in a list. |`List[Model]`|
187
-
|**KinesisDataStreamEnvelope**| 1. Parses data using `KinesisDataStreamModel` which will base64 decode it. ``2. Parses records in in`Records` key using your model and returns them in a list. |`List[Model]`|
188
-
|**KinesisFirehoseEnvelope**| 1. Parses data using `KinesisFirehoseModel` which will base64 decode it. ``2. Parses records in in`Records` key using your model and returns them in a list. |`List[Model]`|
189
-
|**SnsEnvelope**| 1. Parses data using `SnsModel`. ``2. Parses records in`body` key using your model and return them in a list. |`List[Model]`|
184
+
|**EventBridgeEnvelope**| 1. Parses data using `EventBridgeModel`. ``2. Parses`detail` key using your model`` and returns it. |`Model`|
185
+
|**SqsEnvelope**| 1. Parses data using `SqsModel`. ``2. Parses records in`body` key using your model`` and return them in a list. |`List[Model]`|
186
+
|**CloudWatchLogsEnvelope**| 1. Parses data using `CloudwatchLogsModel` which will base64 decode and decompress it. ``2. Parses records in`message` key using your model`` and return them in a list. |`List[Model]`|
187
+
|**KinesisDataStreamEnvelope**| 1. Parses data using `KinesisDataStreamModel` which will base64 decode it. ``2. Parses records in in`Records` key using your model`` and returns them in a list. |`List[Model]`|
188
+
|**KinesisFirehoseEnvelope**| 1. Parses data using `KinesisFirehoseModel` which will base64 decode it. ``2. Parses records in in`Records` key using your model`` and returns them in a list. |`List[Model]`|
189
+
|**SnsEnvelope**| 1. Parses data using `SnsModel`. ``2. Parses records in`body` key using your model`` and return them in a list. |`List[Model]`|
190
190
|**SnsSqsEnvelope**| 1. Parses data using `SqsModel`. `` 2. Parses SNS records in `body` key using `SnsNotificationModel`. `` 3. Parses data in `Message` key using your model and return them in a list. |`List[Model]`|
191
-
|**ApiGatewayEnvelope**| 1. Parses data using `APIGatewayProxyEventModel`. ``2. Parses`body` key using your model and returns it. |`Model`|
192
-
|**ApiGatewayV2Envelope**| 1. Parses data using `APIGatewayProxyEventV2Model`. ``2. Parses`body` key using your model and returns it. |`Model`|
193
-
|**LambdaFunctionUrlEnvelope**| 1. Parses data using `LambdaFunctionUrlModel`. ``2. Parses`body` key using your model and returns it. |`Model`|
194
-
|**KafkaEnvelope**| 1. Parses data using `KafkaRecordModel`. ``2. Parses`value` key using your model and returns it. |`Model`|
195
-
|**VpcLatticeEnvelope**| 1. Parses data using `VpcLatticeModel`. ``2. Parses`value` key using your model and returns it. |`Model`|
196
-
|**BedrockAgentEnvelope**| 1. Parses data using `BedrockAgentEventModel`. ``2. Parses`inputText` key using your model and returns it. |`Model`|
191
+
|**ApiGatewayEnvelope**| 1. Parses data using `APIGatewayProxyEventModel`. ``2. Parses`body` key using your model`` and returns it. |`Model`|
192
+
|**ApiGatewayV2Envelope**| 1. Parses data using `APIGatewayProxyEventV2Model`. ``2. Parses`body` key using your model`` and returns it. |`Model`|
193
+
|**LambdaFunctionUrlEnvelope**| 1. Parses data using `LambdaFunctionUrlModel`. ``2. Parses`body` key using your model`` and returns it. |`Model`|
194
+
|**KafkaEnvelope**| 1. Parses data using `KafkaRecordModel`. ``2. Parses`value` key using your model`` and returns it. |`Model`|
195
+
|**VpcLatticeEnvelope**| 1. Parses data using `VpcLatticeModel`. ``2. Parses`value` key using your model`` and returns it. |`Model`|
196
+
|**BedrockAgentEnvelope**| 1. Parses data using `BedrockAgentEventModel`. ``2. Parses`inputText` key using your model`` and returns it. |`Model`|
197
197
198
198
#### Bringing your own envelope
199
199
200
-
You can create your own Envelope model and logic by inheriting from `BaseEnvelope`, and implementing the `parse` method or `@event_parser` decorator.
200
+
You can create your own Envelope model and logic by inheriting from `BaseEnvelope`, and implementing the `parse` method.
201
201
202
202
Here's a snippet of how the EventBridge envelope we demonstrated previously is implemented.
0 commit comments