Skip to content
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(api-gateway): Sample layout does not work #835

Closed
Tracked by #1009
michaelbrewer opened this issue Nov 19, 2021 · 7 comments
Closed
Tracked by #1009

docs(api-gateway): Sample layout does not work #835

michaelbrewer opened this issue Nov 19, 2021 · 7 comments
Labels
documentation Improvements or additions to documentation

Comments

@michaelbrewer
Copy link
Contributor

michaelbrewer commented Nov 19, 2021

What were you initially searching for in the docs?

Current layout to in the docs for routes across multiple files is not working.

  • Tests out of the box on Pycharm does not work when you select a test to run

Screen Shot 2021-11-18 at 7 33 10 PM

  • Running the lambda products an error too

Screen Shot 2021-11-18 at 7 34 08 PM

Is this related to an existing part of the documentation? Please share a link

If you have a proposed update, please share it here

Screenshot of the tests working on VSCode (without the .env)

Screen Shot 2021-11-18 at 7 36 48 PM

Screenshot of the tests working in Pycharm (without any settings modifications)

Screen Shot 2021-11-18 at 7 37 59 PM

The working layout

.
├── Makefile
├── Pipfile
├── Pipfile.lock
├── README.md
├── mypy.ini
├── src
│   ├── __init__.py
│   └── users
│       ├── __init__.py
│       ├── lambda_function.py
│       ├── requirements.txt
│       └── routers
│           ├── __init__.py
│           ├── health.py
│           └── users.py
├── template.yml
└── tests
    ├── __init__.py
    └── functional
        ├── __init__.py
        ├── conftest.py
        └── test_lambda_function.py

tamplate.yml

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Example service with multiple routes
Globals:
    Function:
        Timeout: 10
        MemorySize: 512
        Runtime: python3.9
        Tracing: Active
        Environment:
            Variables:
                LOG_LEVEL: INFO
                POWERTOOLS_LOGGER_LOG_EVENT: true
                POWERTOOLS_METRICS_NAMESPACE: MyServerlessApplication
                POWERTOOLS_SERVICE_NAME: users
Resources:
    UsersService:
        Type: AWS::Serverless::Function
        Properties:
            Handler: users.lambda_function.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:4
            Events:
                ByUser:
                    Type: Api
                    Properties:
                        Path: /users/{name}
                        Method: GET
                AllUsers:
                    Type: Api
                    Properties:
                        Path: /users
                        Method: GET
                HealthCheck:
                    Type: Api
                    Properties:
                        Path: /status
                        Method: GET
Outputs:
    UsersApiEndpoint:
        Description: "API Gateway endpoint URL for Prod environment for Users Function"
        Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod"
    AllUsersURL:
        Description: "URL to fetch all registered users"
        Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/users"
    ByUserURL:
        Description: "URL to retrieve details by user"
        Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/users/test"
    UsersServiceFunctionArn:
        Description: "Users Lambda Function ARN"
        Value: !GetAtt UsersService.Arn

lambda_function.py

from typing import Dict

from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.event_handler import ApiGatewayResolver
from aws_lambda_powertools.event_handler.api_gateway import ProxyEventType
from aws_lambda_powertools.logging.correlation_paths import APPLICATION_LOAD_BALANCER
from aws_lambda_powertools.utilities.typing import LambdaContext

from .routers import health, users

tracer = Tracer()
logger = Logger()
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)
@tracer.capture_lambda_handler
def lambda_handler(event: Dict, context: LambdaContext):
    return app.resolve(event, context)

test_lambda_function.py

import json

from src.users import lambda_function


def test_lambda_handler(apigw_event, lambda_context):
    ret = lambda_function.lambda_handler(apigw_event, lambda_context)
    expected = json.dumps({"message": "hello universe"}, separators=(",", ":"))

    assert ret["statusCode"] == 200
    assert ret["body"] == expected
@michaelbrewer michaelbrewer added the documentation Improvements or additions to documentation label Nov 19, 2021
@michaelbrewer
Copy link
Contributor Author

@heitorlessa

@michaelbrewer michaelbrewer changed the title docs: m docs: routes across multiple files not working Nov 19, 2021
@michaelbrewer
Copy link
Contributor Author

@heitorlessa - have a look and see if this makes sense.

@heitorlessa
Copy link
Contributor

heitorlessa commented Nov 22, 2021 via email

@michaelbrewer
Copy link
Contributor Author

Thanks @heitorlessa Other than it does not run when deployed (for me at least), the import statement in the unit tests does not work.

@michaelbrewer michaelbrewer changed the title docs: routes across multiple files not working docs(api-gateway): Sample layout does not work Nov 29, 2021
@michaelbrewer
Copy link
Contributor Author

@heitorlessa - have added another branch for the http-api version too

@heitorlessa
Copy link
Contributor

heitorlessa commented Nov 30, 2021 via email

@heitorlessa
Copy link
Contributor

Now available in the latest docs. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants