From 5177e4dce98d9910b734ee1696ba2a307748252a Mon Sep 17 00:00:00 2001 From: Ajey Awasthi <115537579+dropps07@users.noreply.github.com> Date: Sun, 15 Nov 2026 19:37:49 +0530 Subject: [PATCH 1/2] fix(parser): keep lifecycle and replication event data in S3RecordModel --- .../utilities/parser/models/s3.py | 77 +++++++++++++++++++ tests/events/s3EventReplication.json | 46 +++++++++++ tests/unit/parser/_pydantic/test_s3.py | 31 ++++++++ 3 files changed, 154 insertions(+) create mode 100644 tests/events/s3EventReplication.json diff --git a/aws_lambda_powertools/utilities/parser/models/s3.py b/aws_lambda_powertools/utilities/parser/models/s3.py index 080151c4d8a..053fc8f15b6 100644 --- a/aws_lambda_powertools/utilities/parser/models/s3.py +++ b/aws_lambda_powertools/utilities/parser/models/s3.py @@ -50,6 +50,59 @@ class S3EventRecordIntelligentTieringEventData(BaseModel): ) +class S3EventRecordLifecycleTransitionEventData(BaseModel): + destinationStorageClass: str = Field( + description="The storage class the object transitioned to. For Lifecycle Transition events.", + examples=[ + "INTELLIGENT_TIERING", + "STANDARD_IA", + "GLACIER", + ], + ) + + +class S3EventRecordLifecycleEventData(BaseModel): + transitionEventData: S3EventRecordLifecycleTransitionEventData = Field( + description="Event data produced only for 's3:LifecycleTransition' events.", + examples=[ + { + "transitionEventData": { + "destinationStorageClass": "INTELLIGENT_TIERING", + }, + }, + ], + ) + + +class S3EventRecordReplicationEventData(BaseModel): + replicationRuleId: str | None = Field( + default=None, + description="The ID of the replication rule. For Replication events.", + examples=["replication-rule-1"], + ) + destinationBucket: str | None = Field( + default=None, + description="The destination bucket ARN for the replication. For Replication events.", + examples=["arn:aws:s3:::destination-bucket"], + ) + s3Operation: str | None = Field( + default=None, + description="The S3 operation that was replicated. For Replication events.", + examples=["ObjectCreated:Put"], + ) + requestTime: str | None = Field( + default=None, + description="The time, in ISO-8601 format, when the replication was requested. For Replication events.", + examples=["2019-09-03T19:37:27.192Z"], + ) + failureReason: str | None = Field( + default=None, + description="The reason the replication failed, present only for failed replication events. " + "For Replication events.", + examples=["AssumedRole is not authorized to perform the requested operation."], + ) + + class S3Identity(BaseModel): principalId: str = Field( description="Amazon identifier of the user, role, account or services who caused the event.", @@ -483,6 +536,30 @@ class S3RecordModel(BaseModel): }, ], ) + lifecycleEventData: S3EventRecordLifecycleEventData | None = Field( + default=None, + description="The Lifecycle event data object.", + examples=[ + { + "transitionEventData": { + "destinationStorageClass": "INTELLIGENT_TIERING", + }, + }, + ], + ) + replicationEventData: S3EventRecordReplicationEventData | None = Field( + default=None, + description="The Replication event data object.", + examples=[ + { + "replicationRuleId": "replication-rule-1", + "destinationBucket": "arn:aws:s3:::destination-bucket", + "s3Operation": "ObjectCreated:Put", + "requestTime": "2019-09-03T19:37:27.192Z", + "failureReason": "AssumedRole is not authorized to perform the requested operation.", + }, + ], + ) @model_validator(mode="before") def validate_s3_object(cls, values): diff --git a/tests/events/s3EventReplication.json b/tests/events/s3EventReplication.json new file mode 100644 index 00000000000..9b9b296fc44 --- /dev/null +++ b/tests/events/s3EventReplication.json @@ -0,0 +1,46 @@ +{ + "Records": [ + { + "awsRegion": "us-east-1", + "eventName": "Replication:OperationFailedReplication", + "eventSource": "aws:s3", + "eventTime": "2019-09-03T19:37:27.192Z", + "eventVersion": "2.1", + "replicationEventData": { + "destinationBucket": "arn:aws:s3:::destination-bucket", + "failureReason": "AssumedRole is not authorized to perform the requested operation.", + "replicationRuleId": "replication-rule-1", + "requestTime": "2019-09-03T19:37:27.192Z", + "s3Operation": "ObjectCreated:Put" + }, + "requestParameters": { + "sourceIPAddress": "s3.amazonaws.com" + }, + "responseElements": { + "x-amz-id-2": "vlR7PnpV2Ce81l0PRw6jlUpck7Jo5ZsQjryTjKlc5aLWGVHPZLj5NeC6qMa0emYBDXOo6QBU0Wo=", + "x-amz-request-id": "D82B88E5F771F645" + }, + "s3": { + "bucket": { + "arn": "arn:aws:s3:::lambda-artifacts-deafc19498e3f2df", + "name": "lambda-artifacts-deafc19498e3f2df", + "ownerIdentity": { + "principalId": "A3I5XTEXAMAI3E" + } + }, + "configurationId": "828aa6fc-f7b5-4305-8584-487c791949c1", + "object": { + "eTag": "abcdef1232423423", + "key": "/path/to/file.parquet", + "sequencer": "0C0F6F405D6ED209E1", + "size": 12345, + "versionId": "SomeThingThere" + }, + "s3SchemaVersion": "1.0" + }, + "userIdentity": { + "principalId": "s3.amazonaws.com" + } + } + ] +} diff --git a/tests/unit/parser/_pydantic/test_s3.py b/tests/unit/parser/_pydantic/test_s3.py index 7a9beb990c4..fd39b2fd93c 100644 --- a/tests/unit/parser/_pydantic/test_s3.py +++ b/tests/unit/parser/_pydantic/test_s3.py @@ -175,6 +175,11 @@ def test_s3_trigger_event_lifecycle_transition(): assert convert_time == 1567539447192 assert record.eventName == raw_record["eventName"] assert record.glacierEventData is None + assert record.lifecycleEventData is not None + assert ( + record.lifecycleEventData.transitionEventData.destinationStorageClass + == raw_record["lifecycleEventData"]["transitionEventData"]["destinationStorageClass"] + ) user_identity = record.userIdentity assert user_identity.principalId == raw_record["userIdentity"]["principalId"] @@ -198,3 +203,29 @@ def test_s3_trigger_event_lifecycle_transition(): assert bucket.name == raw_bucket["name"] assert bucket.ownerIdentity.principalId == raw_bucket["ownerIdentity"]["principalId"] assert bucket.arn == raw_bucket["arn"] + + +def test_s3_trigger_event_replication(): + raw_event = load_event("s3EventReplication.json") + parsed_event: S3Model = S3Model(**raw_event) + + records = list(parsed_event.Records) + assert len(records) == 1 + + record: S3RecordModel = records[0] + raw_record = raw_event["Records"][0] + assert record.eventVersion == raw_record["eventVersion"] + assert record.eventSource == raw_record["eventSource"] + assert record.awsRegion == raw_record["awsRegion"] + assert record.eventName == raw_record["eventName"] + assert record.glacierEventData is None + assert record.lifecycleEventData is None + + replication = record.replicationEventData + assert replication is not None + raw_replication = raw_record["replicationEventData"] + assert replication.replicationRuleId == raw_replication["replicationRuleId"] + assert replication.destinationBucket == raw_replication["destinationBucket"] + assert replication.s3Operation == raw_replication["s3Operation"] + assert replication.requestTime == raw_replication["requestTime"] + assert replication.failureReason == raw_replication["failureReason"] From 5058eaa583db3bf42fdbe4a65a31aace8b446be1 Mon Sep 17 00:00:00 2001 From: Ajey Awasthi <115537579+dropps07@users.noreply.github.com> Date: Tue, 17 Nov 2026 09:26:40 +0530 Subject: [PATCH 2/2] fix(parser): use real S3 replication codes in S3RecordModel examples and fixture --- aws_lambda_powertools/utilities/parser/models/s3.py | 8 ++++---- tests/events/s3EventReplication.json | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/aws_lambda_powertools/utilities/parser/models/s3.py b/aws_lambda_powertools/utilities/parser/models/s3.py index 053fc8f15b6..f81811fe887 100644 --- a/aws_lambda_powertools/utilities/parser/models/s3.py +++ b/aws_lambda_powertools/utilities/parser/models/s3.py @@ -88,7 +88,7 @@ class S3EventRecordReplicationEventData(BaseModel): s3Operation: str | None = Field( default=None, description="The S3 operation that was replicated. For Replication events.", - examples=["ObjectCreated:Put"], + examples=["OBJECT_PUT"], ) requestTime: str | None = Field( default=None, @@ -99,7 +99,7 @@ class S3EventRecordReplicationEventData(BaseModel): default=None, description="The reason the replication failed, present only for failed replication events. " "For Replication events.", - examples=["AssumedRole is not authorized to perform the requested operation."], + examples=["AssumeRoleNotPermitted"], ) @@ -554,9 +554,9 @@ class S3RecordModel(BaseModel): { "replicationRuleId": "replication-rule-1", "destinationBucket": "arn:aws:s3:::destination-bucket", - "s3Operation": "ObjectCreated:Put", + "s3Operation": "OBJECT_PUT", "requestTime": "2019-09-03T19:37:27.192Z", - "failureReason": "AssumedRole is not authorized to perform the requested operation.", + "failureReason": "AssumeRoleNotPermitted", }, ], ) diff --git a/tests/events/s3EventReplication.json b/tests/events/s3EventReplication.json index 9b9b296fc44..1d1033988f9 100644 --- a/tests/events/s3EventReplication.json +++ b/tests/events/s3EventReplication.json @@ -8,10 +8,10 @@ "eventVersion": "2.1", "replicationEventData": { "destinationBucket": "arn:aws:s3:::destination-bucket", - "failureReason": "AssumedRole is not authorized to perform the requested operation.", + "failureReason": "AssumeRoleNotPermitted", "replicationRuleId": "replication-rule-1", "requestTime": "2019-09-03T19:37:27.192Z", - "s3Operation": "ObjectCreated:Put" + "s3Operation": "OBJECT_PUT" }, "requestParameters": { "sourceIPAddress": "s3.amazonaws.com"