Skip to content

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

Closed
@michaelbrewer

Description

@michaelbrewer

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions