aws / aws/aws-lambda-rust-runtime

`AWS_LAMBDA_HTTP_IGNORE_STAGE_IN_PATH` does not ignore stage in API Gateway V2 (HTTP)

オープン
#919 コメント 6 件 リアクション 1 件 担当者 0 名 GitHub で見る
主要言語
Rust
スター
3.6k
フォーク
396
PR マージ指標
30日以内にマージされた PR はありません

説明

The `AWS_LAMBDA_HTTP_IGNORE_STAGE_IN_PATH` env variable doesn't seem to work on API Gateway V2 (HTTP).

Example:
```rust
use axum::{response::Json, routing::get, Router};
use lambda_http::request::RequestContext::ApiGatewayV2;
use lambda_http::{run, tracing, Error};
use serde_json::{json, Value};

// Sample middleware that logs the request id
async fn mw_sample(
req: axum::extract::Request,
next: axum::middleware::Next,
) -> impl axum::response::IntoResponse {
let context = req
.extensions()
.get::();
if let Some(ApiGatewayV2(ctx)) = context {
tracing::info!("RequestId = {:?}", ctx);
}
next.run(req).await
}

async fn handler_sample() -> Json {
Json(json!({ "echo": "hello" }))
}

#[tokio::main]
async fn main() -> Result<(), Error> {
std::env::set_var("AWS_LAMBDA_HTTP_IGNORE_STAGE_IN_PATH", "true"); // ignore stages in routing

tracing::init_default_subscriber();

let app = Router::new()
.route("/not-staged", get(handler_sample)) // stage not specified, not working
.route("/v1/staged", get(handler_sample)) // stage specified, working
.layer(axum::middleware::from_fn(mw_sample));

run(app).await
}
```

Cargo.toml:
```toml
[package]
name = "test"
version = "0.1.0"
edition = "2021"

[dependencies]
axum = "0.7.5"
lambda_http = "0.13.0"
serde = "1.0.209"
serde_json = "1.0.127"
tokio = { version = "1", features = ["macros"] }
```

I will attach my Terraform code as well:
```hcl
module "api_lambda_function" {
source = "terraform-aws-modules/lambda/aws"

function_name = "some-api"

runtime = "provided.al2023"
architectures = ["arm64"]
timeout = 15

create_package = false
// Change path to your bin
local_existing_package = "../../../target/lambda/simple2/bootstrap.zip"
handler = "bootstrap"

create_current_version_allowed_triggers = false
allowed_triggers = {
AllowExecutionFromAPIGateway = {
service = "apigateway"
source_arn = "${aws_apigatewayv2_api.main.execution_arn}/*"
}
}
}

resource "aws_apigatewayv2_api" "main" {
name = "${local.name_prefix}-api"
protocol_type = "HTTP"
}

resource "aws_apigatewayv2_stage" "v1" {
api_id = aws_apigatewayv2_api.main.id
name = "v1"
auto_deploy = true
}

resource "aws_apigatewayv2_integration" "api_lambda" {
api_id = aws_apigatewayv2_api.main.id
integration_type = "AWS_PROXY"
integration_method = "POST"
integration_uri = module.api_lambda_function.lambda_function_arn
payload_format_version = "2.0"
depends_on = [module.api_lambda_function.lambda_function_arn]
}

resource "aws_apigatewayv2_route" "get_1" {
api_id = aws_apigatewayv2_api.main.id
route_key = "GET /not-staged"
target = "integrations/${aws_apigatewayv2_integration.api_lambda.id}"
}

resource "aws_apigatewayv2_route" "get_2" {
api_id = aws_apigatewayv2_api.main.id
route_key = "GET /staged"
target = "integrations/${aws_apigatewayv2_integration.api_lambda.id}"
}
```

Build with:
```
cargo lambda build --release --arm64 --output-format zip
```
And then apply Terraform.

When calling a `/v1/staged` route, response is returned:
![image](https://github.com/user-attachments/assets/6fc0f28e-c72e-4cb2-999a-200b2beb7841)

But not for `/v1/not-staged` - response is 404:
![image](https://github.com/user-attachments/assets/5f2e32f2-a439-4b45-9bbc-866ce838edb8)

Am I doing something wrong?
Can someone please suggest how to debug this issue?

コントリビューションガイド

コントリビューションガイドを開く

調査の方向性

まず、AWS_LAMBDA_HTTP_IGNORE_STAGE_IN_PATH を有効にして、提供されている Axum および API Gateway V2 の再現手順を実行し、次に RequestContext::ApiGatewayV2 と環境変数の処理を追跡します。/v1/not-staged リクエストが /not-staged ルートに到達し、staged ルートも引き続き動作すれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
aws, rust
領域
api, backend
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
静か
明瞭さ
おおむね明確
初心者へのやさしさ
48/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。