You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/utilities/middleware_factory.md
+106-51
Original file line number
Diff line number
Diff line change
@@ -3,89 +3,144 @@ title: Middleware factory
3
3
description: Utility
4
4
---
5
5
6
+
<!-- markdownlint-disable MD043 -->
7
+
6
8
Middleware factory provides a decorator factory to create your own middleware to run logic before, and after each Lambda invocation synchronously.
7
9
8
10
## Key features
9
11
10
12
* Run logic before, after, and handle exceptions
11
-
* Trace each middleware when requested
13
+
* Built-in tracing opt-in capability
14
+
15
+
## Getting started
16
+
17
+
???+ tip
18
+
All examples shared in this documentation are available within the [project repository](https://github.com/awslabs/aws-lambda-powertools-python/tree/develop/examples){target="_blank"}.
19
+
20
+
You might need a custom middleware to abstract non-functional code. These are often custom authorization or any reusable logic you might need to run before/after a Lambda function invocation.
12
21
13
-
## Middleware with no params
22
+
###Middleware with no params
14
23
15
24
You can create your own middleware using `lambda_handler_decorator`. The decorator factory expects 3 arguments in your function signature:
16
25
17
26
***handler** - Lambda function handler
18
27
***event** - Lambda function invocation event
19
28
***context** - Lambda function context object
20
29
21
-
```python hl_lines="3-4 10" title="Creating your own middleware for before/after logic"
22
-
from aws_lambda_powertools.middleware_factory import lambda_handler_decorator
For advanced use cases, you can instantiate [Tracer](../core/tracer.md) inside your middleware, and add annotations as well as metadata for additional operational insights.
If you are making use of [Tracer](../core/tracer.md), you can trace the execution of your middleware to ease operations.
59
91
60
92
This makes use of an existing Tracer instance that you may have initialized anywhere in your code.
61
93
62
-
```python hl_lines="3" title="Tracing custom middlewares with Tracer"
63
-
from aws_lambda_powertools.middleware_factory import lambda_handler_decorator
94
+
???+ warning
95
+
You must [enable Active Tracing](../core/tracer/#permissions) in your Lambda function when using this feature, otherwise Lambda cannot send traces to XRay.
For advanced use cases, you can instantiate [Tracer](../core/tracer.md) inside your middleware, and add annotations as well as metadata for additional operational insights.
108
+
When executed, your middleware name will [appear in AWS X-Ray Trace details as](../core/tracer.md)`## middleware_name`, in this example the middleware name is `## middleware_with_tracing`.
You can create your own middleware and combine many features of Lambda Powertools such as [trace](../core/logger.md), [logs](../core/logger.md), [feature flags](feature_flags.md), [validation](validation.md), [jmespath_functions](jmespath_functions.md) and others to abstract non-functional code.
115
+
116
+
In the example below, we create a Middleware with the following features:
117
+
118
+
* Logs and traces
119
+
* Validate if the payload contains a specific header
120
+
* Extract specific keys from event
121
+
* Automatically add security headers on every execution
Description: Middleware-powertools-utilities example
4
+
5
+
Globals:
6
+
Function:
7
+
Timeout: 5
8
+
Runtime: python3.9
9
+
Tracing: Active
10
+
Architectures:
11
+
- x86_64
12
+
Environment:
13
+
Variables:
14
+
LOG_LEVEL: DEBUG
15
+
POWERTOOLS_LOGGER_SAMPLE_RATE: 0.1
16
+
POWERTOOLS_LOGGER_LOG_EVENT: true
17
+
POWERTOOLS_SERVICE_NAME: middleware
18
+
19
+
Resources:
20
+
MiddlewareFunction:
21
+
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
0 commit comments