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

api_gateway_base_path not supported for ALB? #238

Open
ShedPlant opened this issue Feb 23, 2022 · 2 comments · May be fixed by #291
Open

api_gateway_base_path not supported for ALB? #238

ShedPlant opened this issue Feb 23, 2022 · 2 comments · May be fixed by #291
Labels
improvement Improve an existing feature

Comments

@ShedPlant
Copy link

ShedPlant commented Feb 23, 2022

Summary

  • Mangum supports events from AWS API Gateway and AWS ALB.
  • Mangum has a api_gateway_base_path argument to strip the prefix of the http path before passing to the underlying app
  • api_gateway_base_path works as expected for API Gateway events
  • As far as I can tell, api_gateway_base_path does not work for ALB events.

I looked at https://mangum.io/http/ and the GitHub issues and couldn't find this mentioned anywhere.

My Setup

  • Python 3.9
  • mangum 0.12.4
  • starlette 0.17.1

Simplified version of my lambda handler:

def public_foo_function:
    print("Public foo function")

def private_bar_function:
    print("Private bar function")

app = Starlette(
    routes=[
        Route(
            path="/public/foo",
            endpoint=public_foo_function,
        ),
        Route(
            path="/private/bar",
            endpoint=private_bar_function,
        ),
    ]
)

mangum = Mangum(
    app,
    api_gateway_base_path="/prefix",
)

def main(event, context):
    return mangum(event, context)

An external API Gateway v2 HTTP has a route /prefix/public/{proxy+} integrated with the lambda.
An internal ALB has a listener rule /prefix/private/* integrated with the lambda.

Requests to <api gateway url>/prefix/public/foo arrive in the lambda and public_foo_function is called, as expected.

From within my VPC, internal requests to <alb url>/prefix/private/bar arrive in the lambda and Starlette returns Not Found 404. This is unexpected.

As a workaround, if I change the private route in Starlette app from /private/bar to /prefix/private/bar, private_bar_function is called.

Considerations

api_gateway_base_path does have 'api gateway' in the name, and ALB is not an API Gateway. So there's an argument it could be intentional?

But, I expected mangum to behave consistently for all of its supported input events.

@jordaneremieff
Copy link
Owner

jordaneremieff commented Mar 2, 2022

@ShedPlant

api_gateway_base_path does have 'api gateway' in the name, and ALB is not an API Gateway. So there's an argument it could be intentional?

I'm not sure if it is intentional or not. API Gateway was originally the only supported event source and ALB support came long after the api_gateway_base_path setting was first included, so it's possible that those contributors of the ALB support assumed to ignore the setting due to the api_gateway_* prefix in the setting name.

Should we replace api_gateway_base_path with a single base_path configuration that behaves the same way for the ALB handler?

@ShedPlant
Copy link
Author

@jordaneremieff

Should we replace api_gateway_base_path with a single base_path configuration that behaves the same way for the ALB handler?

Thanks for your reply.
That sounds right to me.

@jordaneremieff jordaneremieff added the improvement Improve an existing feature label Mar 3, 2022
@nipun-AiR nipun-AiR linked a pull request Mar 10, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
improvement Improve an existing feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants