Skip to content

Commit f309012

Browse files
feat(parser): add model for the DynamoDB Stream Lambda invocation record (#7818)
This commit adds a model for the invocation record that a Lambda with a DynamoDB event source sends to a SNS, SQS or S3 destination for failed invocations. The record format is described in https://docs.aws.amazon.com/lambda/latest/dg/services-dynamodb-errors.html The model can be used in the handler of a lambda that processes failed invocations from SNS or SQS by combining it with the source envelope model, e.g., from aws_lambda_powertools.utilities.parser import envelopes, event_parser from aws_lambda_powertools.utilities.parser.models import DynamoDBStreamLambdaOnFailureDestinationModel @event_parser(model=DynamoDBStreamLambdaOnFailureDestinationModel, envelope=envelopes.SqsEnvelope) def lambda_handler(event: list[DynamoDBStreamLambdaOnFailureDestinationModel], context: LambdaContext): ... Co-authored-by: Leandro Damascena <[email protected]>
1 parent 16748c9 commit f309012

File tree

5 files changed

+205
-58
lines changed

5 files changed

+205
-58
lines changed

aws_lambda_powertools/utilities/parser/models/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
)
6767
from .dynamodb import (
6868
DynamoDBStreamChangedRecordModel,
69+
DynamoDBStreamLambdaOnFailureDestinationModel,
6970
DynamoDBStreamModel,
7071
DynamoDBStreamRecordModel,
7172
)
@@ -173,6 +174,7 @@
173174
"DynamoDBStreamModel",
174175
"EventBridgeModel",
175176
"DynamoDBStreamChangedRecordModel",
177+
"DynamoDBStreamLambdaOnFailureDestinationModel",
176178
"DynamoDBStreamRecordModel",
177179
"DynamoDBStreamChangedRecordModel",
178180
"KinesisDataStreamModel",

aws_lambda_powertools/utilities/parser/models/dynamodb.py

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
from datetime import datetime
33
from typing import Any, Dict, List, Literal, Optional, Type, Union
44

5-
from pydantic import BaseModel, Field, field_validator
5+
from pydantic import BaseModel, ConfigDict, Field, field_validator
6+
from pydantic.alias_generators import to_camel
67

78
from aws_lambda_powertools.shared.dynamodb_deserializer import TypeDeserializer
89

@@ -110,3 +111,91 @@ class DynamoDBStreamModel(BaseModel):
110111
},
111112
],
112113
)
114+
115+
116+
class DDBStreamBatchInfo(BaseModel):
117+
model_config = ConfigDict(alias_generator=to_camel)
118+
119+
approximate_arrival_of_first_record: datetime = Field(
120+
description="The approximate date and time when the first stream record from the batch was created"
121+
", in ISO-8601 format.",
122+
examples=["1970-01-01T00:00:00.000Z"],
123+
)
124+
approximate_arrival_of_last_record: datetime = Field(
125+
description="The approximate date and time when the last stream record from the batch was created"
126+
", in ISO-8601 format.",
127+
examples=["1970-01-01T00:00:00.000Z"],
128+
)
129+
batch_size: int = Field(
130+
description="The size of the batch.",
131+
examples=[1],
132+
)
133+
end_sequence_number: str = Field(
134+
description="The unique identifier of the last stream record from the batch.",
135+
examples=["222"],
136+
)
137+
shard_id: str = Field(
138+
description="The unique identifier of the DynamoDB Stream shard that contains the records from the batch.",
139+
examples=["shardId-00000000000000000000-00000000"],
140+
)
141+
start_sequence_number: str = Field(
142+
description="The unique identifier of the first stream record from the batch.",
143+
examples=["222"],
144+
)
145+
stream_arn: str = Field(
146+
description="The Amazon Resource Name (ARN) of the DynamoDB stream.",
147+
examples=["arn:aws:dynamodb:us-west-2:123456789012:table/ExampleTable/stream/2021-01-01T00:00:00.000"],
148+
)
149+
150+
151+
class RequestContext(BaseModel):
152+
model_config = ConfigDict(alias_generator=to_camel)
153+
154+
approximate_invoke_count: int = Field(
155+
description="The number of Lambda invocations for the record.",
156+
examples=[1],
157+
)
158+
condition: str = Field(
159+
description="The condition that caused the record to be discarded.",
160+
examples=["RetryAttemptsExhausted"],
161+
)
162+
function_arn: str = Field(
163+
description="The Amazon Resource Name (ARN) of the Lambda.",
164+
examples=["arn:aws:lambda:eu-west-1:809313241:function:test"],
165+
)
166+
request_id: str = Field(
167+
description="The unique identifier of the request.",
168+
)
169+
170+
171+
class ResponseContext(BaseModel):
172+
model_config = ConfigDict(alias_generator=to_camel)
173+
174+
executed_version: str = Field(
175+
description="The version of the Lambda executed",
176+
examples=["$LATEST"],
177+
)
178+
function_error: str = Field(
179+
description="",
180+
examples=["Unhandled"],
181+
)
182+
status_code: int = Field(
183+
description="The status code returned by the Lambda",
184+
)
185+
186+
187+
# https://docs.aws.amazon.com/lambda/latest/dg/services-dynamodb-errors.html
188+
class DynamoDBStreamLambdaOnFailureDestinationModel(BaseModel):
189+
model_config = ConfigDict(alias_generator=to_camel)
190+
191+
ddb_stream_batch_info: DDBStreamBatchInfo = Field(alias="DDBStreamBatchInfo")
192+
request_context: RequestContext
193+
response_context: ResponseContext
194+
timestamp: datetime = Field(
195+
description="The record time, in ISO-8601 format.",
196+
examples=["1970-01-01T00:00:00.000Z"],
197+
)
198+
version: str = Field(
199+
description="The version of the record format.",
200+
examples=["1.0"],
201+
)

0 commit comments

Comments
 (0)