Skip to content

Commit fcfa2e4

Browse files
author
Michael Brewer
authored
docs(apigateway): fix sample layout provided (#864)
1 parent 8cfa773 commit fcfa2e4

File tree

1 file changed

+30
-39
lines changed

1 file changed

+30
-39
lines changed

Diff for: docs/core/event_handler/api_gateway.md

+30-39
Original file line numberDiff line numberDiff line change
@@ -1027,43 +1027,42 @@ When necessary, you can set a prefix when including a router object. This means
10271027

10281028
#### Sample layout
10291029

1030-
!!! info "We use ALB to demonstrate that the UX remains the same"
1031-
1032-
This sample project contains an Users function with two distinct set of routes, `/users` and `/health`. The layout optimizes for code sharing, no custom build tooling, and it uses [Lambda Layers](../../index.md#lambda-layer) to install Lambda Powertools.
1030+
This sample project contains a Users function with two distinct set of routes, `/users` and `/health`. The layout optimizes for code sharing, no custom build tooling, and it uses [Lambda Layers](../../index.md#lambda-layer) to install Lambda Powertools.
10331031

10341032
=== "Project layout"
10351033

10361034

1037-
```python hl_lines="6 8 10-13"
1035+
```python hl_lines="1 8 10 12-15"
10381036
.
1039-
├── Pipfile # project app & dev dependencies; poetry, pipenv, etc.
1037+
├── Pipfile # project app & dev dependencies; poetry, pipenv, etc.
10401038
├── Pipfile.lock
1041-
├── mypy.ini # namespace_packages = True
1042-
├── .env # VSCode only. PYTHONPATH="users:${PYTHONPATH}"
1043-
├── users
1044-
│ ├── requirements.txt # sam build detect it automatically due to CodeUri: users, e.g. pipenv lock -r > users/requirements.txt
1045-
│ ├── lambda_function.py # this will be our users Lambda fn; it could be split in folders if we want separate fns same code base
1046-
│ ├── constants.py
1047-
│ └── routers # routers module
1039+
├── README.md
1040+
├── src
10481041
│ ├── __init__.py
1049-
│ ├── users.py # /users routes, e.g. from routers import users; users.router
1050-
│ ├── health.py # /health routes, e.g. from routers import health; health.router
1051-
├── template.yaml # SAM template.yml, CodeUri: users, Handler: users.main.lambda_handler
1042+
│ ├── requirements.txt # sam build detect it automatically due to CodeUri: src, e.g. pipenv lock -r > src/requirements.txt
1043+
│ └── users
1044+
│ ├── __init__.py
1045+
│ ├── main.py # this will be our users Lambda fn; it could be split in folders if we want separate fns same code base
1046+
│ └── routers # routers module
1047+
│ ├── __init__.py
1048+
│ ├── health.py # /users routes, e.g. from routers import users; users.router
1049+
│ └── users.py # /users routes, e.g. from .routers import users; users.router
1050+
├── template.yml # SAM template.yml, CodeUri: src, Handler: users.main.lambda_handler
10521051
└── tests
10531052
├── __init__.py
10541053
├── unit
10551054
│ ├── __init__.py
1056-
│ └── test_users.py # unit tests for the users router
1057-
│ └── test_health.py # unit tests for the health router
1055+
│ └── test_users.py # unit tests for the users router
1056+
│ └── test_health.py # unit tests for the health router
10581057
└── functional
10591058
├── __init__.py
1060-
├── conftest.py # pytest fixtures for the functional tests
1061-
└── test_lambda_function.py # functional tests for the main lambda handler
1059+
├── conftest.py # pytest fixtures for the functional tests
1060+
└── test_main.py # functional tests for the main lambda handler
10621061
```
10631062

10641063
=== "template.yml"
10651064

1066-
```yaml hl_lines="20-21"
1065+
```yaml hl_lines="22-23"
10671066
AWSTemplateFormatVersion: '2010-09-09'
10681067
Transform: AWS::Serverless-2016-10-31
10691068
Description: Example service with multiple routes
@@ -1073,6 +1072,8 @@ This sample project contains an Users function with two distinct set of routes,
10731072
MemorySize: 512
10741073
Runtime: python3.9
10751074
Tracing: Active
1075+
Architectures:
1076+
- x86_64
10761077
Environment:
10771078
Variables:
10781079
LOG_LEVEL: INFO
@@ -1083,11 +1084,11 @@ This sample project contains an Users function with two distinct set of routes,
10831084
UsersService:
10841085
Type: AWS::Serverless::Function
10851086
Properties:
1086-
Handler: lambda_function.lambda_handler
1087-
CodeUri: users
1087+
Handler: users.main.lambda_handler
1088+
CodeUri: src
10881089
Layers:
10891090
# Latest version: https://awslabs.github.io/aws-lambda-powertools-python/latest/#lambda-layer
1090-
- !Sub arn:aws:lambda:${AWS::Region}:017000801446:layer:AWSLambdaPowertoolsPython:3
1091+
- !Sub arn:aws:lambda:${AWS::Region}:017000801446:layer:AWSLambdaPowertoolsPython:4
10911092
Events:
10921093
ByUser:
10931094
Type: Api
@@ -1119,7 +1120,7 @@ This sample project contains an Users function with two distinct set of routes,
11191120
Value: !GetAtt UsersService.Arn
11201121
```
11211122

1122-
=== "users/lambda_function.py"
1123+
=== "src/users/main.py"
11231124

11241125
```python hl_lines="9 15-16"
11251126
from typing import Dict
@@ -1130,23 +1131,23 @@ This sample project contains an Users function with two distinct set of routes,
11301131
from aws_lambda_powertools.logging.correlation_paths import APPLICATION_LOAD_BALANCER
11311132
from aws_lambda_powertools.utilities.typing import LambdaContext
11321133

1133-
from routers import health, users
1134+
from .routers import health, users
11341135

11351136
tracer = Tracer()
11361137
logger = Logger()
1137-
app = ApiGatewayResolver(proxy_type=ProxyEventType.ALBEvent)
1138+
app = ApiGatewayResolver(proxy_type=ProxyEventType.APIGatewayProxyEvent)
11381139

11391140
app.include_router(health.router)
11401141
app.include_router(users.router)
11411142

11421143

1143-
@logger.inject_lambda_context(correlation_id_path=APPLICATION_LOAD_BALANCER)
1144+
@logger.inject_lambda_context(correlation_id_path=API_GATEWAY_REST)
11441145
@tracer.capture_lambda_handler
11451146
def lambda_handler(event: Dict, context: LambdaContext):
11461147
return app.resolve(event, context)
11471148
```
11481149

1149-
=== "users/routers/health.py"
1150+
=== "src/users/routers/health.py"
11501151

11511152
```python hl_lines="4 6-7 10"
11521153
from typing import Dict
@@ -1169,7 +1170,7 @@ This sample project contains an Users function with two distinct set of routes,
11691170
```python hl_lines="3"
11701171
import json
11711172

1172-
from users import main # follows namespace package from root
1173+
from src.users import main # follows namespace package from root
11731174

11741175

11751176
def test_lambda_handler(apigw_event, lambda_context):
@@ -1180,16 +1181,6 @@ This sample project contains an Users function with two distinct set of routes,
11801181
assert ret["body"] == expected
11811182
```
11821183

1183-
=== ".env"
1184-
1185-
> Note: It is not needed for PyCharm (select folder as source).
1186-
1187-
This is necessary for Visual Studio Code, so integrated tooling works without failing import.
1188-
1189-
```bash
1190-
PYTHONPATH="users:${PYTHONPATH}"
1191-
```
1192-
11931184
### Considerations
11941185

11951186
This utility is optimized for fast startup, minimal feature set, and to quickly on-board customers familiar with frameworks like Flask — it's not meant to be a fully fledged framework.

0 commit comments

Comments
 (0)