diff --git a/main.tf b/main.tf index e21241f..de837b5 100644 --- a/main.tf +++ b/main.tf @@ -1,9 +1,19 @@ locals { # Get distinct list of domains and SANs - distinct_domain_names = distinct(concat([var.domain_name], [for s in var.subject_alternative_names : replace(s, "*.", "")])) + distinct_domain_names = distinct(concat([replace(var.domain_name, "*.", "")], [for s in var.subject_alternative_names : replace(s, "*.", "")])) # Copy domain_validation_options for the distinct domain names - 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, "*.", ""))] : [] + 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, "\\*\\.", ""))] : [] + + host_to_zone_regex = "/^(?:.*\\.)?([^.]+\\.[^.]+)$/" + zone_id_map = zipmap(local.distinct_domain_names, data.aws_route53_zone.this.*.zone_id) +} + +data "aws_route53_zone" "this" { + count = length(local.distinct_domain_names) + + name = replace(local.distinct_domain_names[count.index], local.host_to_zone_regex, "$1") + private_zone = false } resource "aws_acm_certificate" "this" { @@ -25,9 +35,9 @@ resource "aws_acm_certificate" "this" { } resource "aws_route53_record" "validation" { - count = var.create_certificate && var.validation_method == "DNS" && var.validate_certificate ? length(local.distinct_domain_names) + 1 : 0 + count = var.create_certificate && var.validation_method == "DNS" && var.validate_certificate ? length(local.distinct_domain_names) : 0 - zone_id = var.zone_id + zone_id = lookup(local.zone_id_map, element(local.validation_domains, count.index)["domain_name"], var.zone_id) name = element(local.validation_domains, count.index)["resource_record_name"] type = element(local.validation_domains, count.index)["resource_record_type"] ttl = var.dns_ttl diff --git a/outputs.tf b/outputs.tf index 192f516..8092cf2 100644 --- a/outputs.tf +++ b/outputs.tf @@ -27,3 +27,8 @@ output "validation_domains" { description = "List of distinct domain validation options. This is useful if subject alternative names contain wildcards." value = local.validation_domains } + +output "zone_id_map" { + description = "List of distinct domains to hosted zone id." + value = local.zone_id_map +} \ No newline at end of file