Skip to content

Commit 5a6a74c

Browse files
author
Tom McCarthy
committed
docs: add docs for data classes utility
1 parent 52314e6 commit 5a6a74c

File tree

4 files changed

+214
-1
lines changed

4 files changed

+214
-1
lines changed

docs/content/index.mdx

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ sam init --location https://github.com/aws-samples/cookiecutter-aws-sam-python
3030
* [Bring your own middleware](./utilities/middleware_factory) - Decorator factory to create your own middleware to run logic before, and after each Lambda invocation
3131
* [Parameters utility](./utilities/parameters) - Retrieve parameter values from AWS Systems Manager Parameter Store, AWS Secrets Manager, or Amazon DynamoDB, and cache them for a specific amount of time
3232
* [Batch utility](./utilities/batch) - Batch processing for AWS SQS, handles partial failure.
33+
* [Event source data classes](./utilities/data_classes) - Batch processing for AWS SQS, handles partial failure.
3334

3435
### Lambda Layer
3536

@@ -73,6 +74,7 @@ Utility | Description
7374
[Bring your own middleware](.//utilities/middleware_factory) | Decorator factory to create your own middleware to run logic before, and after each Lambda invocation
7475
[Parameters utility](./utilities/parameters) | Retrieve parameter values from AWS Systems Manager Parameter Store, AWS Secrets Manager, or Amazon DynamoDB, and cache them for a specific amount of time
7576
[Typing utility](./utilities/typing) | Static typing classes to speedup development in your IDE
77+
[Event source data classes](./utilities/data_classes) - Batch processing for AWS SQS, handles partial failure.
7678

7779
## Environment variables
7880

311 KB
Loading
+210
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
---
2+
title: Event Source Data Classes
3+
description: Utility
4+
---
5+
6+
import Note from "../../src/components/Note"
7+
8+
The event source data classes utility provides classes describing the schema of common trigger Lambda events triggers.
9+
10+
**Key Features**
11+
12+
* Type hinting and code completion for common event types
13+
* Helper functions for decoding/deserializing nested fields
14+
* Docstrings for fields contained in event schemas
15+
16+
**Background**
17+
18+
When authoring Lambda functions, you often need to understand the schema of the event dictionary which is passed to the
19+
handler. There are several common event types which follow a specific schema, depending on the service triggering the
20+
Lambda function.
21+
22+
23+
## Utilizing the data classes
24+
25+
The classes are initialized by passing in the Lambda event object into the constructor of the appropriate data class.
26+
For example, if your Lambda function is being triggered by an API Gateway proxy integration, you can use the
27+
`APIGatewayProxyEvent` class.
28+
29+
![Utilities Data Classes](../media/utilities_data_classes.png)
30+
31+
32+
## Supported event sources
33+
<Note type="info">
34+
The examples provided below are far from exhaustive - the data classes themselves are designed to provide a form of
35+
documentation inherently (via autocompletion, types and docstrings).
36+
</Note>
37+
38+
39+
### API Gateway Proxy V1 (REST API)
40+
```python:title=lambda_app.py
41+
from aws_lambda_powertools.utilities.data_classes import APIGatewayProxyEvent
42+
43+
def lambda_handler(event, context):
44+
event = APIGatewayProxyEvent(event)
45+
request_context = event.request_context
46+
identity = request_context.identity
47+
48+
if 'helloworld' in event.path && event.http_method == 'GET':
49+
user = identity.user
50+
do_something_with(event.body, user)
51+
```
52+
53+
### API Gateway Proxy V2 (HTTP API)
54+
```python:title=lambda_app.py
55+
from aws_lambda_powertools.utilities.data_classes import APIGatewayProxyEventV2
56+
57+
def lambda_handler(event, context):
58+
event = APIGatewayProxyEventV2(event)
59+
request_context = event.request_context
60+
query_string_parameters = event.query_string_parameters
61+
62+
if 'helloworld' in event.raw_path && request_context.http.method == 'POST':
63+
do_something_with(event.body, query_string_parameters)
64+
```
65+
66+
### CloudWatch logs
67+
CloudWatch logs events by default are compressed and base64 encoded. You can use the helper function provided to decode,
68+
decompress and parse json data from the event.
69+
70+
```python:title=lambda_app.py
71+
from aws_lambda_powertools.utilities.data_classes import CloudWatchLogsEvent
72+
73+
def lambda_handler(event, context):
74+
event = CloudWatchLogsEvent(event)
75+
76+
decompressed_log = event.parse_logs_data
77+
log_events = decompressed_log.log_events
78+
for event in log_events:
79+
do_something_with(event.timestamp, event.message)
80+
```
81+
82+
### Cognito user pool triggers
83+
Cognito User Pools have several [different Lambda trigger sources](https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-identity-pools-working-with-aws-lambda-triggers.html#cognito-user-identity-pools-working-with-aws-lambda-trigger-sources), all of which map to a different data class, which
84+
can be imported from `aws_lambda_powertools.data_classes.cognito_user_pool_event`:
85+
86+
Trigger/Event Source | Data Class
87+
------------------------------------------------- | -------------------------------------------------
88+
Custom message event | `data_classes.cognito_user_pool_event.CustomMessageTriggerEvent`
89+
Post authentication | `data_classes.cognito_user_pool_event.PostAuthenticationTriggerEvent`
90+
Post confirmation | `data_classes.cognito_user_pool_event.PostConfirmationTriggerEvent`
91+
Pre authentication | `data_classes.cognito_user_pool_event.PreAuthenticationTriggerEvent`
92+
Pre sign-up | `data_classes.cognito_user_pool_event.PreSignUpTriggerEvent`
93+
Pre token generation | `data_classes.cognito_user_pool_event.PreTokenGenerationTriggerEvent`
94+
User migration | `data_classes.cognito_user_pool_event.UserMigrationTriggerEvent`
95+
96+
```python:title=lambda_app.py
97+
from aws_lambda_powertools.utilities.cognito_user_pool_event import PostConfirmationTriggerEvent
98+
99+
def lambda_handler(event, context):
100+
event = PostConfirmationTriggerEvent(event)
101+
102+
user_attributes = user_attributes = event.request.user_attributes
103+
do_something_with(user_attributes)
104+
```
105+
106+
### DynamoDB streams
107+
The DynamoDB data class utility provides the base class for `DynamoDBStreamEvent`, a typed class for
108+
attributes values (`AttributeValue`), as well as enums for stream view type (`StreamViewType`) and event type
109+
(`DynamoDBRecordEventName`).
110+
111+
```python:title=lambda_app.py
112+
from aws_lambda_powertools.utilities.data_classes import DynamoDBStreamEvent, StreamViewType
113+
114+
def lambda_handler(event, context):
115+
event = DynamoDBStreamEvent(event)
116+
117+
# Multiple records can be delivered in a single event
118+
for record in event.records:
119+
if record.event_name == DynamoDBRecordEventName.MODIFY:
120+
do_something_with(record.dynamodb.new_image)
121+
do_something_with(record.dynamodb.old_image)
122+
```
123+
124+
### EventBridge
125+
```python:title=lambda_app.py
126+
from aws_lambda_powertools.utilities.data_classes import EventBridgeEvent
127+
128+
def lambda_handler(event, context):
129+
event = EventBridgeEvent(event)
130+
do_something_with(event.detail)
131+
132+
```
133+
134+
### Kinesis streams
135+
Kinesis events by default contain base64 encoded data. You can use the helper function to access the data either as json
136+
or plain text, depending on the original payload.
137+
```python:title=lambda_app.py
138+
from aws_lambda_powertools.utilities.data_classes import KinesisStreamEvent
139+
140+
def lambda_handler(event, context):
141+
event = KinesisStreamEvent(event)
142+
143+
# if data was delivered as json
144+
data = event.data_as_text()
145+
146+
# if data was delivered as text
147+
data = event.data_as_json()
148+
149+
do_something_with(data)
150+
151+
```
152+
153+
### S3 events
154+
```python:title=lambda_app.py
155+
from aws_lambda_powertools.utilities.data_classes import S3Event
156+
157+
def lambda_handler(event, context):
158+
event = S3Event(event)
159+
bucket_name = event.bucket_name
160+
161+
# Multiple records can be delivered in a single event
162+
for record in event.records:
163+
object_key = record.s3.get_object.key
164+
165+
do_something_with(f'{bucket_name}/{object_key}')
166+
167+
```
168+
169+
### SES events
170+
```python:title=lambda_app.py
171+
from aws_lambda_powertools.utilities.data_classes import SESEvent
172+
173+
def lambda_handler(event, context):
174+
event = SESEvent(event)
175+
176+
# Multiple records can be delivered in a single event
177+
for record in event.records:
178+
mail = record.ses.mail
179+
common_headers = list(mail.common_headers)
180+
181+
do_something_with(common_headers.to, common_headers.subject)
182+
183+
```
184+
185+
### SNS
186+
```python:title=lambda_app.py
187+
from aws_lambda_powertools.utilities.data_classes import SNSEvent
188+
189+
def lambda_handler(event, context):
190+
event = SNSEvent(event)
191+
192+
# Multiple records can be delivered in a single event
193+
for record in event.records:
194+
message = record.sns.message
195+
subject = record.sns.subject
196+
197+
do_something_with(subject, message)
198+
```
199+
200+
### SQS
201+
```python:title=lambda_app.py
202+
from aws_lambda_powertools.utilities.data_classes import SQSEvent
203+
204+
def lambda_handler(event, context):
205+
event = SQSEvent(event)
206+
207+
# Multiple records can be delivered in a single event
208+
for record in event.records:
209+
do_something_with(record.body)
210+
```

docs/gatsby-config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ module.exports = {
3434
'utilities/parameters',
3535
'utilities/batch',
3636
'utilities/typing',
37-
'utilities/validation'
37+
'utilities/validation',
38+
'utilities/data_classes'
3839
],
3940
},
4041
navConfig: {

0 commit comments

Comments
 (0)