I’m trying to follow the example at https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/acm_certificate#referencing-domain_validation_options-with-for_each-based-resources, but I can’t seem to work out how to handle a situation where there’s more than one aws_acm_certificate.example
.
It seems like the following should work, but I keep getting the error below.
variable "create_load_balancer" {
default = true
description = "Controls if the Load Balancer should created (it effects almost all resources)."
type = bool
}
variable "target_groups" {
type = map(list(string))
}
locals {
tuple_of_maps = [
for target_group, domains in var.target_groups :
{ for domain in domains : domain => target_group }
]
domains = merge(flatten([local.tuple_of_maps])...)
}
resource "aws_acm_certificate" "this" {
for_each = var.create_load_balancer ? local.domains : {}
domain_name = each.key
validation_method = "DNS"
tags = {
ManagedBy = "Terraform"
}
lifecycle {
create_before_destroy = true
}
}
locals {
domain_validation_options = var.create_load_balancer ? flatten([
for d in local.domains :
{
for dvo in aws_acm_certificate.this[d.key].domain_validation_options : dvo.domain_name => {
resource_record_name = dvo.resource_record_name
resource_record_value = dvo.resource_record_value
resource_record_type = dvo.resource_record_type
}
}
]) : []
}
resource "aws_route53_record" "this" {
for_each = {
for dvo in local.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
type = each.value.type
zone_id = var.zone_id
records = [each.value.record]
ttl = 300
}
Error: Unsupported attribute
on modules/util/aws/https-load-balancer/main.tf line 80, in locals:
80: for dvo in aws_acm_certificate.this[d.key].domain_validation_options : dvo.domain_name => {
This value does not have any attributes.