It is not possible to remember all methods and properties from all classes that you write and use.
That is the reason why I like to have type hinting in my IDE. In fact I will spend time to make sure it works. I even wrote a blog post about how to set it up when I started experimenting with a new language.
In this post I will provide you with a couple of tips that help you develop faster. By leveraging type hinting using the AWS Lambda Powertools
When you are developing AWS Lambda functions you receive an
event and a
context property. If you need metadata from the context you could read the documentation or use type hinting.
As you can see in the example, the IDE will help you find the correct method or property. But what about the
event? The example tells you that we expect a
Dict[str, Any] but this depends on how you invoke the Lambda function.
Event Source Data Classes
Most of the time you know how you invoke your Lambda function. For example an API Gateway proxy integration could trigger the function:
Next to using the data class you could also use a decorator. Then use the type hint in the method signature.
from aws_lambda_powertools.utilities.data_classes import event_source, APIGatewayProxyEvent @event_source(data_class=APIGatewayProxyEvent) def lambda_handler(event: APIGatewayProxyEvent, context): if 'helloworld' in event.path and event.http_method == 'GET': do_something_with(event.body, user)
For a full list see the supported event sources page.
What about my custom events?
You could also invoke a Lambda function with a custom payload. This event is not supported as a data class.
Let say we receive an order with one or more items. By using the parser you can define the event in a model. And you can use that model for type hinting.
from typing import Optional from aws_lambda_powertools.utilities.parser import event_parser, BaseModel, ValidationError from aws_lambda_powertools.utilities.typing import LambdaContext import json class OrderItem(BaseModel): id: int quantity: int description: str class Order(BaseModel): id: int description: str items: List[OrderItem] # use nesting models optional_field: Optional[str] # this field may or may not be available when parsing @event_parser(model=Order) def handler(event: Order, context: LambdaContext): print(event.id) print(event.description) print(event.items) order_items = [items for item in event.items] ...
But my custom event is in an existing event source
Well in that case you need to supply the event source as an envelope.
from aws_lambda_powertools.utilities.parser import event_parser, parse, BaseModel, envelopes from aws_lambda_powertools.utilities.typing import LambdaContext class UserModel(BaseModel): username: str password1: str password2: str # Same behavior but using our decorator @event_parser(model=UserModel, envelope=envelopes.EventBridgeEnvelope) def handler(event: UserModel, context: LambdaContext): assert event.password1 == event.password2
If you found this blog useful I recommend reading the official documentation. It’s a small investment that will pay out in nice and clean code.
No need to remember all the methods and properties and what types the expect and return. An IDE can do that for you so that you can focus on the actual business logic.
Top comments (0)