|
| 1 | +locals { |
| 2 | + all_domains = [ |
| 3 | + for v in var.domains: v.domain |
| 4 | + ] |
| 5 | + all_zones = [ |
| 6 | + for v in var.domains: v.zone |
| 7 | + ] |
| 8 | + distinct_zones = distinct([ |
| 9 | + for v in var.domains: v.zone |
| 10 | + ]) |
| 11 | + distinct_domains = distinct([ |
| 12 | + for domain in local.all_domains: replace(domain, "*.", "") |
| 13 | + ]) |
| 14 | + zone_name_to_id_map = zipmap(local.distinct_zones, data.aws_route53_zone.self[*].zone_id) |
| 15 | + domain_to_zone_map = zipmap(local.all_domains, local.all_zones) |
| 16 | + |
| 17 | + cert_domain_name = sort(local.all_domains)[0] |
| 18 | + cert_san = slice(sort(local.all_domains), 1, length(local.all_domains)) |
| 19 | + cert_validation_domains = [ |
| 20 | + for v in aws_acm_certificate.self.domain_validation_options: tomap(v) if contains(local.distinct_domains, replace(v.domain_name, "*.", "")) |
| 21 | + ] |
| 22 | +} |
| 23 | + |
| 24 | +data "aws_route53_zone" "self" { |
| 25 | + count = length(local.distinct_zones) |
| 26 | + |
| 27 | + name = local.distinct_zones[count.index] |
| 28 | + private_zone = false |
| 29 | +} |
| 30 | + |
| 31 | +resource "aws_acm_certificate" "self" { |
| 32 | + domain_name = local.cert_domain_name |
| 33 | + subject_alternative_names = local.cert_san |
| 34 | + validation_method = "DNS" |
| 35 | + |
| 36 | + tags = var.tags |
| 37 | + |
| 38 | + lifecycle { |
| 39 | + create_before_destroy = true |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +resource "aws_route53_record" "validation" { |
| 44 | + count = var.validation_set_records ? length(local.distinct_domains) : 0 |
| 45 | + |
| 46 | + zone_id = lookup(local.zone_name_to_id_map, lookup(local.domain_to_zone_map, local.cert_validation_domains[count.index]["domain_name"])) |
| 47 | + name = local.cert_validation_domains[count.index]["resource_record_name"] |
| 48 | + type = local.cert_validation_domains[count.index]["resource_record_type"] |
| 49 | + ttl = 60 |
| 50 | + |
| 51 | + allow_overwrite = var.validation_allow_overwrite_records |
| 52 | + |
| 53 | + records = [ |
| 54 | + local.cert_validation_domains[count.index]["resource_record_value"] |
| 55 | + ] |
| 56 | +} |
| 57 | + |
| 58 | +resource "aws_acm_certificate_validation" "self" { |
| 59 | + count = var.validate_certificate ? 1 : 0 |
| 60 | + |
| 61 | + certificate_arn = aws_acm_certificate.self.arn |
| 62 | + |
| 63 | + validation_record_fqdns = local.cert_validation_domains[*]["resource_record_name"] |
| 64 | +} |
0 commit comments