Skip to content

Commit 1652cf6

Browse files
committed
Setup multi zone aws-acm module
1 parent 669f63e commit 1652cf6

File tree

4 files changed

+112
-0
lines changed

4 files changed

+112
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Terraform AWS Certificate Manager (ACM) with Multi Zone Module
2+
3+
Terraform module to create an ACM resource that contains domains from multiple Route53 hosted zone.
4+
ACM validation is using Route53 domain.
5+
This module supports terraform version 0.12 only.
6+

main.tf

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
}

outputs.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
output "certificate_arn" {
2+
description = "The ARN of the certificate"
3+
value = aws_acm_certificate.self.arn
4+
}
5+
6+
output "certificate_domains" {
7+
description = "List of domain names covered by the certificate"
8+
value = concat([aws_acm_certificate.self.domain_name], aws_acm_certificate.self.subject_alternative_names)
9+
}
10+
11+
output "certificate_domain_validation_options" {
12+
description = "A list of attributes to feed into other resources to complete certificate validation"
13+
value = flatten(aws_acm_certificate.self[*].domain_validation_options)
14+
}

variables.tf

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
variable "domains" {
2+
description = "List of map of string containing domain name for the certificate and its corresponding hosted zone name"
3+
type = list(map(string))
4+
}
5+
6+
variable "tags" {
7+
description = "Key and value pair that will be added as tag"
8+
type = map(string)
9+
default = {}
10+
}
11+
12+
variable "validate_certificate" {
13+
description = "Whether to validate certificate"
14+
type = bool
15+
default = true
16+
}
17+
18+
variable "validation_set_records" {
19+
description = "Whether to configure Route53 records for validation"
20+
type = bool
21+
default = true
22+
}
23+
24+
variable "validation_allow_overwrite_records" {
25+
description = "Whether to allow overwrite of Route53 records"
26+
type = bool
27+
default = true
28+
}

0 commit comments

Comments
 (0)