Skip to content

Commit 7e30531

Browse files
committed
Support different AWS account between ACM and Route53
1 parent 4490764 commit 7e30531

File tree

5 files changed

+69
-37
lines changed

5 files changed

+69
-37
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,21 @@ The `domain_name` and `subject_alternative_names` variables consist of map (obje
1212
- The **domain** key contains domain name that will be used in the certificate whether in the domain name or subject alternative names section.
1313

1414
```terraform
15+
provider "aws" { }
16+
1517
module "acm" {
1618
source = "../../"
1719
20+
providers = {
21+
aws.acm = aws
22+
aws.route53 = aws
23+
}
24+
1825
domain_name = {
1926
zone = "example.com"
2027
domain = "example.com"
2128
}
29+
2230
subject_alternative_names = [
2331
{
2432
zone = "example.com"
@@ -54,6 +62,19 @@ Due to the [https://github.com/terraform-providers/terraform-provider-aws/issues
5462
1. Run `terraform plan -out=tfplan.out` and review the execution plan.
5563
1. Apply the change using `terraform apply tfplan.out`.
5664

65+
## Requirements
66+
67+
| Name | Version |
68+
|------|---------|
69+
| terraform | >= 0.12.0 |
70+
71+
## Providers
72+
73+
| Name | Version |
74+
|------|---------|
75+
| aws.acm | n/a |
76+
| aws.route53 | n/a |
77+
5778
## Inputs
5879

5980
| Name | Description | Type | Default | Required |

main.tf

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,62 +2,62 @@ locals {
22
all_domains = concat([var.domain_name.domain], [
33
for v in var.subject_alternative_names : v.domain
44
])
5+
56
all_zones = concat([var.domain_name.zone], [
67
for v in var.subject_alternative_names : v.zone
78
])
8-
distinct_zones = distinct(local.all_zones)
9-
zone_name_to_id_map = zipmap(local.distinct_zones, data.aws_route53_zone.self[*].zone_id)
10-
domain_to_zone_map = zipmap(local.all_domains, local.all_zones)
119

12-
cert_san = reverse(sort([
10+
domain_zone_mapping = zipmap(local.all_domains, local.all_zones)
11+
12+
cert_sans = sort([
1313
for v in var.subject_alternative_names : v.domain
14-
]))
15-
cert_validation_domains = [
16-
for v in aws_acm_certificate.self.domain_validation_options : tomap(v)
17-
]
14+
])
15+
16+
default_tags = {
17+
ManagedBy = "terraform"
18+
}
1819
}
1920

2021
data "aws_route53_zone" "self" {
21-
count = length(local.distinct_zones)
22+
provider = aws.route53
23+
for_each = toset(local.all_zones)
2224

23-
name = local.distinct_zones[count.index]
25+
name = each.value
2426
private_zone = false
2527
}
2628

2729
resource "aws_acm_certificate" "self" {
30+
provider = aws.acm
31+
2832
domain_name = var.domain_name.domain
29-
subject_alternative_names = local.cert_san
33+
subject_alternative_names = local.cert_sans
3034
validation_method = "DNS"
3135

32-
tags = var.tags
33-
34-
lifecycle {
35-
create_before_destroy = true
36-
# Workaround for SAN doesn't maintain order
37-
# See https://github.com/terraform-providers/terraform-provider-aws/issues/8531
38-
ignore_changes = [subject_alternative_names]
39-
}
36+
tags = merge(local.default_tags, var.tags)
4037
}
4138

4239
resource "aws_route53_record" "validation" {
43-
count = var.validation_set_records ? length(distinct(local.all_domains)) : 0
44-
45-
zone_id = lookup(local.zone_name_to_id_map, lookup(local.domain_to_zone_map, local.cert_validation_domains[count.index]["domain_name"]))
46-
name = local.cert_validation_domains[count.index]["resource_record_name"]
47-
type = local.cert_validation_domains[count.index]["resource_record_type"]
48-
ttl = 60
49-
40+
provider = aws.route53
41+
for_each = var.validation_set_records ? {
42+
for dvo in aws_acm_certificate.self.domain_validation_options : dvo.domain_name => {
43+
name = dvo.resource_record_name
44+
record = dvo.resource_record_value
45+
type = dvo.resource_record_type
46+
}
47+
} : {}
48+
49+
zone_id = data.aws_route53_zone.self[local.domain_zone_mapping[each.key]].zone_id
50+
name = each.value.name
51+
type = each.value.type
52+
records = [each.value.record]
53+
ttl = 60
5054
allow_overwrite = var.validation_allow_overwrite_records
51-
52-
records = [
53-
local.cert_validation_domains[count.index]["resource_record_value"]
54-
]
5555
}
5656

5757
resource "aws_acm_certificate_validation" "self" {
58-
count = var.validate_certificate ? 1 : 0
59-
60-
certificate_arn = aws_acm_certificate.self.arn
58+
provider = aws.acm
59+
count = var.validate_certificate ? 1 : 0
6160

62-
validation_record_fqdns = local.cert_validation_domains[*]["resource_record_name"]
61+
certificate_arn = aws_acm_certificate.self.arn
62+
validation_record_fqdns = [for record in aws_route53_record.validation : record.fqdn]
6363
}

outputs.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ output "certificate_arn" {
55

66
output "certificate_domains" {
77
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)
8+
value = concat([aws_acm_certificate.self.domain_name], list(aws_acm_certificate.self.subject_alternative_names))
99
}
1010

1111
output "certificate_domain_validation_options" {

providers.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
provider "aws" {
2+
alias = "route53"
3+
}
4+
5+
provider "aws" {
6+
alias = "acm"
7+
}

versions.tf

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
terraform {
2-
required_version = ">= 0.12.0"
3-
}
2+
required_providers {
3+
aws = "~> 3.0"
4+
}
5+
6+
required_version = ">= 0.12.6"
7+
}

0 commit comments

Comments
 (0)