Skip to content

Bug: EventBridgeEvent dataclass has optional fields as compulsory #8300

Description

@amin-farjadi

Expected Behaviour

replay-name field is optional for event bridge events, that should be reflected in EventBridgeEvent data class.

from aws_lambda_powertools.utilities.data_classes import EventBridgeEvent, event_source


def test_event_bridge_event():

    @event_source(data_class=EventBridgeEvent)
    def lambda_handler(event: EventBridgeEvent, _context):
        source = event.source
        _ = event.account
        return source

    event = {
        "version": "0",
        "id": "6a7e8feb-b491-4cf7-a9f1-bf3703467718",
        "detail-type": "EC2 Instance State-change Notification",
        "source": "aws.ec2",
        "account": "111122223333",
        "time": "2017-12-22T18:43:48Z",
        "region": "us-west-1",
        "resources": [],
        "detail": {
            "instance_id": "i-1234567890abcdef0",
            "state": "terminated",
        },
    }

    source = lambda_handler(event, {})

    assert source == "aws.ec2"

Current Behaviour

The above test rasises KeyError for the replay-name field.

Code snippet

Provided above

Possible Solution

Changed all non-compulsory properties (get_id, version, account, time, region, resources, replay_name) to use self.get() and return str | None (or list[str] | None). The three guaranteed fields (source, detail_type, detail) remain unchanged.

Steps to Reproduce

Run the above test

Powertools for AWS Lambda (Python) version

latest

AWS Lambda function runtime

3.14

Packaging format used

Lambda Layers

Debugging logs

self = <aws_lambda_powertools.utilities.data_classes.event_bridge_event.EventBridgeEvent object at 0x1121cb0e0>
key = 'replay-name'

    def __getitem__(self, key: str) -> Any:
>       return self._data[key]
E       KeyError: 'replay-name'

.venv/lib/python3.14/site-packages/aws_lambda_powertools/utilities/data_classes/common.py:98: KeyError

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingtriagePending triage from maintainers

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status
    Pending customer

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions