|
| 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 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 | + |
| 30 | + |
| 31 | + |
| 32 | +## Supported event sources |
| 33 | + |
| 34 | +Event Source | Data_class |
| 35 | +------------------------------------------------- | --------------------------------------------------------------------------------- |
| 36 | +[API Gateway Proxy](#api-gateway-proxy) | `APIGatewayProxyEvent` |
| 37 | +[API Gateway Proxy event v2](#api-gateway-proxy-v2) | `APIGatewayProxyEventV2` |
| 38 | +[CloudWatch Logs](#cloudWatch-logs) | `CloudWatchLogsEvent` |
| 39 | +[Cognito User Pool](#cognito-user-pool-triggers) | Multiple available under `cognito_user_pool_event` |
| 40 | +[DynamoDB streams](#dynamoDB-streams) | `DynamoDBStreamEvent`, `DynamoDBRecordEventName` |
| 41 | +[EventBridge](#eventbridge) | `EventBridgeEvent` |
| 42 | +[Kinesis Data Stream](#kinesis-streams) | `KinesisStreamEvent` |
| 43 | +[S3](#S3) | `S3Event` |
| 44 | +[SES](#SES) | `SESEvent` |
| 45 | +[SNS](#SNS) | `SNSEvent` |
| 46 | +[SQS](#SQS) | `SQSEvent` |
| 47 | + |
| 48 | + |
| 49 | +<Note type="info"> |
| 50 | + The examples provided below are far from exhaustive - the data classes themselves are designed to provide a form of |
| 51 | + documentation inherently (via autocompletion, types and docstrings). |
| 52 | +</Note> |
| 53 | + |
| 54 | + |
| 55 | +## API Gateway Proxy |
| 56 | + |
| 57 | +Typically used for API Gateway REST API or HTTP API using v1 proxy event. |
| 58 | + |
| 59 | +```python:title=lambda_app.py |
| 60 | +from aws_lambda_powertools.utilities.data_classes import APIGatewayProxyEvent |
| 61 | + |
| 62 | +def lambda_handler(event, context): |
| 63 | + event: APIGatewayProxyEvent = APIGatewayProxyEvent(event) |
| 64 | + request_context = event.request_context |
| 65 | + identity = request_context.identity |
| 66 | + |
| 67 | + if 'helloworld' in event.path && event.http_method == 'GET': |
| 68 | + user = identity.user |
| 69 | + do_something_with(event.body, user) |
| 70 | +``` |
| 71 | + |
| 72 | +## API Gateway Proxy v2 |
| 73 | + |
| 74 | +```python:title=lambda_app.py |
| 75 | +from aws_lambda_powertools.utilities.data_classes import APIGatewayProxyEventV2 |
| 76 | + |
| 77 | +def lambda_handler(event, context): |
| 78 | + event: APIGatewayProxyEventV2 = APIGatewayProxyEventV2(event) |
| 79 | + request_context = event.request_context |
| 80 | + query_string_parameters = event.query_string_parameters |
| 81 | + |
| 82 | + if 'helloworld' in event.raw_path && request_context.http.method == 'POST': |
| 83 | + do_something_with(event.body, query_string_parameters) |
| 84 | +``` |
| 85 | + |
| 86 | +## CloudWatch Logs |
| 87 | + |
| 88 | +CloudWatch Logs events by default are compressed and base64 encoded. You can use the helper function provided to decode, |
| 89 | +decompress and parse json data from the event. |
| 90 | + |
| 91 | +```python:title=lambda_app.py |
| 92 | +from aws_lambda_powertools.utilities.data_classes import CloudWatchLogsEvent |
| 93 | + |
| 94 | +def lambda_handler(event, context): |
| 95 | + event: CloudWatchLogsEvent = CloudWatchLogsEvent(event) |
| 96 | + |
| 97 | + decompressed_log = event.parse_logs_data |
| 98 | + log_events = decompressed_log.log_events |
| 99 | + for event in log_events: |
| 100 | + do_something_with(event.timestamp, event.message) |
| 101 | +``` |
| 102 | + |
| 103 | +## Cognito User Pool |
| 104 | + |
| 105 | +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 |
| 106 | +can be imported from `aws_lambda_powertools.data_classes.cognito_user_pool_event`: |
| 107 | + |
| 108 | +Trigger/Event Source | Data Class |
| 109 | +------------------------------------------------- | ------------------------------------------------- |
| 110 | +Custom message event | `data_classes.cognito_user_pool_event.CustomMessageTriggerEvent` |
| 111 | +Post authentication | `data_classes.cognito_user_pool_event.PostAuthenticationTriggerEvent` |
| 112 | +Post confirmation | `data_classes.cognito_user_pool_event.PostConfirmationTriggerEvent` |
| 113 | +Pre authentication | `data_classes.cognito_user_pool_event.PreAuthenticationTriggerEvent` |
| 114 | +Pre sign-up | `data_classes.cognito_user_pool_event.PreSignUpTriggerEvent` |
| 115 | +Pre token generation | `data_classes.cognito_user_pool_event.PreTokenGenerationTriggerEvent` |
| 116 | +User migration | `data_classes.cognito_user_pool_event.UserMigrationTriggerEvent` |
| 117 | + |
| 118 | +```python:title=lambda_app.py |
| 119 | +from aws_lambda_powertools.utilities.cognito_user_pool_event import PostConfirmationTriggerEvent |
| 120 | + |
| 121 | +def lambda_handler(event, context): |
| 122 | + event: PostConfirmationTriggerEvent = PostConfirmationTriggerEvent(event) |
| 123 | + |
| 124 | + user_attributes = user_attributes = event.request.user_attributes |
| 125 | + do_something_with(user_attributes) |
| 126 | +``` |
| 127 | + |
| 128 | +## DynamoDB Streams |
| 129 | + |
| 130 | +The DynamoDB data class utility provides the base class for `DynamoDBStreamEvent`, a typed class for |
| 131 | +attributes values (`AttributeValue`), as well as enums for stream view type (`StreamViewType`) and event type |
| 132 | +(`DynamoDBRecordEventName`). |
| 133 | + |
| 134 | +```python:title=lambda_app.py |
| 135 | +from aws_lambda_powertools.utilities.data_classes import DynamoDBStreamEvent, DynamoDBRecordEventName |
| 136 | + |
| 137 | +def lambda_handler(event, context): |
| 138 | + event: DynamoDBStreamEvent = DynamoDBStreamEvent(event) |
| 139 | + |
| 140 | + # Multiple records can be delivered in a single event |
| 141 | + for record in event.records: |
| 142 | + if record.event_name == DynamoDBRecordEventName.MODIFY: |
| 143 | + do_something_with(record.dynamodb.new_image) |
| 144 | + do_something_with(record.dynamodb.old_image) |
| 145 | +``` |
| 146 | + |
| 147 | +## EventBridge |
| 148 | + |
| 149 | +```python:title=lambda_app.py |
| 150 | +from aws_lambda_powertools.utilities.data_classes import EventBridgeEvent |
| 151 | + |
| 152 | +def lambda_handler(event, context): |
| 153 | + event: EventBridgeEvent = EventBridgeEvent(event) |
| 154 | + do_something_with(event.detail) |
| 155 | + |
| 156 | +``` |
| 157 | + |
| 158 | +## Kinesis streams |
| 159 | + |
| 160 | +Kinesis events by default contain base64 encoded data. You can use the helper function to access the data either as json |
| 161 | +or plain text, depending on the original payload. |
| 162 | + |
| 163 | +```python:title=lambda_app.py |
| 164 | +from aws_lambda_powertools.utilities.data_classes import KinesisStreamEvent |
| 165 | + |
| 166 | +def lambda_handler(event, context): |
| 167 | + event: KinesisStreamEvent = KinesisStreamEvent(event) |
| 168 | + |
| 169 | + # if data was delivered as json |
| 170 | + data = event.data_as_text() |
| 171 | + |
| 172 | + # if data was delivered as text |
| 173 | + data = event.data_as_json() |
| 174 | + |
| 175 | + do_something_with(data) |
| 176 | + |
| 177 | +``` |
| 178 | + |
| 179 | +## S3 |
| 180 | + |
| 181 | +```python:title=lambda_app.py |
| 182 | +from aws_lambda_powertools.utilities.data_classes import S3Event |
| 183 | + |
| 184 | +def lambda_handler(event, context): |
| 185 | + event: S3Event = S3Event(event) |
| 186 | + bucket_name = event.bucket_name |
| 187 | + |
| 188 | + # Multiple records can be delivered in a single event |
| 189 | + for record in event.records: |
| 190 | + object_key = record.s3.get_object.key |
| 191 | + |
| 192 | + do_something_with(f'{bucket_name}/{object_key}') |
| 193 | + |
| 194 | +``` |
| 195 | + |
| 196 | +## SES |
| 197 | + |
| 198 | +```python:title=lambda_app.py |
| 199 | +from aws_lambda_powertools.utilities.data_classes import SESEvent |
| 200 | + |
| 201 | +def lambda_handler(event, context): |
| 202 | + event: SESEvent = SESEvent(event) |
| 203 | + |
| 204 | + # Multiple records can be delivered in a single event |
| 205 | + for record in event.records: |
| 206 | + mail = record.ses.mail |
| 207 | + common_headers = list(mail.common_headers) |
| 208 | + |
| 209 | + do_something_with(common_headers.to, common_headers.subject) |
| 210 | + |
| 211 | +``` |
| 212 | + |
| 213 | +## SNS |
| 214 | + |
| 215 | +```python:title=lambda_app.py |
| 216 | +from aws_lambda_powertools.utilities.data_classes import SNSEvent |
| 217 | + |
| 218 | +def lambda_handler(event, context): |
| 219 | + event: SNSEvent = SNSEvent(event) |
| 220 | + |
| 221 | + # Multiple records can be delivered in a single event |
| 222 | + for record in event.records: |
| 223 | + message = record.sns.message |
| 224 | + subject = record.sns.subject |
| 225 | + |
| 226 | + do_something_with(subject, message) |
| 227 | +``` |
| 228 | + |
| 229 | +## SQS |
| 230 | + |
| 231 | +```python:title=lambda_app.py |
| 232 | +from aws_lambda_powertools.utilities.data_classes import SQSEvent |
| 233 | + |
| 234 | +def lambda_handler(event, context): |
| 235 | + event: SQSEvent = SQSEvent(event) |
| 236 | + |
| 237 | + # Multiple records can be delivered in a single event |
| 238 | + for record in event.records: |
| 239 | + do_something_with(record.body) |
| 240 | +``` |
0 commit comments