Skip to content

Instantly share code, notes, and snippets.

@jenspeveling
Last active June 10, 2020 14:31
Show Gist options
  • Save jenspeveling/c892da691d1898e1a32519cb3f797ad2 to your computer and use it in GitHub Desktop.
Save jenspeveling/c892da691d1898e1a32519cb3f797ad2 to your computer and use it in GitHub Desktop.
'use strict';
/**
* this is useful if you want to use S3 endpoint over S3 Webserver Endpoint for Cloudfront Origin because
* OAI is not possible with S3 Webserver Origin (You can restrict access by using secrets in headers but
* you'd have to leave the bucket public - this might be against company policy or does not fit your security
* architecture)
*
Downside if using S3 endpoint origin is lack of support for paths in URI. e.g
* www.example.com/path/ will return empty object. What we actually want is to
* serve the index..html existing in the origin subdirectory. You'd have to type expplicitly
* www.example.com/path/index.html. It is ugly and there's still the issue with the empty download
*
reason for this behaviour is that CF uses S3 Object API instead of treating S3 as a Webserver.
* configure this lambda to intercept the request from CF to S3 object API in
* the behaviour configuration for the path.
* uses S3 object API
* adds "index.html" to the end of a uri with a path
* Extract request from a CloudFront event that is
* sent to Lambda@Edge and replaces
*/
exports.handler = (event, context, callback) => {
var request = event.Records[0].cf.request;
var olduri = request.uri;
var newuri = olduri.replace(/\/$/, '\/index.html');
request.uri = newuri;
return callback(null, request);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment