Injecting key, values into APIGatewayRestResolver object #5282
-
Hi, We're currently using around 10 lambda functions as proxy integrations with APIGateway endpoints. When running our lambda_handler functions, we wish to inject a user class collected from Cognito into the event that is passed into APIGatewayRestResolver.resolve. We have a couple ideas for doing this in an abstracted and repeatable manner. Firstly, build a decorator which injects user attributes as key, value pairs into the lambda event dict, before running APIGatewayRestResolver.resolve on that event. Secondly, build an extended class from the APIGatewayRestResolver class and add the user class injection as part of the resolve function. We were wondering if APIGatewayRestResolver already has an automated way for injecting extra data ito the event as part of the resolve function call, or if anyone had any better suggestions for solving this problem? Thank you |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hi @SeanCoaker, this is a very interesting question that customers always ask about dependency injection for various reasons: testing, code reuse, and others. Personally, I use dependency injection whenever I can, it makes a lot of things easier. While our I created an example that can help you. from aws_lambda_powertools import Logger
from aws_lambda_powertools.event_handler import APIGatewayRestResolver
from aws_lambda_powertools.logging import correlation_paths
from aws_lambda_powertools.utilities.typing import LambdaContext
logger = Logger()
app = APIGatewayRestResolver()
class MyInjectedDependency:
def __init__(self, name: str) -> None:
self.name = name
def return_name(self):
return self.name
@app.get("/hello")
def get_hello():
my_injected_dependency: MyInjectedDependency = app.context.get("my_injected_dependency")
return {"my_dependency_type": str(type(my_injected_dependency)), "my_dependency_method": my_injected_dependency.return_name()}
# You can continue to use other utilities just as before
@logger.inject_lambda_context(correlation_id_path=correlation_paths.API_GATEWAY_REST)
def lambda_handler(event: dict, context: LambdaContext) -> dict:
app.append_context(my_injected_dependency=MyInjectedDependency(name="powertools"))
return app.resolve(event, context) Please let me know if it help you to solve your question. Have a good day. |
Beta Was this translation helpful? Give feedback.
Hi @SeanCoaker, this is a very interesting question that customers always ask about dependency injection for various reasons: testing, code reuse, and others. Personally, I use dependency injection whenever I can, it makes a lot of things easier.
While our
app.resolve(event, context)
function doesn't support dependency injection directly on this, which makes sense because we focus on solving problems to resolve AWS events coming into Lambda, we do allow you to share data/classes/others through what we call contextual data. You can inject data before invokeapp.resolve()
and all routes will have access to that.I created an example that can help you.