Skip to content

Commit bc151d8

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

File tree

4 files changed

+13
-12
lines changed

4 files changed

+13
-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: 6 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 {
@@ -108,10 +108,11 @@ resource "aws_cloudfront_vpc_origin" "this" {
108108
tags = local.tags
109109

110110
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.
111+
# Some 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 origin is updated
113+
# properly.
113114
create_before_destroy = true
114-
replace_triggered_by = [terraform_data.prefix]
115+
replace_triggered_by = [terraform_data.prefix, var.origin_alb_arn]
115116
}
116117
}
117118

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)