rustaws-lambdaaws-api-gatewayrust-axumcargo-lambda

How to deal with different route URLS from api gateway to lambda with Axum Rust?


I have a rust lambda that handles fetching data from dynamo DB.

The issues occurs when API Gateway passes the proxy information onto the lambda, it also includes the stage in the URL. For example, here is the api_gateway example json that is given by the aws-rust-runtime crate which includes the stage = testStage.

What this means is that the routing must now include that testStage at the beginning of the route or else nothing will match. This:

pub fn get_router() -> axum::Router {
    Router::new()
        .route("/messages/message/:message_id", get(get_message))
        .route("/messages/message", post(create_message))
        ...
}

must now be

pub fn get_router() -> axum::Router {
    Router::new()
        .route("/testStage/messages/message/:message_id", get(get_message))
        .route("/testStage/messages/message", post(create_message))
        ...
}

This is annoying and problematic when deploying this function with IaaC across different stages and different environments.

Is there a way to deal with this or cut it out from the route from API Gateway?

I thought there might be a way to tell Axum to pay attention to only certain parts of the payload coming from Gateway, but it seems by default, axum is getting the entire uri as seen in this trace {method=GET uri=https://gy415nuibc.execute-api.us-east-2.amazonaws.com/testStage/messages/message/okay


Solution

  • If you use lambda_http for the runtime, you can just set this to cut stage names from the path:

    std::env::set_var("AWS_LAMBDA_HTTP_IGNORE_STAGE_IN_PATH", "true");