I am using Terraform v0.13.5. If I create a single AWS certificate resource for a domain registered in AWS, I can also successfully create a Route53 DNS validation record using:
resource "aws_acm_certificate" "api" {
domain_name = "api.example.com"
validation_method = "DNS"
lifecycle {
create_before_destroy = true
}
}
resource "aws_route53_record" "api_validation" {
for_each = {
for dvo in aws_acm_certificate.api.domain_validation_options : dvo.domain_name => {
name = dvo.resource_record_name
record = dvo.resource_record_value
type = dvo.resource_record_type
}
}
allow_overwrite = true
name = each.value.name
records = [each.value.record]
ttl = 60
type = each.value.type
zone_id = data.aws_route53_zone.example.zone_id # already exists
}
However I want to create multiple certificates using a for_each
with map variable. I have defined the aws_acm_certificate
resources using:
variable "sub_domains" {
type = map
default = {
"api" = "api"
"api_test" = "api.test"
}
}
resource "aws_acm_certificate" "certs" {
for_each = var.sub_domains
domain_name = "${each.value}.example.com"
validation_method = "DNS"
lifecycle {
create_before_destroy = true
}
}
I cannot work out how to refer to the dynamic certificates created when defining the validation records. The following snippet now does not work:
resource "aws_route53_record" "api_validation" {
for_each = {
for dvo in aws_acm_certificate.certs.domain_validation_options : dvo.domain_name => {
name = dvo.resource_record_name
record = dvo.resource_record_value
type = dvo.resource_record_type
}
}
allow_overwrite = true
name = each.value.name
records = [each.value.record]
ttl = 60
type = each.value.type
zone_id = data.aws_route53_zone.myzone.zone_id # already exists
}
Terraform complains with the following error:
Because aws_acm_certificate.certs has "for_each" set, its attributes must be
accessed on specific instances.
So how do I get the domain_validation_options
for each dynamically created certificate?