Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: ValidationError for EventBridgeModel #6112

Closed
tthirietdev opened this issue Feb 17, 2025 · 5 comments · Fixed by #6134
Closed

Bug: ValidationError for EventBridgeModel #6112

tthirietdev opened this issue Feb 17, 2025 · 5 comments · Fixed by #6134
Assignees
Labels
bug Something isn't working parser Parser (Pydantic) utility

Comments

@tthirietdev
Copy link

tthirietdev commented Feb 17, 2025

Expected Behaviour

When running a lambda from an EventBridge scheduler, EventBridgeModel should be able to parse the event.

Current Behaviour

EventBridgeModel fails to parse EventBridge scheduler event because of the field detail.

For some reason, the event returned by AWS has detail property as a string and not as a dict. I am wondering if it's because it is empty.

Exemple of the event received

{
  'version': '0',
  'id': '1967b367-1419-4bf0-87ee-70d64740e942',
  'detail-type': 'Scheduled Event', 
  'source': 'aws.scheduler', 
  'account': '111111111111', 
  'time': '2025-02-17T16:43:00Z', 
  'region': 'eu-west-1', 
  'resources': ['arn:aws:scheduler:eu-west-1:111111111111:schedule/default/example-scheduler'], 
  'detail': '{}'
}

And the returned error

[ERROR] InvalidModelTypeError: Error: 3 validation errors for EventBridgeModel
detail.dict[str,any]
  Input should be a valid dictionary [type=dict_type, input_value='{}', input_type=str]
    For further information visit https://errors.pydantic.dev/2.9/v/dict_type detail.is-subclass[BaseModel]
  
Input should be a subclass of BaseModel [type=is_subclass_of, input_value='{}', input_type=str]
    For further information visit https://errors.pydantic.dev/2.9/v/is_subclass_of detail.BaseModel
  
Input should be a valid dictionary or instance of BaseModel [type=model_type, input_value='{}', input_type=str]
    For further information visit https://errors.pydantic.dev/2.9/v/model_type. Please ensure the Input model inherits from BaseModel,
and your payload adheres to the specified Input model structure.
Model=<class 'aws_lambda_powertools.utilities.parser.models.event_bridge.EventBridgeModel'>

Code snippet

event = {
  'version': '0',
  'id': '1967b367-1419-4bf0-87ee-70d64740e942',
  'detail-type': 'Scheduled Event', 
  'source': 'aws.scheduler', 
  'account': '111111111111', 
  'time': '2025-02-17T16:43:00Z', 
  'region': 'eu-west-1', 
  'resources': ['arn:aws:scheduler:eu-west-1:111111111111:schedule/default/example-scheduler'], 
  'detail': '{}'
}

parse(event, model=EventBridgeModel)


# Or with an actual event bridge scheduler invoking the lambda
@event_parser(model=EventBridgeModel)
def handler(event, context: LambdaContext):
    print(event)

Possible Solution

  • Maybe a pre validator if type is string for detail property that json.loads() the value

Steps to Reproduce

  • Create a lambda with a simple handler that parse EventBridgeModel
  • Create an eventbridge scheduler with a near CRON time.
  • Make sure the target role has lambda:InvokeFunction policy
  • Consult CloudWatch logs

Powertools for AWS Lambda (Python) version

latest

AWS Lambda function runtime

3.12

Packaging format used

Lambda Layers

Debugging logs

@tthirietdev tthirietdev added bug Something isn't working triage Pending triage from maintainers labels Feb 17, 2025
Copy link

boring-cyborg bot commented Feb 17, 2025

Thanks for opening your first issue here! We'll come back to you as soon as we can.
In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link

@leandrodamascena
Copy link
Contributor

Hey @tthirietdev! Thanks for opening this issue and reporting this behavior.

@anafalcao can you please take a look on this?

Copy link
Contributor

⚠️COMMENT VISIBILITY WARNING⚠️

This issue is now closed. Please be mindful that future comments are hard for our team to see.

If you need more assistance, please either tag a team member or open a new issue that references this one.

If you wish to keep having a conversation with other community members under this issue feel free to do so.

@github-actions github-actions bot added the pending-release Fix or implementation already in dev waiting to be released label Feb 20, 2025
@tthirietdev
Copy link
Author

Thanks a lot for your swift handling of this issue. :)

Copy link
Contributor

This is now released under 3.7.0 version!

@github-actions github-actions bot removed the pending-release Fix or implementation already in dev waiting to be released label Feb 25, 2025
@leandrodamascena leandrodamascena moved this from Coming soon to Shipped in Powertools for AWS Lambda (Python) Feb 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working parser Parser (Pydantic) utility
Projects
Status: Shipped
3 participants