Skip to content

Commit 9a96644

Browse files
authored
feat: add AWS_LWA_PROXY_LAMBDA_RUNTIME_API to overwrite (#588)
* feat: add AWS_LWA_PROXY_LAMBDA_RUNTIME_API to overwrite AWS_LAMBDA_RUNTIME_API and use a proxy * rename runtime proxy env variable to AWS_LWA_LAMBDA_RUNTIME_API_PROXY and add detailed documentation
1 parent 486448d commit 9a96644

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ The readiness check port/path and traffic port can be configured using environme
106106
| AWS_LWA_PASS_THROUGH_PATH | the path for receiving event payloads that are passed through from non-http triggers | "/events" |
107107
| AWS_LWA_AUTHORIZATION_SOURCE | a header name to be replaced to `Authorization` | None |
108108
| 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 |
109+
| AWS_LWA_LAMBDA_RUNTIME_API_PROXY | overwrites `AWS_LAMBDA_RUNTIME_API` to allow proxying request (not affecting registration) | None |
109110

110111
> **Note:**
111112
> 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.
@@ -172,6 +173,10 @@ Please note that `sam local` starts a Lambda Runtime Interface Emulator on port
172173

173174
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.
174175

176+
## Intercepting request and response
177+
178+
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
179+
175180
## Examples
176181

177182
- [FastAPI](examples/fastapi)

src/lib.rs

+7
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,13 @@ impl Adapter<HttpConnector, Body> {
310310
let compression = self.compression;
311311
let invoke_mode = self.invoke_mode;
312312

313+
if let Ok(runtime_proxy) = env::var("AWS_LWA_LAMBDA_RUNTIME_API_PROXY") {
314+
// overwrite the env variable since
315+
// LambdaInvokeMode::Buffered => lambda_http::run(svc).await,
316+
// calls the lambda lambda_runtime::run which doesn't allow to change the client URI
317+
env::set_var("AWS_LAMBDA_RUNTIME_API", runtime_proxy);
318+
}
319+
313320
if compression {
314321
let svc = ServiceBuilder::new().layer(CompressionLayer::new()).service(self);
315322
match invoke_mode {

0 commit comments

Comments
 (0)