This utility provides input validation and advanced parsing. It mitigates any hidden input assumption including value constraints.
Rule of thumb: Always, always, always (!), validate all input.
The Parser will validate the incoming event, extract the input business payload, decode it and validate it according to a predefined schema.
This schema will verify that all required parameters exist, their type is as expected and validate any value constraints.
And all this will be achieved with one line of code.
This "magic" line will allow you to focus on your business logic and not worry about metadata and encapsulating services.
When an AWS Service sends an event that triggers your AWS Lambda function, metadata information is added to the event, and the business logic payload is encapsulated.
Let's call this metadata information 'envelope.'
The envelope contains valuable information, interesting headers, and the interesting part, the business logic input that you wish to process.
That's where it gets tricky.
Each AWS service has its envelope structure and may encapsulate the business logic payload differently.
Supported AWS Services¶
For a complete list click here.
Define Business Logic Schema¶
Use Pydantic schemas to define the expected input format. Extend 'BaseModel' class.
Define type and value constraints.
1 2 3 4 5
The schema defines:
- 'customer_name' - customer name, a non-empty string with up to 20 characters.
- 'order_item_count' - a positive integer representing the number of ordered items that 'customer_name' placed.
Learn about models here and about advanced parsing here.
Usage in Handler¶
The parser is a called with the function 'parse'.
Use the envelope class that matches the AWS service that triggers your AWS Lambda function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Accessing Envelope Metadata¶
You can access the metadata parameters by extending the model class and parsing the input without the envelope class.
Read more about it here.
Read more about the importance of input validation and the potential pitfalls it prevents in my blog. Click HERE.
You can find more information at the official documentation.
Go to https://awslabs.github.io/aws-lambda-powertools-python/latest/utilities/parser/