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

Axum cannot route properly as proxy lambda to API gatway because the stage name is passed through #782

Closed
Bryson14 opened this issue Jan 17, 2024 · 4 comments · Fixed by #784

Comments

@Bryson14
Copy link
Contributor

As seen on this example page, you are using axum to route the incoming request.

I have a router set up something like this:

pub fn get_router() -> axum::Router {
    // With Routing, order does matter, so put the more specific first and then more generic routes later
    Router::new()
        .route(
            "/message/conversation/:conversation_id/message/:message_id",
            get(route_handlers::message::get_message),
        )
        .route(
            "/messages/conversation/:conversation_id",
            get(route_handlers::message::get_messages_by_conversation_id),
        )
        .route("/*path", get(catch_get).post(catch_post)) // just for catching and client feedback
        ...

This lambda works great when tested from the AWS Lambda console, but fails when called from the API gateway.

aws apigateway test-invoke-method --rest-api-id 5oclxtbcq3 --resource-id dnse2ng1jh --http-method GET --path-with-query-string '/conversation/user/c8089eae-5e7d-11ec-94c8-b42e99b5b2c6/conversation/059f27eb-f921-4a8c-b52b-09195f4204be' --region us-east-1
{
    "status": 404,
    "body": "{\"error_message\":\"unknown GET request to route 'test-invoke-stage/conversation/user/c8089eae-5e7d-11ec-94c8-b42e99b5b2c6/conversation/059f27eb-f921-4a8c-b52b-09195f4204be'\"}",
    "headers": {
        "X-Amzn-Trace-Id": "Root=1-65a8480d-fa17b2d9ea4b2aa6dc4f88c8;Sampled=0;lineage=b2a823c1:0",
        "content-length": "173",
        "content-type": "application/json"
    },
    "multiValueHeaders": {
        "X-Amzn-Trace-Id": [
            "Root=1-65a8480d-fa17b2d9ea4b2aa6dc4f88c8;Sampled=0;lineage=b2a823c1:0"
        ],
        "content-length": [
            "173"
        ],
        "content-type": [
            "application/json"
        ]
    },
    "log": "Execution log for request 3c07bc93-1162-46de-8d78-fffc80a37d8a\nWed Jan 17 21:35:09 UTC 2024 : Starting execution for request: 3c07bc93-1162-46de-8d78-fffc80a37d8a\nWed Jan 17 21:35:09 UTC 2024 : HTTP Method: GET, Resource Path: /conversation/user/c8089eae-5e7d-11ec-94c8-b42e99b5b2c6/conversation/059f27eb-f921-4a8c-b52b-09195f4204be\nWed Jan 17 21:35:09 UTC 2024 : Method request path: {}\nWed Jan 17 21:35:09 UTC 2024 : Method request query string: {}\nWed Jan 17 21:35:09 UTC 2024 : Method request headers: {}\nWed Jan 17 21:35:09 UTC 2024 : Method request body before transformations: \nWed Jan 17 21:35:09 UTC 2024 : Endpoint request URI: https://lambda.us-east-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:us-east-1:11:

I have not configured any stage for this API but still the test-invoke-stage prefix in on the api request.

How can I remove this or deal with it in the router?

@Bryson14
Copy link
Contributor Author

On way to get around this is to add a nested route which reads from an environment variable. This isn't ideal but works for now.

 // This is a way to route even though API Gateway can add its stage name to the front of th URI request
    // i.e. user asks for /conversation/123, but the API gateway asks for /test-stage/conversation/123
    // Check if STAGE_NAME environment variable is set
    if let Ok(stage_name) = std::env::var("STAGE_NAME") {
        
        let parent_router = Router::new()
            .nest(&format!("/{}", stage_name), app)
            .route("/*path", get(catch_get).post(catch_post))
            .layer(
                TraceLayer::new_for_http()
                    .make_span_with(trace::DefaultMakeSpan::new().level(LOG_LEVEL))
                    .on_response(trace::DefaultOnResponse::new().level(LOG_LEVEL)),
            );
        // Apply the middleware to your app
        app = parent_router;
    }

@calavera
Copy link
Contributor

You can disable that behavior, I've documented how to do it in #784

@Bryson14
Copy link
Contributor Author

Thank you!!

Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for the maintainers of this repository to see.
If you need more assistance, please open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

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

Successfully merging a pull request may close this issue.

2 participants