Skip to content

Commit 595804f

Browse files
committed
Added ability to redirect to a host that isn't the cf distribution
1 parent 514b440 commit 595804f

File tree

3 files changed

+7
-7
lines changed

3 files changed

+7
-7
lines changed

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ exports.handler = (event, context, callback) => {
22
const request = event.Records[0].cf.request;
33
const hostname = request.headers.host[0].value;
44

5-
if ("${primary_hostname}" && hostname === "${primary_hostname}") {
5+
if (hostname === "${redirect_to}") {
66
return callback(null, request);
77
}
88

@@ -12,7 +12,7 @@ exports.handler = (event, context, callback) => {
1212
headers: {
1313
location: [{
1414
key: 'Location',
15-
value: "https://${primary_hostname}" + request.uri + (request.querystring === "" ? "" : "?" + request.querystring),
15+
value: "https://${redirect_to}" + request.uri + (request.querystring === "" ? "" : "?" + request.querystring),
1616
}],
1717
},
1818
}

lambda.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ data template_file lambda {
33
template = "${file("${path.module}/index.js")}"
44

55
vars = {
6-
primary_hostname = var.redirect_all ? "" : var.cloudfront_distribution
6+
redirect_to = var.redirect_to ? var.redirect_to : var.cloudfront_distribution
77
}
88
}
99

variables.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ variable lambda_function_name {
5858
description = "The name of the Lambda@Edge function for redirecting to the primary hostname"
5959
}
6060

61-
variable redirect_all {
62-
type = bool
63-
default = false
64-
description = "Whether all requests should be redirected, or just ones which don't match the CloudFront distribution primary host"
61+
variable redirect_to {
62+
type = string
63+
default = ""
64+
description = "Where to redirect requests to, if not set the cloudfront distribution host will be used"
6565
}

0 commit comments

Comments
 (0)