question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using parser with envelopes.ApiGatewayEnvelope

See original GitHub issue

Using parser() to parse Event for API Gateway Lambda. APIGatewayProxyEventModel has version as mandatory attribute APIGatewayEventRequestContext has authorizer as mandatory attribute

These are not present in ALL Lambda events from API Gateway via Lambda Proxy integration.

What were you trying to accomplish? Trying to parse payload as a Model according to documentation on parser

Expected Behavior

To correctly parse the API Gateway Event passed to Lambda without throwing an exception

Current Behavior

Validation exception is thrown for missing fields.

Possible Solution

class APIGatewayEventRequestContext(BaseModel):
    ...
    authorizer: Optional[APIGatewayEventAuthorizer]
    ...
class APIGatewayProxyEventModel(BaseModel):
    version: Optional[str]
    ...

Steps to Reproduce (for bugs)

import json
from aws_lambda_powertools.utilities.parser import BaseModel, envelopes
from aws_lambda_powertools.logging import Logger
from aws_lambda_powertools.utilities.parser.parser import parse

# Specify the API payload Model expected
class UserModel(BaseModel):
    username: str
    password1: str
    password2: str

logger = Logger()

def test(event, context):
    parsed_event = parse(event=event, model=UserModel, envelope=envelopes.ApiGatewayEnvelope)
    response = {
        "statusCode": 200,
        "body": json.dumps(event.dict())
    }

    return response

Environment

  • Powertools version used: 1.17.1
  • Packaging format (Layers, PyPi): PyPi
  • AWS Lambda function runtime: 3.8
  • Debugging logs
[ERROR] ValidationError: 2 validation errors for APIGatewayProxyEventModel
version
  field required (type=value_error.missing)
requestContext -> authorizer
  field required (type=value_error.missing)
Traceback (most recent call last):
  File "/var/task/handler.py", line 34, in test
    parsed_event = parse(event=event, model=UserModel, envelope=envelopes.ApiGatewayEnvelope)
  File "/var/task/aws_lambda_powertools/utilities/parser/parser.py", line 148, in parse
    return envelope().parse(data=event, model=model)
  File "/var/task/aws_lambda_powertools/utilities/parser/envelopes/apigw.py", line 30, in parse
    parsed_envelope = APIGatewayProxyEventModel.parse_obj(data)
  File "/var/task/pydantic/main.py", line 578, in parse_obj
    return cls(**obj)
  File "/var/task/pydantic/main.py", line 406, in __init__
    raise validation_error

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
heitorlessacommented, Jul 20, 2021

hey @walmsles - This is now fixed in 1.18.0

More details in the release notes: https://github.com/awslabs/aws-lambda-powertools-python/releases/tag/v1.18.0

1reaction
heitorlessacommented, Jul 17, 2021

Staged for next week’s release - Friday the latest. Let me know if this is impacting production and I can bring it forward

Read more comments on GitHub >

github_iconTop Results From Across the Web

Module aws_lambda_powertools.utilities.parser.envelopes
API Gateway envelope to extract data within body key ... model : Type[Model]: Data model provided to parse after extracting data using envelope...
Read more >
aws-lambda-powertools-python/parser.md at develop - GitHub
Defines data in pure Python classes, then parse, validate and extract only what you want; Built-in envelopes to unwrap, extend, and validate ...
Read more >
Common API Tasks : Parsing an envelope's audit events
Parse audit events in your envelopes' history with the eSignature REST API to find corrections and downloads.
Read more >
Envelopes | Sentry Developer Documentation
Envelopes are optimized for fast parsing and human readability. They support a combination of multiple Items in a single payload, such as: Submit...
Read more >
DocuSign Developer FAQ: Templates, Envelopes, and Connect
API DocuSign eSignature. Dec 5, 2022. The following are answers to some of the most common questions we receive about Templates, Envelope Management, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found