'use strict';
exports.handler = (event, context, callback) => {
var rawJson = JSON.stringify(event);
var searchMask = '"host"';
var regEx = new RegExp(searchMask, "ig");
var replaceMask = '"Host"';
rawJson = rawJson.replace(regEx, replaceMask);
event = JSON.parse(rawJson);
const request = event.Records[0].cf.request;
var str = request.headers.Host[0].value;
if (str.startsWith("www.") && !str.endsWith('.cloudfront.net')) {
var updatedHost = str.replace("www.","");
var location = "https://" + updatedHost + request.uri;
const response = {
status: '301',
statusDescription: '301 Found',
headers: {
location: [{
key: 'Location',
value: location,
}],
}
};
console.log(JSON.stringify(response));
callback(null, response);
return;
}
callback(null, request);
};
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": [
"edgelambda.amazonaws.com",
"lambda.amazonaws.com"
]
},
"Action": "sts:AssumeRole"
}
]
}