Increasing your development speed with AWS Lambda Powertools
Tags: aws, lambda, powertools
Published: 2022-12-12 08:34:08

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](https://binx.io/blog/2021/07/27/use-sam-go-and-vs-code/) 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](https://awslabs.github.io/aws-lambda-powertools-python/latest/)

LambdaContext
-------------

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.

![LambdaContext type hinting example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cqma6alhjb5vbvlqf7pj.png)

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:

![LambdaContext type hinting example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cqma6alhjb5vbvlqf7pj.png)

Next to using the data class you could also use a decorator. Then use the type hint in the method signature.

```python
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](https://awslabs.github.io/aws-lambda-powertools-python/latest/utilities/data_classes/) 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](https://awslabs.github.io/aws-lambda-powertools-python/latest/utilities/parser/) you can define the event in a model. And you can use that model for type hinting.

```python
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.

```python
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
```

Conclusion
----------

If you found this blog useful I recommend reading the official [documentation](https://awslabs.github.io/aws-lambda-powertools-python/latest/). 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.