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

113 views Asked by At

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

2

There are 2 answers

0
kairius On BEST ANSWER

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");
0
devio On

I think the most appropriate way to do it is via the AWS Console. You can configure your API Mapping in the Custom Domain Names section, and add your stage testStage to the your API GTW; this way, your stage will "absorbed" by AWS GTW, and the path that will land on your app is /messages...

N.B: I'm still looking for a repost.aws on this which I came across recently..