A Rust-based AWS Lambda function that proxies REST API requests.
This repository contains an AWS Lambda function written in Rust that acts as a proxy for REST APIs. The function forwards incoming HTTP requests to a specified target host. The target host can be set either via a header (X-Target-Host) or an environment variable (TARGET_HOST). The function also checks for an API key in the header (X-API-Key) to authorize requests and prevents recursive calls by ensuring the target host is not the same as the source host.
- Authorization: Checks for an
X-API-Keyheader and compares it with theAPI_KEYenvironment variable. If the API keys do not match, it returns a401 Unauthorizedresponse. - Target Host Configuration: The target host for forwarding the request can be specified via the
X-Target-Hostheader or theTARGET_HOSTenvironment variable as host:port (localhost:1234or without portexample.com). - HTTP Method Support: Supports various HTTP methods (
GET,POST,PUT,DELETE) and returns the response from the target host back to the client.
src/main.rs: The main Rust source file containing the Lambda function logic.Cargo.toml: The Cargo configuration file specifying dependencies and project metadata.tests: Contains unit tests for the Lambda function.
Build
cargo build --release --target aarch64-unknown-linux-musl && sam buildDeploy
sam deploy --guidedSometimes after deployment, the endpoint may return {"Message":"Forbidden"}. This is a known AWS issue. To resolve it, go to the AWS Management Console, navigate to the Lambda function's configuration, find the Function URL settings, and resave the configuration by clicking "Edit" and then "Save".
Contributions are welcome! Please feel free to submit a pull request or open an issue to discuss changes.
This project is licensed under the MIT License. See the LICENSE file for details.