-
Notifications
You must be signed in to change notification settings - Fork 412
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs(apigateway): fix sample layout provided #864
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1027,43 +1027,42 @@ When necessary, you can set a prefix when including a router object. This means | |
|
||
#### Sample layout | ||
|
||
!!! info "We use ALB to demonstrate that the UX remains the same" | ||
|
||
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. | ||
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. | ||
|
||
=== "Project layout" | ||
|
||
|
||
```python hl_lines="6 8 10-13" | ||
```python hl_lines="1 8 10 12-15" | ||
. | ||
├── Pipfile # project app & dev dependencies; poetry, pipenv, etc. | ||
├── Pipfile # project app & dev dependencies; poetry, pipenv, etc. | ||
├── Pipfile.lock | ||
├── mypy.ini # namespace_packages = True | ||
├── .env # VSCode only. PYTHONPATH="users:${PYTHONPATH}" | ||
├── users | ||
│ ├── requirements.txt # sam build detect it automatically due to CodeUri: users, e.g. pipenv lock -r > users/requirements.txt | ||
│ ├── lambda_function.py # this will be our users Lambda fn; it could be split in folders if we want separate fns same code base | ||
│ ├── constants.py | ||
│ └── routers # routers module | ||
├── README.md | ||
├── src | ||
│ ├── __init__.py | ||
│ ├── users.py # /users routes, e.g. from routers import users; users.router | ||
│ ├── health.py # /health routes, e.g. from routers import health; health.router | ||
├── template.yaml # SAM template.yml, CodeUri: users, Handler: users.main.lambda_handler | ||
│ ├── requirements.txt # sam build detect it automatically due to CodeUri: src, e.g. pipenv lock -r > src/requirements.txt | ||
│ └── users | ||
│ ├── __init__.py | ||
│ ├── main.py # this will be our users Lambda fn; it could be split in folders if we want separate fns same code base | ||
│ └── routers # routers module | ||
│ ├── __init__.py | ||
│ ├── health.py # /users routes, e.g. from routers import users; users.router | ||
│ └── users.py # /users routes, e.g. from .routers import users; users.router | ||
├── template.yml # SAM template.yml, CodeUri: src, Handler: users.main.lambda_handler | ||
└── tests | ||
├── __init__.py | ||
├── unit | ||
│ ├── __init__.py | ||
│ └── test_users.py # unit tests for the users router | ||
│ └── test_health.py # unit tests for the health router | ||
│ └── test_users.py # unit tests for the users router | ||
│ └── test_health.py # unit tests for the health router | ||
└── functional | ||
├── __init__.py | ||
├── conftest.py # pytest fixtures for the functional tests | ||
└── test_lambda_function.py # functional tests for the main lambda handler | ||
├── conftest.py # pytest fixtures for the functional tests | ||
└── test_main.py # functional tests for the main lambda handler | ||
``` | ||
|
||
=== "template.yml" | ||
|
||
```yaml hl_lines="20-21" | ||
```yaml hl_lines="22-23" | ||
AWSTemplateFormatVersion: '2010-09-09' | ||
Transform: AWS::Serverless-2016-10-31 | ||
Description: Example service with multiple routes | ||
|
@@ -1073,6 +1072,8 @@ This sample project contains an Users function with two distinct set of routes, | |
MemorySize: 512 | ||
Runtime: python3.9 | ||
Tracing: Active | ||
Architectures: | ||
- arm64 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We build Layer for x86 only atm There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. making it |
||
Environment: | ||
Variables: | ||
LOG_LEVEL: INFO | ||
|
@@ -1083,11 +1084,11 @@ This sample project contains an Users function with two distinct set of routes, | |
UsersService: | ||
Type: AWS::Serverless::Function | ||
Properties: | ||
Handler: lambda_function.lambda_handler | ||
CodeUri: users | ||
Handler: users.main.lambda_handler | ||
CodeUri: src | ||
Layers: | ||
# Latest version: https://awslabs.github.io/aws-lambda-powertools-python/latest/#lambda-layer | ||
- !Sub arn:aws:lambda:${AWS::Region}:017000801446:layer:AWSLambdaPowertoolsPython:3 | ||
- !Sub arn:aws:lambda:${AWS::Region}:017000801446:layer:AWSLambdaPowertoolsPython:4 | ||
Events: | ||
ByUser: | ||
Type: Api | ||
|
@@ -1119,7 +1120,7 @@ This sample project contains an Users function with two distinct set of routes, | |
Value: !GetAtt UsersService.Arn | ||
``` | ||
|
||
=== "users/lambda_function.py" | ||
=== "src/users/main.py" | ||
|
||
```python hl_lines="9 15-16" | ||
from typing import Dict | ||
|
@@ -1130,23 +1131,23 @@ This sample project contains an Users function with two distinct set of routes, | |
from aws_lambda_powertools.logging.correlation_paths import APPLICATION_LOAD_BALANCER | ||
from aws_lambda_powertools.utilities.typing import LambdaContext | ||
|
||
from routers import health, users | ||
from .routers import health, users | ||
|
||
tracer = Tracer() | ||
logger = Logger() | ||
app = ApiGatewayResolver(proxy_type=ProxyEventType.ALBEvent) | ||
app = ApiGatewayResolver(proxy_type=ProxyEventType.APIGatewayProxyEvent) | ||
|
||
app.include_router(health.router) | ||
app.include_router(users.router) | ||
|
||
|
||
@logger.inject_lambda_context(correlation_id_path=APPLICATION_LOAD_BALANCER) | ||
@logger.inject_lambda_context(correlation_id_path=API_GATEWAY_REST) | ||
@tracer.capture_lambda_handler | ||
def lambda_handler(event: Dict, context: LambdaContext): | ||
return app.resolve(event, context) | ||
``` | ||
|
||
=== "users/routers/health.py" | ||
=== "src/users/routers/health.py" | ||
|
||
```python hl_lines="4 6-7 10" | ||
from typing import Dict | ||
|
@@ -1169,7 +1170,7 @@ This sample project contains an Users function with two distinct set of routes, | |
```python hl_lines="3" | ||
import json | ||
|
||
from users import main # follows namespace package from root | ||
from src.users import main # follows namespace package from root | ||
|
||
|
||
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, | |
assert ret["body"] == expected | ||
``` | ||
|
||
=== ".env" | ||
|
||
> Note: It is not needed for PyCharm (select folder as source). | ||
|
||
This is necessary for Visual Studio Code, so integrated tooling works without failing import. | ||
|
||
```bash | ||
PYTHONPATH="users:${PYTHONPATH}" | ||
``` | ||
|
||
### Considerations | ||
|
||
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. | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shall we update to poetry pyproject.tomo since we're gonna update the sample cookiecutter already?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@heitorlessa - currently I like
Pipefile
as this is the default for SAM cli and Amplify templates. However I am cool to switch to PoetryThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So let's keep as-is until we migrate others.