๐ชStatic website deployment preview with S3 and CloudFront
How to create deployment previews with S3 and CloudFront
PreviousCloud Run Deployment Preview Github Actions workflowsNextAllow Docker Remote API access over Tailscale network
Last updated
How to create deployment previews with S3 and CloudFront
Last updated
// deployment-preview-proxy-function
'use strict';
exports.handler = (event, context, callback) => {
// Get request and request headers, uri
const request = event.Records[0].cf.request;
const headers = request.headers;
const uri = request.uri;
const host = headers.host[0].value;
// Configure authentication
const authUser = 'admin';
const authPass = 'minda';
// Configure deployment preview subdomain
const subdomain = 'preview';
let sprintUri = '';
let hostArray = host.split('.');
// Check whether the subdomain is match
if (host.match(subdomain) && (hostArray[0] != subdomain)) {
sprintUri = hostArray[0] + '/';
}
// Check whether the URI is missing a file name.
if (uri.endsWith('/')) {
request.uri += `${sprintUri}index.html`;
}
// Check whether the URI is missing a file extension.
else if (!uri.includes('.')) {
request.uri += `/${sprintUri}index.html`;
}
// Construct the Basic Auth string
const authString = 'Basic ' + new Buffer(authUser + ':' + authPass).toString('base64');
// Require Basic authentication
if (typeof headers.authorization == 'undefined' || headers.authorization[0].value != authString) {
const body = 'Unauthorized';
const response = {
status: '401',
statusDescription: 'Unauthorized',
body: body,
headers: {
'www-authenticate': [{key: 'WWW-Authenticate', value:'Basic'}]
},
};
callback(null, response);
}
// Continue request processing if authentication passed
callback(null, request);
};