Skip to content

Wrong routing when using Custom Domain Name #773

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

Closed
pismy opened this issue Oct 22, 2021 · 5 comments
Closed

Wrong routing when using Custom Domain Name #773

pismy opened this issue Oct 22, 2021 · 5 comments
Labels
bug Something isn't working

Comments

@pismy
Copy link

pismy commented Oct 22, 2021

I instantiated the Cookiecutter SAM for Python Lambda functions, embedded the Lambda Powertools as a layer, then deployed it to my AWS account: all good!

Then I tried to configure Custom Domain Name for my API Gateway, and the API internal routing is no longer working.

Calling the API through the default execute-api endpoint ✅

curl https://{my-api-id}.execute-api.{region}.amazonaws.com/Prod/hello/pismy

Returns 200 OK:

{"message": "hello pismy"}

With CloudWatch logs:

{
    "level": "INFO",
    "location": "decorate:345",
    "message": {
        "resource": "/hello/{name}",
        "path": "/hello/pismy",
        "httpMethod": "GET",
       ...
        "queryStringParameters": null,
        "multiValueQueryStringParameters": null,
        "pathParameters": null,
        "stageVariables": null,
        "requestContext": {
            "resourcePath": "/hello/{name}",
            "httpMethod": "GET",
            "path": "/Prod/hello/pismy",
            "stage": "Prod",
            "domainPrefix": "abcd1234",
            "domainName": "abcd1234.execute-api.us-east-1.amazonaws.com",
            "apiId": "abcd1234"
        },
        "body": null,
        "isBase64Encoded": false
    },
    ...
}

Calling the API through the Custom Domain Name 🟥

I first registered the API mapping with path hellopath

curl https://api.mydomain.xyz/hellopath/hello/pismy

Returns 404 Not Found:

{"statusCode":404,"message":"Not found"}

With CloudWatch logs:

{
    "level": "INFO",
    "location": "decorate:345",
    "message": {
        "resource": "/hello/{name}",
        "path": "/hellopath/hello/pismy",
        "httpMethod": "GET",
       ...
        "queryStringParameters": null,
        "multiValueQueryStringParameters": null,
        "pathParameters": null,
        "stageVariables": null,
        "requestContext": {
            "resourcePath": "/hello/{name}",
            "httpMethod": "GET",
            "path": "/hellopath/hello/pismy",
            "stage": "Prod",
            "domainPrefix": "api",
            "domainName": "api.mydomain.xyz",
            "apiId": "abcd1234"
        },
        "body": null,
        "isBase64Encoded": false
    },
    ...
}

Possible Solution

As I'm seeing the log in Cloud Watch, that means the request reaches my Lambda, but it must be the internal routing to the handler function that fails.

@app.get("/hello/<name>")
def hello_you(name):
    # query_strings_as_dict = app.current_event.query_string_parameters
    # json_payload = app.current_event.json_body
    return {"message": f"hello {name}"}

I guess Lambda Powertools must be using message.path or message.requestContext.path from the Lambda event rather than message.resource or message.requestContext.resource to implement the routing.

Unfortunately, when using Custom Domain Names, the message.path contains the API mapping prefix.

@pismy pismy added bug Something isn't working triage Pending triage from maintainers labels Oct 22, 2021
@boring-cyborg
Copy link

boring-cyborg bot commented Oct 22, 2021

Thanks for opening your first issue here! We'll come back to you as soon as we can.

@heitorlessa
Copy link
Contributor

Hey @pismy does this feature help?

https://awslabs.github.io/aws-lambda-powertools-python/latest/core/event_handler/api_gateway/#custom-domain-api-mappings

@pismy
Copy link
Author

pismy commented Oct 22, 2021

Right, that's what I needed.

I'll close this issue (as it's not a bug), but I would appreciate very much to have your answer on this first @heitorlessa :)

It's a bit sad that the prefix shall be hardcoded in the code.
By the way - depending on the software lifecycle - the same code might need to be deployed first with a prefix staging, and then a prefix prod right ? Then the code shall be rebuilt first... I guess the prefix could be passed as an env variable... 🤔

I'm wondering: wouldn't it be possible to handle Custom Domain Name support implicitly, simply by implementing routing based on message.resource rather than message.path ?

@heitorlessa
Copy link
Contributor

heitorlessa commented Oct 22, 2021 via email

@pismy
Copy link
Author

pismy commented Oct 22, 2021

Thank you so much for your answer.

@pismy pismy closed this as completed Oct 22, 2021
@heitorlessa heitorlessa removed the triage Pending triage from maintainers label Feb 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants