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/core/event_handler/api_gateway.md
+117-11
Original file line number
Diff line number
Diff line change
@@ -853,27 +853,129 @@ You can instruct API Gateway handler to use a custom serializer to best suit you
853
853
}
854
854
```
855
855
856
-
### Splitting routes across multiple files
856
+
### Split routes with Router
857
857
858
-
When building a larger application, sometimes to helps to split out your routes into multiple file. Also
859
-
there might be cases where you have some shared routes for multiple lambdas like a `health` status lambda
860
-
to be used with Application Load Balancer.
858
+
As you grow the number of routes a given Lambda function should handle, it is natural to split routes into separate files to ease maintenance - That's where the `Router` feature is useful.
861
859
862
-
Below is an example project layout for AWS Lambda Functions using AWS SAM CLI that allows for relative path
863
-
imports (ie: `from .routers import health`).
860
+
Let's assume you have `app.py` as your Lambda function entrypoint and routes in `users.py`, this is how you'd use the `Router` feature.
864
861
865
-
!!! tip "See in `src/app/main.py`, when including a route we can add a prefix to those routes ie: `prefix="/health"`."
866
-
!!! tip "See in `src/app/routers/health.py`, when adding a child logger we use `Logger(child=True)`."
862
+
=== "users.py"
863
+
864
+
We import **Router** instead of **ApiGatewayResolver**; syntax wise is exactly the same.
865
+
866
+
```python hl_lines="4 8 12 15 21"
867
+
import itertools
868
+
from typing import Dict
869
+
870
+
from aws_lambda_powertools import Logger
871
+
from aws_lambda_powertools.event_handler.api_gateway import Router
As routes are now split in their own files, you can optionally instruct `Router` to inject a prefix for all routes during registration.
918
+
919
+
In the previous example, `users.py` routes had a `/users` prefix. We could remove `/users` from all route definitions, and then set `include_router(users.router, prefix="/users")` in the `app.py`.
920
+
921
+
=== "app.py"
922
+
923
+
```python hl_lines="9"
924
+
from typing import Dict
925
+
926
+
from aws_lambda_powertools.event_handler import ApiGatewayResolver
927
+
from aws_lambda_powertools.utilities.typing import LambdaContext
928
+
929
+
import users
930
+
931
+
app = ApiGatewayResolver()
932
+
app.include_router(users.router, prefix="/users") # prefix '/users' to any route in `users.router`
@router.get("/") # /users, when we set the prefix in app.py
952
+
def get_users() -> Dict:
953
+
...
954
+
955
+
@router.get("/<username>")
956
+
def get_user(username: str) -> Dict:
957
+
...
958
+
959
+
# many other related /users routing
960
+
```
961
+
962
+
#### Sample larger layout
963
+
964
+
Below is an example project layout where we have Users routes similar to the previous example, and health check route - We use ALB to demonstrate the UX remains the same.
965
+
966
+
Note that this layout optimizes for code sharing and for those familiar with Python modules. This means multiple functions will share the same `CodeUri` and package, though they are only built once.
967
+
968
+
!!! tip "External dependencies can be [built as a Lambda Layer](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/building-layers.html){target="_blank"} and set as `dev` dependencies for the project, though outside of scope for this documentation."
0 commit comments