forked from terraform-aws-modules/terraform-aws-acm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
46 lines (33 loc) · 1.6 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
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, "*.", "")]))
// 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, v.domain_name)] : []
}
resource "aws_acm_certificate" "this" {
count = var.create_certificate ? 1 : 0
domain_name = var.domain_name
subject_alternative_names = var.subject_alternative_names
validation_method = var.validation_method
tags = var.tags
lifecycle {
create_before_destroy = true
}
}
resource "aws_route53_record" "validation" {
count = var.create_certificate && var.validation_method == "DNS" && var.validate_certificate ? length(local.distinct_domain_names) : 0
zone_id = var.zone_id
name = element(local.validation_domains, count.index)["resource_record_name"]
type = element(local.validation_domains, count.index)["resource_record_type"]
ttl = 60
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 = var.create_certificate && var.validation_method == "DNS" && var.validate_certificate && var.wait_for_validation ? 1 : 0
certificate_arn = aws_acm_certificate.this[0].arn
validation_record_fqdns = aws_route53_record.validation.*.fqdn
}