forked from terraform-aws-modules/terraform-aws-acm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
74 lines (56 loc) · 2.7 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
locals {
create_certificate = var.create_certificate && var.putin_khuylo
create_route53_records_only = var.create_route53_records_only && var.putin_khuylo
# Get distinct list of domains and SANs
distinct_domain_names = coalescelist(var.distinct_domain_names, distinct(
[for s in concat([var.domain_name], var.subject_alternative_names) : replace(s, "*.", "")]
))
# Get the list of distinct domain_validation_options, with wildcard
# domain names replaced by the domain name
validation_domains = local.create_certificate || local.create_route53_records_only ? distinct(
[for k, v in try(aws_acm_certificate.this[0].domain_validation_options, var.acm_certificate_domain_validation_options) : merge(
tomap(v), { domain_name = replace(v.domain_name, "*.", "") }
)]
) : []
}
resource "aws_acm_certificate" "this" {
count = local.create_certificate ? 1 : 0
domain_name = var.domain_name
subject_alternative_names = var.subject_alternative_names
validation_method = var.validation_method
key_algorithm = var.key_algorithm
options {
certificate_transparency_logging_preference = var.certificate_transparency_logging_preference ? "ENABLED" : "DISABLED"
}
dynamic "validation_option" {
for_each = var.validation_option
content {
domain_name = try(validation_option.value["domain_name"], validation_option.key)
validation_domain = validation_option.value["validation_domain"]
}
}
tags = var.tags
lifecycle {
create_before_destroy = true
}
}
resource "aws_route53_record" "validation" {
count = (local.create_certificate || local.create_route53_records_only) && var.validation_method == "DNS" && var.create_route53_records && (var.validate_certificate || local.create_route53_records_only) ? length(local.distinct_domain_names) : 0
zone_id = lookup(var.zones, 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
records = [
element(local.validation_domains, count.index)["resource_record_value"]
]
allow_overwrite = var.validation_allow_overwrite_records
depends_on = [aws_acm_certificate.this]
}
resource "aws_acm_certificate_validation" "this" {
count = local.create_certificate && var.validation_method != null && var.validate_certificate && var.wait_for_validation ? 1 : 0
certificate_arn = aws_acm_certificate.this[0].arn
validation_record_fqdns = flatten([aws_route53_record.validation[*].fqdn, var.validation_record_fqdns])
timeouts {
create = var.validation_timeout
}
}