Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/lambda origins #1

Merged
merged 6 commits into from
Aug 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified dist/lambda-gateway.zip
Binary file not shown.
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@
"license": "BSD-3",
"private": true,
"scripts": {
"build": "webpack; zip -j dist/lambda-gateway.zip dist/*; rm dist/index.js"
"build": "rm -rf dist; mkdir -p dist; webpack; zip -j dist/lambda-gateway.zip dist/*; rm dist/index.js"
},
"dependencies": {
"aws-sdk": "^2.1117.0"
"aws-sdk": "^2.1153.0"
},
"devDependencies": {
"@types/aws-lambda": "^8.10.94",
"@types/aws-lambda": "^8.10.100",
"@types/node": "^17.0.25",
"fork-ts-checker-webpack-plugin": "^7.2.6",
"ts-loader": "^9.2.6",
"ts-node": "^10.7.0",
"typescript": "^4.6.3",
"webpack": "^5.61.0",
"webpack-cli": "^4.9.1"
"fork-ts-checker-webpack-plugin": "^7.2.11",
"ts-loader": "^9.3.0",
"ts-node": "^10.8.1",
"typescript": "^4.7.3",
"webpack": "^5.73.0",
"webpack-cli": "^4.10.0"
}
}
52 changes: 39 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import path from 'path';
import { urlToHttpOptions } from 'url';
import { CloudFrontRequestEvent, CloudFrontRequestResult } from 'aws-lambda';

function log(message: string, context: any) {
console.log(
JSON.stringify({
message,
context,
})
);
}

export async function handler(event: CloudFrontRequestEvent): Promise<CloudFrontRequestResult> {
const log = (message: string, context: any) => {
console.info(
JSON.stringify({
message,
context,
})
);
};

try {
const { config, request } = event.Records[0].cf;
if (config.eventType.toLowerCase() !== 'origin-request') {
Expand All @@ -25,13 +26,38 @@ export async function handler(event: CloudFrontRequestEvent): Promise<CloudFront
const baseHost = s3.customHeaders['x-base-host'][0].value;
const hostDiff = targetHost.length - baseHost.length;
const subDomain = hostDiff > 0 ? targetHost.substring(0, hostDiff - 1) : 'www';
const s3Path = path.join(s3.path, subDomain);
const s3Path = path.join(s3.path, subDomain).replace(/\/$/i, '');

log('Redirecting request to origin path', { s3Path, request });
const customHost =
s3.customHeaders[`x-origin-${subDomain.toLowerCase()}`]?.[0]?.value ??
s3.customHeaders[`X-Origin-${subDomain.toUpperCase()}`]?.[0]?.value ??
null;
if (!!customHost) {
const opts = urlToHttpOptions(new URL(customHost));
log('Redirecting request to custom origin', { s3Path, request, opts });
Reflect.deleteProperty(request.origin, 's3');
const uri = opts.path.split('/').pop();
request.origin.custom = {
path: opts.path.split('/').reverse().splice(1).reverse().join('/'),
readTimeout: 30,
keepaliveTimeout: 5,
domainName: opts.hostname,
customHeaders: s3.customHeaders,
port: parseInt(`${opts.port ?? '443'}`),
sslProtocols: ['TLSv1', 'TLSv1.1', 'TLSv1.2'],
protocol: 'https',
};
request.uri = `/${uri}`;
request.headers['host'][0].value = opts.hostname;
} else {
log('Redirecting request to S3 origin path', { s3Path, request });

request.origin.s3.path = s3Path;
request.headers['host'][0].value = request.origin.s3.domainName;
}

request.origin.s3.path = s3Path;
request.headers['host'][0].value = request.origin.s3.domainName;
request.headers['x-target-domain'] = [{ value: targetHost }];
log('Responding with modified request', { request });

return request;
} catch (e) {
Expand Down
Loading