Given this module input:
variable "port_mapping_metadata" {
type = map(object({
target_type = optional(string, "instance")
ssl_policy = optional(string)
listener_cert_arn = optional(string)
additional_SNI_listener_cert_arns = optional(list(string),[])
...
I’m trying to create a resource per port_mapping_metadata
(if not null, keys may be duplicates across resources), per item in additional_SNI_listener_cert_arns
(if not empty). I have tried writing the resource as with a for_each as follows:
resource "aws_lb_listener_certificate" "additional_SNI_listener_certs" {
for_each = {
for k, v in var.port_mapping_metadata : k => {
for index, cert_arn in try(v.additional_SNI_listener_cert_arns, []) : "${k}-${index}" => cert_arn
}
if try(v.additional_SNI_listener_cert_arns, []) != [] && try(v.additional_SNI_listener_cert_arns, null) != null
}
##this _should_ be "{ "listener_name-0" = "arn1" , ... }"
listener_arn = aws_lb_listener.all[element(split("-", each.key), 0)].arn ##this should be "listener_name"
certificate_arn = each.value ##this should be "arn1"
}
But for some reason I get the error:
│ Error: Invalid index
│
│ on .terraform/modules/load_balancer/load_balancer.tf line 235, in resource "aws_lb_listener_certificate" "additional_SNI_listener_certs":
│ 235: certificate_arn = each.value["cert_arn"]
│ ├────────────────
│ │ each.value is object with 2 attributes
│
│ The given key does not identify an element in this collection value.
I can’t understand why each.value
in the resource is not a single string arn1
.