Skip to content

Instantly share code, notes, and snippets.

@jeroenvollenbrock
Last active April 12, 2024 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeroenvollenbrock/8a4ed5d6fbec7782aaa794a6328581f2 to your computer and use it in GitHub Desktop.
Save jeroenvollenbrock/8a4ed5d6fbec7782aaa794a6328581f2 to your computer and use it in GitHub Desktop.
AWS Proxy Lambda
const TARGET = process.env.TARGET_BASE_URL;
export const EXCLUDED_HEADERS = [
'accept-encoding',
'connection',
'content-encoding',
'content-length',
'host',
'server',
'transfer-encoding',
'x-amzn-cipher-suite',
'x-amzn-tls-version',
'x-amzn-trace-id',
'x-amzn-vpc-id',
'x-amzn-vpce-config',
'x-amzn-vpce-id',
'x-apigw-api-id',
'x-forwarded-for',
'vary',
];
export const handler = async (event) => {
const method = event.httpMethod ?? event.requestContext.http.method;
const path = (event.rawPath ?? event.path);
const url = new URL(`${TARGET}${path}`);
if(event.queryStringParameters) {
for(const [key, value] of Object.entries(event.queryStringParameters)) {
url.searchParams.append(key, value);
}
}
const body = event.isBase64Encoded ? Buffer.from(event.body, 'base64') : event.body;
const headers = event.headers;
for (const header of EXCLUDED_HEADERS) {
delete headers[header];
}
const result = await fetch(url, {
method,
headers,
body: method !== 'GET' ? body : undefined,
});
const responseHeaders = {};
for (const [key, value] of result.headers) {
if(!EXCLUDED_HEADERS.includes(key.toLowerCase())) {
responseHeaders[key] = value;
}
}
const response = await result.text();
return {
statusCode: result.status,
headers: responseHeaders,
body: response,
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment