Skip to content
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
8 changes: 8 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@ module "playful-web" {
domain = var.playful_web_domain
host = var.playful_web_host
noindex = var.env != "prod"

subdomain_redirects = {
www = {
location = "https://${var.playful_web_domain}"
preserve_url = true
status = 301
}
}
}
104 changes: 97 additions & 7 deletions modules/playful-web/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,31 @@ variable "noindex" {
description = "Whether a noindex header should be appended to every response"
}

variable "subdomain_redirects" {
type = map(object({
location = string
preserve_url = bool
status = number
}))
description = "Map of subdomain to its redirect configuration"
default = {}

validation {
condition = alltrue([
for redirect in values(var.subdomain_redirects) :
contains([301, 302], redirect.status)
])
error_message = "Redirect status must be either 301 or 302."
}
}

locals {
subdomain_redirect_hosts = {
for subdomain, redirect in var.subdomain_redirects :
"${subdomain}.${var.domain}" => redirect
}
}

resource "fastly_service_vcl" "cdn" {
activate = true
comment = "Managed by Tofu"
Expand All @@ -24,6 +49,13 @@ resource "fastly_service_vcl" "cdn" {
name = var.domain
}

dynamic "domain" {
for_each = local.subdomain_redirect_hosts
content {
name = domain.key
}
}

backend {
address = var.host
name = "Host 1"
Expand Down Expand Up @@ -136,6 +168,63 @@ resource "fastly_service_vcl" "cdn" {
name = "Generated by force TLS and enable HSTS"
timer_support = false
}

dynamic "snippet" {
for_each = length(local.subdomain_redirect_hosts) > 0 ? [1] : []
content {
name = "Redirects (tables)"
type = "init"
priority = 100
content = join("\n", concat(
["table redirect_locations STRING {"],
[for host, redirect in local.subdomain_redirect_hosts : " ${jsonencode(host)}: ${jsonencode(redirect.location)},"],
["}", "", "table redirect_statuses INTEGER {"],
[for host, redirect in local.subdomain_redirect_hosts : " ${jsonencode(host)}: ${redirect.status},"],
["}", "", "table redirect_preserve_urls BOOL {"],
[for host, redirect in local.subdomain_redirect_hosts : " ${jsonencode(host)}: ${redirect.preserve_url},"],
["}"],
))
}
}

dynamic "snippet" {
for_each = length(local.subdomain_redirect_hosts) > 0 ? [1] : []
content {
name = "Redirects (recv)"
type = "recv"
priority = 100
content = <<-VCL
if (table.contains(redirect_locations, std.tolower(req.http.host))) {
error 618 "redirect";
}
VCL
}
}

dynamic "snippet" {
for_each = length(local.subdomain_redirect_hosts) > 0 ? [1] : []
content {
name = "Redirects (error)"
type = "error"
priority = 100
content = <<-VCL
if (obj.status == 618 && obj.response == "redirect") {
set obj.status = table.lookup_integer(redirect_statuses, std.tolower(req.http.host), 302);
if (obj.status == 301) {
set obj.response = "Moved Permanently";
} else {
set obj.response = "Found";
}
set obj.http.Location = table.lookup(redirect_locations, std.tolower(req.http.host), "");
if (table.lookup_bool(redirect_preserve_urls, std.tolower(req.http.host), false)) {
set obj.http.Location = obj.http.Location + req.url;
}
synthetic "";
return (deliver);
}
VCL
}
}
}

resource "fastly_tls_subscription" "main" {
Expand Down Expand Up @@ -187,11 +276,12 @@ resource "porkbun_dns_record" "apex" {
ttl = 600
}

resource "porkbun_url_forward" "redirect" {
domain = var.domain
subdomain = "www"
include_path = true
location = "https://${var.domain}"
type = "permanent"
wildcard = false
resource "porkbun_dns_record" "subdomain_redirect" {
for_each = var.subdomain_redirects

domain = var.domain
subdomain = each.key
type = "CNAME"
content = one([for record in data.fastly_tls_configuration.default_tls.dns_records : record.record_value if record.record_type == "CNAME"])
ttl = 600
}
Loading