title | description |
---|---|
Middleware factory |
Utility |
import Note from "../../src/components/Note"
Middleware factory provides a decorator factory to create your own middleware to run logic before, and after each Lambda invocation synchronously.
Key features
- Run logic before, after, and handle exceptions
- Trace each middleware when requested
You can create your own middleware using lambda_handler_decorator
. The decorator factory expects 3 arguments in your function signature:
- handler - Lambda function handler
- event - Lambda function invocation event
- context - Lambda function context object
from aws_lambda_powertools.middleware_factory import lambda_handler_decorator
# highlight-start
@lambda_handler_decorator
def middleware_before_after(handler, event, context):
# highlight-end
# logic_before_handler_execution()
response = handler(event, context)
# logic_after_handler_execution()
return response
@middleware_before_after # highlight-line
def lambda_handler(event, context):
...
You can also have your own keyword arguments after the mandatory arguments.
@lambda_handler_decorator
def obfuscate_sensitive_data(handler, event, context, fields: List = None): # highlight-line
# Obfuscate email before calling Lambda handler
if fields:
for field in fields:
field = event.get(field, "")
if field in event:
event[field] = obfuscate(field)
return handler(event, context)
@obfuscate_sensitive_data(fields=["email"]) # highlight-line
def lambda_handler(event, context):
...
If you are making use of Tracer, you can trace the execution of your middleware to ease operations.
This makes use of an existing Tracer instance that you may have initialized anywhere in your code.
from aws_lambda_powertools.middleware_factory import lambda_handler_decorator
@lambda_handler_decorator(trace_execution=True) # highlight-line
def my_middleware(handler, event, context):
return handler(event, context)
@my_middleware
def lambda_handler(event, context):
...
When executed, your middleware name will appear in AWS X-Ray Trace details as ## middleware_name
.
For advanced use cases, you can instantiate Tracer inside your middleware, and add annotations as well as metadata for additional operational insights.
from aws_lambda_powertools.middleware_factory import lambda_handler_decorator
from aws_lambda_powertools.tracing import Tracer
@lambda_handler_decorator(trace_execution=True)
def middleware_name(handler, event, context):
# highlight-start
tracer = Tracer() # Takes a copy of an existing tracer instance
tracer.add_annotation...
tracer.add_metadata...
# highlight-end
return handler(event, context)
- Use
trace_execution
to quickly understand the performance impact of your middlewares, and reduce or merge tasks when necessary - When nesting multiple middlewares, always return the handler with event and context, or response
- Keep in mind Python decorators execution order. Lambda handler is actually called once (top-down)
- Async middlewares are not supported
When unit testing middlewares with trace_execution
option enabled, use POWERTOOLS_TRACE_DISABLED
env var to safely disable Tracer.
POWERTOOLS_TRACE_DISABLED=1 python -m pytest