Skip to content

Commit 7f2b723

Browse files
committed
feat: Support setting no subdomain.
1 parent 0306522 commit 7f2b723

File tree

4 files changed

+16
-12
lines changed

4 files changed

+16
-12
lines changed

dns.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
resource "aws_route53_record" "subdomain" {
22
zone_id = data.aws_route53_zone.domain.zone_id
3-
name = "${local.subdomain}.${var.domain}"
3+
name = local.fqdn
44
type = "A"
55

66
alias {
@@ -13,7 +13,7 @@ resource "aws_route53_record" "subdomain" {
1313
resource "aws_acm_certificate" "subdomain" {
1414
# Specify the name rather than referencing the resource directly. This allows
1515
# us to create the certificate before the DNS record exists.
16-
domain_name = "${local.subdomain}.${var.domain}"
16+
domain_name = local.fqdn
1717
validation_method = "DNS"
1818

1919
lifecycle {

locals.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
locals {
2-
fqdn = "${local.subdomain}.${var.domain}"
3-
subdomain = var.subdomain == "" ? var.environment : var.subdomain
2+
fqdn = join(".", compact([local.subdomain, var.domain]))
3+
subdomain = var.subdomain != null ? var.subdomain : var.environment
44
# If an origin ALB ARN is provided, use its DNS name; otherwise, use the
55
# provided origin domain or construct one.
66
origin_domain = (var.origin_alb_arn != null
77
? data.aws_lb.origin["this"].dns_name
8-
: (var.origin_domain != "" ? var.origin_domain : join(".", ["origin", local.subdomain, var.domain]))
8+
: (var.origin_domain != "" ? var.origin_domain : join(".", compact(["origin", local.subdomain, var.domain])))
99
)
1010
prefix = "${var.project}-${var.environment}"
11-
tags = merge(var.tags, { domain : "${local.subdomain}.${var.domain}" })
11+
tags = merge(var.tags, { domain : local.fqdn })
1212
}

main.tf

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ resource "aws_cloudfront_distribution" "waf" {
22
enabled = true
33
comment = "Pass traffic through WAF before sending to the origin."
44
is_ipv6_enabled = true
5-
aliases = ["${local.subdomain}.${var.domain}"]
5+
aliases = [local.fqdn]
66
price_class = "PriceClass_100"
77
web_acl_id = aws_wafv2_web_acl.waf.arn
88

@@ -50,7 +50,7 @@ resource "aws_cloudfront_distribution" "waf" {
5050
logging_config {
5151
include_cookies = false
5252
bucket = var.log_bucket
53-
prefix = "cloudfront/${local.subdomain}.${var.domain}"
53+
prefix = "cloudfront/${local.fqdn}"
5454
}
5555

5656
default_cache_behavior {
@@ -88,6 +88,9 @@ resource "aws_cloudfront_distribution" "waf" {
8888
resource "terraform_data" "prefix" {
8989
input = local.prefix
9090
}
91+
resource "terraform_data" "origin_alb" {
92+
input = var.origin_alb_arn
93+
}
9194

9295
resource "aws_cloudfront_vpc_origin" "this" {
9396
for_each = var.origin_alb_arn != null ? toset(["this"]) : toset([])
@@ -108,10 +111,11 @@ resource "aws_cloudfront_vpc_origin" "this" {
108111
tags = local.tags
109112

110113
lifecycle {
111-
# Name changes don't force a replacement, but will fail if the origin is in
112-
# use. We want to force a replacement so that the name is updated properly.
114+
# Some changes don't force a replacement, but will fail if the origin is in
115+
# use. We want to force a replacement so that the origin is updated
116+
# properly.
113117
create_before_destroy = true
114-
replace_triggered_by = [terraform_data.prefix]
118+
replace_triggered_by = [terraform_data.prefix, terraform_data.origin_alb]
115119
}
116120
}
117121

variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ variable "request_policy" {
105105
variable "subdomain" {
106106
type = string
107107
description = "Subdomain for the distribution. Defaults to the environment."
108-
default = ""
108+
default = null
109109
}
110110

111111
variable "tags" {

0 commit comments

Comments
 (0)