Optional Attributes in For Loop of For_each

Ok, so searching through a combination of this post and this post, I got to a solution that solves both issues. Hope this helps others who may come across this:

dynamic "origin" {
    for_each = [for i in "${var.dynamic_custom_origin_config}" : {
      name                     = i.domain_name
      id                       = i.origin_id
      path                     = i.origin_path
      http_port                = i.http_port
      https_port               = i.https_port
      origin_keepalive_timeout = i.origin_keepalive_timeout
      origin_read_timeout      = i.origin_read_timeout
      origin_protocol_policy   = i.origin_protocol_policy
      origin_ssl_protocols     = i.origin_ssl_protocols
      custom_header            = lookup(i, "custom_header", null)
    }]
    content {
      domain_name   = origin.value.name
      origin_id     = origin.value.id
      origin_path   = origin.value.path
      dynamic "custom_header" {
        for_each = origin.value.custom_header == null ? [] : [ for i in origin.value.custom_header : {
          name = i.name
          value = i.value
        }]
1 Like