Optional Attributes in For Loop of For_each

Hello,

Is there a way to set optional attributes in a for loop of a for_each statement?

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            = i.custom_header #OPTIONAL?
    }]
    content {
      domain_name   = origin.value.name
      origin_id     = origin.value.id
      origin_path   = origin.value.path
      dynamic "custom_header" {
        for_each = [ for i in origin.value.custom_header[*] : {
          name = i.name
          value = i.value
        }]

I am trying to make the custom_header an optional attribute, but everything I have tried so far does not work.

Error: Unsupported attribute

  on .terraform/modules/bogie_cf/main.tf line 50, in resource "aws_cloudfront_distribution" "cloudfront_distribution":
  50:       custom_header            = i.custom_header

This object does not have an attribute named "custom_header".

In addition, if I set the custom_header to an empty value, then the name and value associated with that dynamic block causes the same attribute issue.

Error: Unsupported attribute

  on .terraform/modules/bogie_cf/main.tf line 58, in resource "aws_cloudfront_distribution" "cloudfront_distribution":
  58:           name = i.name

This value does not have any attributes.


Error: Unsupported attribute

  on .terraform/modules/bogie_cf/main.tf line 59, in resource "aws_cloudfront_distribution" "cloudfront_distribution":
  59:           value = i.value

This value does not have any attributes.

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
        }]