Middleware factory
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
Middleware with no params¶
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
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Middleware with params¶
You can also have your own keyword arguments after the mandatory arguments.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Tracing middleware execution¶
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.
1 2 3 4 5 6 7 8 9 |
|
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.
1 2 3 4 5 6 7 8 9 |
|
Tips¶
- 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
Testing your code¶
When unit testing middlewares with trace_execution
option enabled, use POWERTOOLS_TRACE_DISABLED
env var to safely disable Tracer.
1 |
|