Consider the following example:
resource aws_route53_record acm_validation {
for_each = {
# use all the info as a key (which is unused) to ensure deduplication of records
for dvo in aws_acm_certificate.default.domain_validation_options : "${dvo.resource_record_name}/${dvo.resource_record_type}/${dvo.resource_record_value}" => {
name = dvo.resource_record_name
record = dvo.resource_record_value
type = dvo.resource_record_type
}
}
name = each.value.name
records = [each.value.record]
ttl = var.dns_ttl
type = each.value.type
zone_id = var.zone.id
}
Note that I’m trying to form a map where the key is:
"${dvo.resource_record_name}/${dvo.resource_record_type}/${dvo.resource_record_value}"
The reason I’m using such a long key is to de-duplicate records in the final map created. ACM certificate validations often contain duplicate records and I’d like to de-duplicate to not create/update the exact same record twice.
Terraform reports an error:
Error: Duplicate object key
…
Two different items produced the key “…”
If duplicates are expected, use the ellipsis (…) after the value expression to enable grouping by key
In a simpler example in terraform console
:
{ for k in ["a", "a"] : k => 1000 }
The error in full:
│ Error: Duplicate object key
│
│ on <console-input> line 1:
│ (source code not available)
│
│ Two different items produced the key "a" in this 'for' expression. If duplicates are expected, use the ellipsis (...) after
│ the value expression to enable grouping by key.
If I do group by key, like so:
{ for k in ["a", "a"] : k => true ... }
I get a result that I don’t want:
{
"a" = [
true,
true,
]
}
I simply want a merged output with duplicate keys superseded.
How can I deduplicate this in my above expression so no duplicate records are present in the map?
EDIT
I tried wrapping it in distinct
and no dice:
{ for dvo in distinct(aws_acm_certificate.default.domain_validation_options) : ... }
Still throws an error.