Skip to content

Commit 627b0eb

Browse files
committed
WIP: Multiple HostedZone support
* Lookup hosted zone_id of distinct_domains * Ignore wildcard validation records
1 parent 3ceb5d2 commit 627b0eb

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

main.tf

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
locals {
22
# Get distinct list of domains and SANs
3-
distinct_domain_names = distinct(concat([var.domain_name], [for s in var.subject_alternative_names : replace(s, "*.", "")]))
3+
distinct_domain_names = distinct(concat([replace(var.domain_name, "*.", "")], [for s in var.subject_alternative_names : replace(s, "*.", "")]))
44

55
# Copy domain_validation_options for the distinct domain names
6-
validation_domains = var.create_certificate ? [for k, v in aws_acm_certificate.this[0].domain_validation_options : tomap(v) if contains(local.distinct_domain_names, replace(v.domain_name, "*.", ""))] : []
6+
validation_domains = var.create_certificate ? [for k, v in aws_acm_certificate.this[0].domain_validation_options : tomap(v) if contains(local.distinct_domain_names, replace(v.domain_name, "\\*\\.", ""))] : []
7+
8+
host_to_zone_regex = "/^(?:.*\\.)?([^.]+\\.[^.]+)$/"
9+
zone_id_map = zipmap(local.distinct_domain_names, data.aws_route53_zone.this.*.zone_id)
10+
}
11+
12+
data "aws_route53_zone" "this" {
13+
count = length(local.distinct_domain_names)
14+
15+
name = replace(local.distinct_domain_names[count.index], local.host_to_zone_regex, "$1")
16+
private_zone = false
717
}
818

919
resource "aws_acm_certificate" "this" {
@@ -25,9 +35,9 @@ resource "aws_acm_certificate" "this" {
2535
}
2636

2737
resource "aws_route53_record" "validation" {
28-
count = var.create_certificate && var.validation_method == "DNS" && var.validate_certificate ? length(local.distinct_domain_names) + 1 : 0
38+
count = var.create_certificate && var.validation_method == "DNS" && var.validate_certificate ? length(local.distinct_domain_names) : 0
2939

30-
zone_id = var.zone_id
40+
zone_id = lookup(local.zone_id_map, element(local.validation_domains, count.index)["domain_name"], var.zone_id)
3141
name = element(local.validation_domains, count.index)["resource_record_name"]
3242
type = element(local.validation_domains, count.index)["resource_record_type"]
3343
ttl = var.dns_ttl

outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,8 @@ output "validation_domains" {
2727
description = "List of distinct domain validation options. This is useful if subject alternative names contain wildcards."
2828
value = local.validation_domains
2929
}
30+
31+
output "zone_id_map" {
32+
description = "List of distinct domains to hosted zone id."
33+
value = local.zone_id_map
34+
}

0 commit comments

Comments
 (0)