Ircama / aws-lambda-reverse-proxy

A simple Python Reverse Proxy using AWS Lambda
21 stars 8 forks source link
aws lambda proxy python reverse-proxy

aws-lambda-reverse-proxy

A simple Python Reverse Proxy using AWS Lambda

Description

This program and related setup implements a simple http based request/response reverse proxy application exposing to internet a default AWS auto-generated public fully functional https endpoint that automatically uses the lambda Function URL certificate or the Amazon API Gateway certificate (or a custom one).

This reverse proxy application is able to inspect the received https request from the client (web browser) and to forward it to an http or https internet backend; in turn, when receiving the response from the backend, it is delivered to the client on the internet (e.g., web browser), which is unaware of the backend service IP address and related protocol, that can be HTTP or https with self-signed SSL certificate, using the same port or a different one.

The http API endpoint includes a valid AWS certificate and allows HTTPS.

Features:

Limitations:

Tested architecture

Tested to integrate an always-free Oracle Cloud OCI computing resource.

flowchart TD;

Browser(Web browser)

Rproxy["AWS Reverse Proxy, exposing a fixed FQDN
over HTTPS with valid SSL certificate
provided by the Amazon Root CA, port 443"]

OCI["Oracle Cloud Compute instance, exposing
a public IP address on a different port,
with self-signed certificate and no FQDN"]

Browser <==>|internet| Rproxy <==>|internet| OCI

Setup the needed AWS resources

Create a Python-based AWS Lambda function

AWS Lambda is a serverless computing service included in the free tier of Amazon Web Services (AWS), including one million free requests per month and 400000 GB-seconds of compute time per month.

Option 1 - Add a "Function URL" to the AWS Lambda function

The configuration of a Function URL is the preferred method, as does not imply the introduction of the fixed "HTTP API Gateway" timeout of 30 secs. Option 2 seems to better change long URLs. Both option can be concurrently active.

Option 2 - Link an HTTP API Gateway to trigger the AWS Lambda function

As alternative option to the usage of a "Function URL", an "HTTP API Gateway" can be created.

Amazon HTTP API Gateway provides a public HTTPS endpoint to the AWS Lambda function and automatically assigns a domain to the API, with a FQDN that uses a valid Amazon API Gateway certificate. It does not generate costs in case of limited number of small-sized requests per month (e.g., 4000 requests per month, with 512 KB each).

Replace the default Python code with the reverse proxy

Test the configuration

In case of error, run /usr/local/bin/aws logs tail /aws/lambda/rproxy --follow and see the logs.

Installation of awscli

Installation of awscli on Unix (or WSL) to trace the AWS Lambda function:

# Install awscli:
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
/usr/local/bin/aws --version

# Configure awscli:
/usr/local/bin/aws configure

Tracing

(After installing awscli)

/usr/local/bin/aws logs tail /aws/lambda/<function name> --follow

Needed environment variables

REMOTE_URL: remote URL of the backend application (http or https). Example: https://httpbin.org

FILTERED_PATH: (optional) path that needs to be filtered. A list of paths can be used, by separating each path with a pipe character (|). Example: /foobar|/baz.

NO_HEADERS: (optional) when existing and set, do not forward headers.

Optional parameters:

Special parameters