Closed
Description
Is this related to an existing feature request or issue?
Which AWS Lambda Powertools utility does this relate to?
Event Handler - REST API
Summary
It would be useful if you could provide an optional dict to the app.resolve()
method, that would then be available in the handlers themselves.
This would contain generic information that is needed for all requests.
Use case
We are implementing callbacks for Twilio. For every event, we need to:
- Decode the body
- Parse the parameters
- Validate the request
Next, we need the same parameters from step 2 in almost every request.
Proposal
Provide an extra parameter to the resolve()
method:
app.py
from aws_lambda_powertools.event_handler import APIGatewayRestResolver
from aws_lambda_powertools.utilities.typing import LambdaContext
import todos
app = APIGatewayRestResolver()
app.include_router(todos.router)
def lambda_handler(event: dict, context: LambdaContext) -> dict:
some_info = {"some_custom": "data"}
return app.resolve(event, context, route_context=some_info)
In the handler itself, it could then be accessible via route_context
:
todos.py
import requests
from aws_lambda_powertools.event_handler.api_gateway import Router
from requests import Response
router = Router()
@router.get("/todos")
def get_todos():
some_data = router.route_context.get("some_custom")
todos: Response = requests.get("https://jsonplaceholder.typicode.com/todos")
todos.raise_for_status()
return {"todos": todos.json()[:10]}
Out of scope
n/a
Potential challenges
n/a
Dependencies and Integrations
No response
Alternative solutions
Currently I'm passing the data in app.lambda_context.__dict__
.
Acknowledgment
- This feature request meets Lambda Powertools Tenets
- Should this be considered in other Lambda Powertools languages? i.e. Java, TypeScript