From 186ddd2f91e541e4de67c298cda7509275fc5b17 Mon Sep 17 00:00:00 2001 From: alexgallotta <5581237+alexgallotta@users.noreply.github.com> Date: Tue, 15 Oct 2024 17:43:00 -0400 Subject: [PATCH 1/2] feat: add AWS_LWA_PROXY_LAMBDA_RUNTIME_API to overwrite AWS_LAMBDA_RUNTIME_API and use a proxy --- README.md | 1 + src/lib.rs | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/README.md b/README.md index 27f5463..593b658 100644 --- a/README.md +++ b/README.md @@ -106,6 +106,7 @@ The readiness check port/path and traffic port can be configured using environme | AWS_LWA_PASS_THROUGH_PATH | the path for receiving event payloads that are passed through from non-http triggers | "/events" | | AWS_LWA_AUTHORIZATION_SOURCE | a header name to be replaced to `Authorization` | None | | AWS_LWA_ERROR_STATUS_CODES | comma-separated list of HTTP status codes that will cause Lambda invocations to fail (e.g. "500,502-504,422") | None | +| AWS_LWA_PROXY_LAMBDA_RUNTIME_API | overwrites `AWS_LAMBDA_RUNTIME_API` to allow proxying request (not affecting registration) | None | > **Note:** > We use "AWS_LWA_" prefix to namespacing all environment variables used by Lambda Web Adapter. The original ones will be supported until we reach version 1.0. diff --git a/src/lib.rs b/src/lib.rs index 87c481d..15efd5f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -310,6 +310,13 @@ impl Adapter { let compression = self.compression; let invoke_mode = self.invoke_mode; + if let Ok(runtime_proxy) = env::var("AWS_LWA_PROXY_LAMBDA_RUNTIME_API") { + // overwrite the env variable since + // LambdaInvokeMode::Buffered => lambda_http::run(svc).await, + // calls the lambda lambda_runtime::run which doesn't allow to change the client URI + env::set_var("AWS_LAMBDA_RUNTIME_API", runtime_proxy); + } + if compression { let svc = ServiceBuilder::new().layer(CompressionLayer::new()).service(self); match invoke_mode { From 335ffe6d5cca88ac119764edb710a9c83e7d59e4 Mon Sep 17 00:00:00 2001 From: alexgallotta <5581237+alexgallotta@users.noreply.github.com> Date: Wed, 9 Apr 2025 09:39:10 -0400 Subject: [PATCH 2/2] rename runtime proxy env variable to AWS_LWA_LAMBDA_RUNTIME_API_PROXY and add detailed documentation --- README.md | 6 +++++- src/lib.rs | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 593b658..c283994 100644 --- a/README.md +++ b/README.md @@ -106,7 +106,7 @@ The readiness check port/path and traffic port can be configured using environme | AWS_LWA_PASS_THROUGH_PATH | the path for receiving event payloads that are passed through from non-http triggers | "/events" | | AWS_LWA_AUTHORIZATION_SOURCE | a header name to be replaced to `Authorization` | None | | AWS_LWA_ERROR_STATUS_CODES | comma-separated list of HTTP status codes that will cause Lambda invocations to fail (e.g. "500,502-504,422") | None | -| AWS_LWA_PROXY_LAMBDA_RUNTIME_API | overwrites `AWS_LAMBDA_RUNTIME_API` to allow proxying request (not affecting registration) | None | +| AWS_LWA_LAMBDA_RUNTIME_API_PROXY | overwrites `AWS_LAMBDA_RUNTIME_API` to allow proxying request (not affecting registration) | None | > **Note:** > We use "AWS_LWA_" prefix to namespacing all environment variables used by Lambda Web Adapter. The original ones will be supported until we reach version 1.0. @@ -173,6 +173,10 @@ Please note that `sam local` starts a Lambda Runtime Interface Emulator on port The Lambda Web Adapter also supports all non-HTTP event triggers, such as SQS, SNS, S3, DynamoDB, Kinesis, Kafka, EventBridge, and Bedrock Agents. The adapter forwards the event payload to the web application via http post to a path defined by the `AWS_LWA_PASS_THROUGH_PATH` environment variable. By default, this path is set to `/events`. Upon receiving the event payload from the request body, the web application should processes it and returns the results as a JSON response. Please checkout [SQS Express.js](examples/sqs-expressjs) and [Bedrock Agent FastAPI in Zip](examples/bedrock-agent-fastapi-zip) examples. +## Intercepting request and response + +The `AWS_LWA_LAMBDA_RUNTIME_API_PROXY` environment varible makes the Lambda Web Adapter redirect the requests to a custom proxy URL. The proxy can then intercept the requests of the [Lambda runtime API](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-api.html), and apply arbitrary operations such as inspection or modification. Possible applications are tracing, payload capturing, obfuscation of sensitive data, headers modification. Note that the payload of the request received by the web application is wrapped inside the GET response body. This proxy _does not_ affect the extension registering API and is meant to be used only to interact with the data received and sent by the web application + ## Examples - [FastAPI](examples/fastapi) diff --git a/src/lib.rs b/src/lib.rs index 15efd5f..b4fb324 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -310,7 +310,7 @@ impl Adapter { let compression = self.compression; let invoke_mode = self.invoke_mode; - if let Ok(runtime_proxy) = env::var("AWS_LWA_PROXY_LAMBDA_RUNTIME_API") { + if let Ok(runtime_proxy) = env::var("AWS_LWA_LAMBDA_RUNTIME_API_PROXY") { // overwrite the env variable since // LambdaInvokeMode::Buffered => lambda_http::run(svc).await, // calls the lambda lambda_runtime::run which doesn't allow to change the client URI